Ansible 常用模块

Ansible 常用模块简述

Ansible 模块是执行具体任务的工具,常用模块按功能分为以下几类:


一、文件操作类

模块 功能 常用参数 示例
copy 复制文件到远程主机 src, dest, mode, owner copy src=/local/file dest=/remote/file mode=0644
file 管理文件属性 path, state, mode, owner file path=/tmp/dir state=directory mode=0755
lineinfile 管理文件中的行 path, line, regexp, state lineinfile path=/etc/hosts line="127.0.0.1 localhost"
blockinfile 管理多行文本块 path, block, marker blockinfile path=/etc/profile block="export JAVA_HOME=/usr/java"
fetch 从远程拉取文件 src, dest fetch src=/remote/file dest=/local/
stat 获取文件状态 path stat path=/etc/passwd

二、系统管理类

模块 功能 常用参数 示例
user 管理用户 name, state, groups, shell user name=john state=present groups=wheel
group 管理用户组 name, state, gid group name=developers state=present
systemd 管理 systemd 服务 name, state, enabled systemd name=nginx state=started enabled=yes
service 管理 SysVinit 服务 name, state, enabled service name=httpd state=restarted
cron 管理定时任务 name, minute, job, state cron name="backup" minute=0 hour=2 job="/backup.sh"
reboot 重启系统 reboot_timeout, pre_reboot_delay reboot reboot_timeout=300
shutdown 关机 minutes, delay shutdown minutes=5

三、软件包管理类

模块 功能 适用系统 示例
yum yum 包管理 RedHat/CentOS yum name=nginx state=present
dnf dnf 包管理 RedHat 8+ dnf name=python3 state=latest
apt apt 包管理 Ubuntu/Debian apt name=nginx state=present update_cache=yes
pip Python 包管理 通用 pip name=requests version=2.28 state=present
npm Node.js 包管理 通用 npm name=express state=present global=yes

四、命令执行类

模块 功能 适用场景 示例
command 执行命令(默认) 简单命令,无管道/重定向 command cat /etc/passwd
shell 执行 shell 命令 需要管道、重定向、变量 `shell "ps aux
raw 执行原始命令 无 Python 环境时 raw "yum install -y python"
script 执行本地脚本 脚本分发执行 script /local/script.sh

五、网络与防火墙类

模块 功能 常用参数 示例
firewalld 管理 firewalld service, port, permanent, state firewalld service=http permanent=yes state=enabled
iptables 管理 iptables 规则 chain, source, jump, protocol iptables chain=INPUT source=1.1.1.1 jump=ACCEPT
uri 发送 HTTP 请求 url, method, body, status_code uri url=http://localhost/health method=GET
get_url 下载文件 url, dest, checksum get_url url=http://example.com/file dest=/tmp/

六、数据库类

模块 功能 示例
mysql_db MySQL 数据库管理 mysql_db name=mydb state=present
mysql_user MySQL 用户管理 mysql_user name=app password=pass priv=*.*:ALL
postgresql_db PostgreSQL 数据库管理 postgresql_db name=mydb state=present

七、常用模块速查表

任务场景 推荐模块 示例
安装 Nginx yum/dnf/apt yum name=nginx state=present
启动 Nginx systemd/service systemd name=nginx state=started
复制配置文件 copy/template copy src=nginx.conf dest=/etc/nginx/
创建用户 user user name=appuser groups=nginx
创建目录 file file path=/data state=directory
下载文件 get_url get_url url=https://... dest=/tmp/
执行脚本 script script /local/install.sh
重启服务器 reboot reboot reboot_timeout=300

八、模块使用示例

yaml 复制代码
# Playbook 综合示例
- hosts: webservers
  tasks:
    # 1. 安装 Nginx
    - name: Install nginx
      yum:
        name: nginx
        state: present

    # 2. 复制配置文件
    - name: Copy nginx config
      copy:
        src: /local/nginx.conf
        dest: /etc/nginx/nginx.conf
        mode: '0644'
      notify: restart nginx

    # 3. 创建应用目录
    - name: Create app directory
      file:
        path: /var/www/myapp
        state: directory
        owner: nginx
        mode: '0755'

    # 4. 启动服务
    - name: Start nginx
      systemd:
        name: nginx
        state: started
        enabled: yes

  handlers:
    - name: restart nginx
      systemd:
        name: nginx
        state: restarted

九、常用命令

bash 复制代码
# 查看模块文档
ansible-doc copy          # 查看 copy 模块详细文档
ansible-doc -l | grep yum # 列出 yum 相关模块

# 临时执行(ad-hoc)
ansible all -m ping                                    # 连通性测试
ansible all -m copy -a "src=/local/file dest=/remote/" # 复制文件
ansible all -m yum -a "name=nginx state=present"       # 安装软件
ansible all -m shell -a "df -h"                        # 执行命令

一句话总结

Ansible 常用模块覆盖**文件(copy/file/lineinfile)、系统(user/systemd)、软件包(yum/apt)、命令(shell/command)、网络(firewalld/uri)**五大类,通过模块化实现配置管理自动化。

相关推荐
feng68_2 小时前
Ansible还原数据库节点
linux·运维·数据库·ansible
QQsuccess2 小时前
人工智能(AI)全体系学习——系列三
人工智能·python·深度学习·学习
老师好,我是刘同学2 小时前
Python执行系统命令的最佳实践
python
EasyCVR2 小时前
国标GB28181/RTSP/ONVIF/RTMP视频监控平台EasyCVR视频质量诊断花屏/蓝屏/画面模糊/冻结检测
网络·数据库·音视频
郝学胜-神的一滴2 小时前
深入解析:生成器在UserList中的应用与Python可迭代对象实现原理
开发语言·python·程序人生·算法
萝卜白菜。2 小时前
Http GET / 请求返回值不同的问题
网络·网络协议·http
犽戾武2 小时前
机械臂 VR 遥操作调试日志记录
linux·服务器·网络
SPC的存折2 小时前
1、Ansible之Ansible安装与入门
linux·数据库·ansible
李昊哲小课2 小时前
aiomysql 完整实战教程
python·mysql·pymysql·aiomysql