
synchronize 模块是 Ansible 中对 rsync 工具的一个封装。它主要用于在控制节点和远程主机之间高效地同步文件和目录。它利用 rsync 的增量传输算法同步时只传输发生变化的部分因此在处理大量文件或大文件时效率远超 copy 等模块参数src定义要同步的源文件或目录。dest目标路径。定义同步的目标位置。mode定义同步方式。push推送默认将本地文件推送到远程pull拉取则将远程文件拉取到本地。archive默认值yes开启归档模式。相当于 rsync 的 -a 选项会保留权限、所有者、时间戳等文件属性。compress默认值为noyes代表传输压缩。在传输过程中压缩数据可节省带宽但会消耗一些 CPU 资源。delete默认值为noyes删除无关文件。删除目标端存在但源端不存在的文件使目标完全与源一致。rsync_opts自定义 rsync 参数。可以传递额外的 rsync 命令行选项如 --exclude。recursive默认值yes递归目录。当 archiveyes 时自动启用。dest_port默认值为22目标 SSH 端口。如果目标主机的 SSH 端口不是默认的 22则需指定。delay_updatesdelay_updates 参数控制 rsync 的 --delay-updates 选项。启用后rsync 会将更新的文件先放到一个临时目录等所有文件都传输完成再一次性重命名到目标位置第一次pull时需要将此参数设置为no示例将远程文件拉取到本地rootmaster:~# ansible web -m synchronize -a ‘src/etc dest/data00/ modepull compressyes archiveyes delay_updatesno’10.37.99.63 | CHANGED {“changed”: true,“cmd”: “/usr/bin/rsync --compress --archive --rsh‘/usr/bin/ssh -S none -o StrictHostKeyCheckingno -o UserKnownHostsFile/dev/null’ --out-format‘%i %n%L’ 10.37.99.63:/etc /data00/”,#…省略万字内容}回到顶部软件包管理模块yumCentOS/RHEL 系统安装软件核心参数name服务名state进行的操作安装、更新、删除installed/present 安装软件包默认不更新默认值removed/absen 删除软件包类似 yum removed需要慎用latest安装或更新(安装最新版本)示例安装常用的软件htop,iotop、tree、sshpassrootmaster:/data00# ansible web -m yum -a ‘namehtop,iotop,tree,sshpass statelatest’aptUbuntu/Debian系统安装软件核心参数name服务名update_cache更新 apt 缓存state进行的操作安装、更新、删除installed/present 安装软件包默认不更新默认值removed/absen 删除软件包类似 yum removed需要慎用latest安装或更新(安装最新版本)示例安装常用的软件htop,iotop、tree、sshpassrootmaster:/data00# ansible web -m apt -a ‘namehtop,iotop,tree,sshpass statelatest’ -e ‘ansible_python_interpreter/usr/bin/python3’unarchive支持 tar/zip可解压本地包或远程 url 包。核心参数src指定要解压的归档文件路径。路径含义取决于 remote_src 的设置desc解压目标目录的绝对路径。注意该目录必须已存在模块不会自动创建remote_src控制源文件位置。no 表示文件在控制节点yes 表示文件已在远程主机owner指定解压后的所属用户group指定解压后的所属组mode设置解压后文件和目录的权限。建议使用引号括起来的八进制数如 ‘0755’示例压缩包在控制节点解压到目标主机rootmaster:~# ansible web -m unarchive -a ‘src/root/jdk.tar.gz dest/usr/local remote_srcno’压缩包在目标主机直接解压rootmaster:~# ansible web -m unarchive -a ‘src/root/jdk.tar.gz dest/usr/local remote_srcyes’archive压缩某个文件参数path要归档的源文件或目录路径。支持列表可指定多个路径。dest目标归档文件的完整路径。如果 path 是多个则此参数必须多个format归档格式。可选gz (tar.gz)、bz2、xz、zip、tar。remove归档成功后是否删除源文件yes是no否owner归档后的所属人group归档后的所属组mode归档后的权限示例ansible all -m archive -a “path/var/log dest/tmp/logs.tar.gz formatgz”回到顶部服务、进程模块systemd用于管理服务启动、停止、重启、开机自启动等参数name服务名称enabled指定服务是否开机自启动yes代表开机自启动no代表不是state表示服务的状态started启动stopped停止restarted重启reloaded重新加载示例rootmaster:~# ansible web -m systemd -a ‘namesshd enabledyes statestarted’10.37.120.9 | SUCCESS {“ansible_facts”: {“discovered_interpreter_python”: “/usr/local/bin/python3.13”},“changed”: false,“enabled”: true,# … 省略万字内容}回到顶部用户模块user远程批量管理用户例如创建、删除用户修改用户密码等参数name指定用户名uid用户idshell指定命令解释器create_home是否创建家目录no表示不创建yes表示创建state管理用户的动作present添加用户adsent删除用户password设置密码示例所有机器创建虚拟用户web_wwwrootmaster:~# ansible all -m user -a ‘nameweb_www statepresent create_homeno shell/sbin/nologin’10.37.120.9 | CHANGED {“ansible_facts”: {“discovered_interpreter_python”: “/usr/local/bin/python3.13”},“changed”: true,“comment”: “”,“create_home”: false,“group”: 2003,“groups”: “”,“home”: “/home/web_www”,“name”: “web_www”,“shell”: “/sbin/nologin”,“state”: “present”,“system”: false,“uid”: 2002}10.37.99.63 | CHANGED {“ansible_facts”: {“discovered_interpreter_python”: “/usr/local/bin/python3.13”},“changed”: true,“comment”: “”,“create_home”: false,“group”: 2003,“groups”: “”,“home”: “/home/web_www”,“name”: “web_www”,“shell”: “/sbin/nologin”,“state”: “present”,“system”: false,“uid”: 2002}rootmaster:~# ansible all -m command -a ‘id web_www’10.37.120.9 | CHANGED | rc0 uid2002(web_www) gid2003(web_www) groups2003(web_www)10.37.99.63 | CHANGED | rc0 uid2002(web_www) gid2003(web_www) groups2003(web_www)创建www用户并设置密码为wwwrootmaster:~# ansible all -m user -a “namewww statepresent shell/bin/bash password{{ ‘www’ | password_hash(‘sha512’, ‘1’) }}”groupgroup是批量管理用户组的模块核心参数name用户组名state管理用户组的动作present添加用户组adsent删除用户组gid用户组的id示例rootmaster:~# ansible web -m group -a ‘nametest gid11111 statepresent’10.37.120.9 | CHANGED {“ansible_facts”: {“discovered_interpreter_python”: “/usr/local/bin/python3.13”},“changed”: true,“gid”: 11111,“name”: “test”,“state”: “present”,“system”: false}10.37.99.63 | CHANGED {“ansible_facts”: {“discovered_interpreter_python”: “/usr/local/bin/python3.13”},“changed”: true,“gid”: 11111,“name”: “test”,“state”: “present”,“system”: false}回到顶部定时任务模块cron设置Linux系统的定时任务参数name定时任务的名称minute指定分钟hour指定小时day指定天month指定月份week指定周几job要执行的命令或脚本即 cron 中的命令行部分。state处理任务的动作present表示新增任务absent表示删除匹配的任务示例每天凌晨2点备份日志rootmaster:~# ansible all -m cron -a “name‘nginx backup’ minute0 hour2 job‘/bin/sh /data/sh/log_backup.sh /var/log/backup.log’ statepresent”10.37.120.9 | CHANGED {“ansible_facts”: {“discovered_interpreter_python”: “/usr/local/bin/python3.13”},“changed”: true,“envs”: [],“jobs”: [“nginx backup”]}10.37.99.63 | CHANGED {“ansible_facts”: {“discovered_interpreter_python”: “/usr/local/bin/python3.13”},“changed”: true,“envs”: [],“jobs”: [“nginx backup”]}