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 |
八、模块使用示例
- hosts: webservers
tasks:
- name: Install nginx
yum:
name: nginx
state: present
- name: Copy nginx config
copy:
src: /local/nginx.conf
dest: /etc/nginx/nginx.conf
mode: '0644'
notify: restart nginx
- name: Create app directory
file:
path: /var/www/myapp
state: directory
owner: nginx
mode: '0755'
- name: Start nginx
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: restart nginx
systemd:
name: nginx
state: restarted
九、常用命令
ansible-doc copy
ansible-doc -l | grep yum
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)**五大类,通过模块化实现配置管理自动化。
所有评论(0)