异步模式
async:参数值代表了这个任务执行时间的上限值。即任务执行所用时间如果超出这个时间,则认为任务失败。此参数若未设置,则为同步执行。
poll:参数值代表了任务异步执行时轮询的时间间隔,另外,如果poll为0,就相当于一个不关心结果的任务
1 2 3 4 5 6 7 8 9 10 11 12
| [root@ceshi-128 playbook]# cat http.yml --- - hosts: all user: root gather_facts: false tasks: - name: yum yum: name: httpd state: installed async: 60 超时时长60秒 poll: 0 无结果执行
|
任务委托
比如批量执行任务,但需要某台机器去干别的事情
delegate_to 语句来在另一台主机上运行task
举例: 批量安装httpd服务,但在某一台机器安装mariadb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| [root@ceshi-128 playbook]# cat http.yml --- - hosts: all user: root gather_facts: false tasks: - name: install httpd yum: name: httpd state: installed - name: install mariadb yum: name: mairadb state: installed delegate_to: 10.1.74.24
|
滚动执行
serial:可以在一个特定时间同时控制多少主机
如下例子:比如当我们有4台机器需要跑任务,serial限制每次只是一台去启动服务
1 2 3 4 5 6 7 8 9 10 11 12 13
| [root@ceshi-128 playbook]# cat serial.yml --- - hosts: ceshi user: root gather_facts: false serial: 1 tasks: - name: start httpd service: name: httpd state: started - wait_for: port: 8888
|
命令执行模块
- command:默认模块。在远程主机执行命令,不支持管道
|、重定向 > 及 shell 变量(如 $HOME)
- shell:在远程主机 shell 中执行命令,支持管道和特殊符号,比 command 灵活,但安全性稍低
- script:将本地(控制端)的脚本传输到远程主机并执行
1 2 3 4 5 6 7 8
| - name: 使用 command 执行命令 (不支持管道) command: /usr/bin/python3 --version
- name: 使用 shell 执行复杂命令 shell: ps -ef | grep httpd | grep -v grep
- name: 在远程主机执行本地脚本 script: /local/path/to/myscript.sh
|
文件管理模块
- file:用于创建目录、文件,修改权限、属主,创建软链接等
- copy:将本地文件复制到远程主机(类似 scp)
- fetch:将远程文件拉取到本地(copy 的反向操作)
- template:基于 Jinja2 模板引擎,将含有变量的模板文件渲染后部署到远程主机(配置文件神器)
1 2 3 4 5 6 7 8
| - name: 使用 command 执行命令 (不支持管道) command: /usr/bin/python3 --version
- name: 使用 shell 执行复杂命令 shell: ps -ef | grep httpd | grep -v grep
- name: 在远程主机执行本地脚本 script: /local/path/to/myscript.sh
|
文件管理模块
- file:用于创建目录、文件,修改权限、属主,创建软链接等
- copy:将本地文件复制到远程主机(类似 scp)
- fetch:将远程文件拉取到本地(copy 的反向操作)
- template:基于 Jinja2 模板引擎,将含有变量的模板文件渲染后部署到远程主机(配置文件神器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| - name: 创建目录 file: path: /data/www state: directory owner: www group: www mode: '0755'
- name: 创建软链接 file: src: /data/www dest: /var/www/html state: link
- name: 推送配置文件 copy: src: ./nginx.conf dest: /etc/nginx/nginx.conf backup: yes
- name: 使用模板渲染配置 (支持变量替换) template: src: ./httpd.conf.j2 dest: /etc/httpd/conf/httpd.conf
|
文本处理模块
- lineinfile:确保某一行文本存在于文件中,或使用正则替换某一行(修改配置文件的瑞士军刀)
- blockinfile:在文件中插入一段文本块(通常带有标记)
1 2 3 4 5
| - name: 修改 SELinux 配置 lineinfile: path: /etc/selinux/config regexp: '^SELINUX=' line: 'SELINUX=disabled'
|
软件与服务模块
- yum / dnf:RedHat/CentOS 系列的包管理
- apt:Debian/Ubuntu 系列的包管理
- service / systemd:管理服务的启动、停止、重启、开机自启
1 2 3 4 5 6 7 8 9 10
| - name: 安装 nginx yum: name: nginx state: installed
- name: 启动 nginx 并设置开机自启 service: name: nginx state: started enabled: yes
|
系统管理模块
- user:管理系统用户
- group:管理用户组
- cron:管理 crontab 计划任务
- mount:挂载文件系统
1 2 3 4 5 6 7 8 9 10 11 12 13
| - name: 创建用户 deploy user: name: deploy shell: /bin/bash groups: wheel append: yes
- name: 添加定时任务 (每天凌晨2点备份) cron: name: "daily backup" minute: "0" hour: "2" job: "/usr/local/bin/backup.sh > /dev/null 2>&1"
|
其他实用模块
- debug:用于打印调试信息(类似 print)
- setup:收集远程主机的 facts 信息(IP、CPU、内存等),
gather_facts: true 时自动调用
- get_url:从网络下载文件(类似 wget/curl)
- unarchive:解压压缩包(支持 zip, tar.gz 等),可选择在本地解压后上传或直接在远程解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| - name: 打印变量信息 debug: msg: "当前主机的IP是 {{ ansible_default_ipv4.address }}"
- name: 下载文件 get_url: url: http://example.com/app.tar.gz dest: /tmp/app.tar.gz
- name: 解压文件到指定目录 unarchive: src: /tmp/app.tar.gz dest: /opt/app/ remote_src: yes
|