官网内置模块:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#plugins-in-ansible-builtin
一、file 模块
file
模块是ansible
中用于管理文件和目录的核心模块,它可以创建文件、目录、符号链接、设置权限和属性等。
常用参数
touch
:创建文件、更新时间戳。file
:修改文件属性
示例:
1. 创建目录
ansible all -m file -a "path=/opt/dir state=directory"
2. 创建文件
ansible all -m file -a "path=/opt/dir/file1.txt state=touch"
3. 创建符号链接
ansible all -m file -a "src=/etc/hosts dest=/opt/dir/hosts_link state=link"
4. 删除文件或目录
### 删除目录
ansible all -m file -a "path=/opt/dir state=absent"
### 删除文件
ansible all -m file -a "path=/opt/dir/file1.txt state=absent"
5. 修改文件属性
### 修改文件权限
ansible all -m file -a "path=/opt/dir/file1.txt mode=0640 owner=root group=root"
### 递归修改目录权限
ansible all -m file -a "path=/opt/dir mode=0640 owner=root group=root recurse=yes"
二、user 模块
Ansible 的 user
模块用于管理系统用户账户,包括创建、修改和删除用户,以及管理用户属性如密码、组、家目录等。
常用参数
1. 创建用户
ansible all -m user -a "name=testuser state=present shell=/sbin/nologin group=test groups=test1 home=/home/test create_home=yes"
2. 删除用户
ansible all -m user -a "name=testuser state=absent remove=yes"
三、group 模块
group
模块是 Ansible 中用于管理系统用户组的核心模块,可以创建、修改和删除用户组。
主要参数
1. 创建基本用户组
ansible all -m group -a "name=developers"
2. 创建指定 GID 的组
ansible webservers -m group -a "name=deploy gid=1042"
3. 创建系统组
ansible db_servers -m group -a "name=dbadmin system=yes"
4. 删除组
ansible old_servers -m group -a "name=legacy state=absent"
5. 修改组 GID
ansible app_servers -m group -a "name=appusers gid=1500"
四、yum 模块
yum
模块是 Ansible 中用于管理 基于 RPM 的 Linux 系统中软件包的核心模块。
主要参数
1. 安装单个软件包
ansible webservers -m yum -a "name=httpd state=present"
2. 安装最新版本
ansible all -m yum -a "name=nginx state=latest"
3. 安装多个软件包
ansible app_servers -m yum -a "name=vim-enhanced,git,tmux state=present"
4. 删除软件包
ansible old_servers -m yum -a "name=telnet state=absent"
5. 使用特定仓库安装
ansible all -m yum -a "name=http enablerepo=epel state=present"
6. 更新所有软件包
ansible production -m yum -a "name=* state=latest"
7. 仅安全更新
ansible critical -m yum -a "security=yes state=latest"
8. 安装包组
ansible dev_hosts -m yum -a "name='@development' state=present"
9. 下载但不安装
ansible all -m yum -a "name=ansible download_only=yes"
10. 从URL安装RPM
ansible all -m yum -a "name=https://example.com/packages/custom.rpm state=present" -b
11. 排除特定包更新
ansible all -m yum -a "name=* state=latest exclude=kernel*"
12. 清理YUM缓存
ansible all -m yum -a "clean=all"
13. 列出可用更新
ansible all -m yum -a "list=updates"
14. 检查包是否安装
ansible all -m yum -a "list=httpd"
15. 验证命令
# 检查包是否安装
ansible all -m shell -a "rpm -q httpd" -b
# 检查版本
ansible all -m shell -a "nginx -v" -b
五、copy 模块
copy
模块是 Ansible 中用于文件传输的核心模块,可以将本地文件复制到远程主机,或直接在远程主机上创建新文件。
主要参数
1. 基本文件复制
ansible webservers -m copy -a "src=/tmp/app.conf dest=/etc/app.conf"
2. 设置文件权限和所有者
ansible all -m copy -a "src=/tmp/script.sh dest=/usr/local/bin/script.sh owner=root group=root mode=0755"
3. 直接创建文件内容
ansible db_servers -m copy -a "content='DB_HOST=127.0.0.1' dest=/etc/db.conf"
4. 复制并备份原文件
ansible production -m copy -a "src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf backup=yes"
5. 在单个主机上复制文件
ansible hostA -m copy -a "src=/path/to/source dest=/path/to/dest remote_src=yes"
6. 验证命令
# 检查文件是否存在
ansible all -m shell -a "ls -l /etc/app.conf" -b
# 检查文件内容
ansible all -m shell -a "cat /etc/motd" -b
# 检查备份文件
ansible all -m shell -a "ls -l /etc/nginx/nginx.conf.*" -b
六、systmd 模块
systemd
模块是 Ansible 中用于管理系统服务的核心模块,专门用于基于 systemd 的 Linux 系统。
主要参数
1. 启动服务
ansible webservers -m systemd -a "name=nginx state=started"
2. 停止服务
ansible all -m systemd -a "name=httpd state=stopped"
3. 重启服务
ansible app_servers -m systemd -a "name=mysqld state=restarted"
4. 重载服务(不重启)
ansible load_balancers -m systemd -a "name=haproxy state=reloaded"
5. 启用开机自启
ansible db_servers -m systemd -a "name=postgresql enabled=yes"
6. 禁用开机自启
ansible legacy_servers -m systemd -a "name=tomcat enabled=no"
7. 检查服务状态
ansible all -m systemd -a "name=sshd"
8. 验证命令
# 检查服务状态
ansible all -m shell -a "systemctl is-active nginx"
# 检查是否启用
ansible all -m shell -a "systemctl is-enabled nginx"
# 查看详细状态
ansible all -m shell -a "systemctl status nginx --no-pager"
七、mount 模块
mount
模块是 Ansible 中用于管理文件系统挂载的核心模块,可以永久性配置 /etc/fstab
中的挂载项并控制当前挂载状态。
主要参数
mounted
会挂载并写入fstab
unmoted
会卸载但不改变fstab
present
只写入fstab
不执行挂载absent
删除fstab
,也取消挂载
1. 挂载本地设备并永久生效
ansible all -m mount -a "path=/data src=/dev/sdb1 fstype=ext4 state=mounted opts=rw,noatime" -b
2. 挂载 NFS 共享
ansible app_servers -m mount -a "path=/mnt/nfs src=nfs01:/data/share fstype=nfs opts=rw,hard,intr state=mounted" -b
3. 卸载文件系统(保留 fstab 记录)
ansible all -m mount -a "path=/mnt/temp state=unmounted" -b
4. 完全移除挂载配置
ansible old_servers -m mount -a "path=/mnt/legacy state=absent" -b
5. 临时挂载(不写入 fstab)
ansible test_servers -m mount -a "path=/tmp/test src=/dev/sdc1 fstype=xfs state=mounted boot=no" -b
6. 配置交换分区
ansible all -m mount -a "path=none src=/dev/sdb2 fstype=swap state=present" -b
7. 修改现有挂载选项
ansible webservers -m mount -a "path=/var/www src=/dev/sdd1 fstype=ext4 opts=rw,noexec state=present" -b
8. 检查挂载状态
ansible all -m mount -a "path=/mnt/data state=present" -b
9. 挂载 CIFS/SMB 共享
ansible clients -m mount -a "path=/mnt/smb src=//fileserver/share fstype=cifs opts=username=user,password=pass,uid=1000 state=mounted" -b
10. 验证命令
# 检查挂载点是否存在
ansible all -m shell -a "ls -ld /mnt/data"
# 检查当前挂载情况
ansible all -m shell -a "mount | grep /mnt/data"
# 检查 fstab 配置
ansible all -m shell -a "grep /mnt/data /etc/fstab"
# 检查挂载选项
ansible all -m shell -a "findmnt -n -o OPTIONS /mnt/data"
八、cron 模块
name 的作用是判断这个任务是否已经存在,以及更新覆盖。
cron
模块是 Ansible 中用于管理系统定时任务(cron jobs)的核心模块,可以创建、修改或删除 crontab 条目。
主要参数
1. 创建基本 cron 任务
ansible all -m cron -a 'name="日常备份" user=root job="/opt/scripts/backup.sh" minute=0 hour=2' -b
2. 使用预设时间(每天执行)
ansible webservers -m cron -a 'name="清理日志" job="/opt/scripts/clean_logs.sh" special_time=daily' -b
3. 创建复杂时间任务创建复杂时间任务
ansible db_servers -m cron -a 'name="数据库维护" minute=30 hour=3 day=15 month="*/2" weekday=1-5 job="/opt/db/maintenance.sh"' -b
4. 为特定用户创建任务
ansible app_servers -m cron -a 'user=appuser name="应用监控" job="/home/appuser/monitor.sh" minute="*/10"' -b
5. 禁用 cron 任务
ansible all -m cron -a 'name="日常备份" disabled=yes' -b
6. 删除 cron 任务
ansible old_servers -m cron -a 'name="旧任务" state=absent' -b
7. 带环境变量的任务
ansible all -m cron -a 'name="带环境变量的任务" job="/opt/scripts/env_test.sh" hour=1 environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"' -b
8. 检查 cron 任务是否存在
ansible all -m cron -a 'name="日常备份" job="/opt/scripts/backup.sh" minute=0 hour=2' -b
9. 验证命令
# 查看root用户的cron任务
ansible all -m shell -a 'crontab -l' -b
# 查看特定用户的cron任务
ansible all -m shell -a 'crontab -u appuser -l' -b
# 检查特定任务是否存在
ansible all -m shell -a 'crontab -l | grep "日常备份"' -b