1. tags:
tags是给任务增加标签,这样我们可以在执行playbook的时候指定标签去执行任务,让我们更有针对性的执行任务,
多个任务可以拥有同一个标签,一个任务也可以拥有多个标签
使用参数 -t 后面加标签 则只有带标签的任务才会被执行
# ansible-playbook -t config_nginx nginx.yml
2. handlers:
使用handlers可以定义一些“触发任务”,这些触发任务正在在执行的时候是不执行的,它们可以被tasks中的其他任务中调用.
2.1 执行条件
执行条件是task中引用的任务状态为更改时才会触发这些“触发任务”!
一般用handlers定义一些重启或者重载服务的任务,然后和推送配置的文件的任务结合使用
3.实例:源码安装nginx
3.1 定义主机清单
[root@localhost ~]# vim /etc/ansible/hosts
[webservers]
192.168.153.184
3. 2 编写nginx配置文件模板
3.2.1 复制配置文件到nginx目录下
[root@localhost ~]# cp nginx_lb/nginx-1.18.0/conf/nginx.conf nginx/
3.2.2 修改配置文件内容如下
[root@localhost ~]# cd nginx
[root@localhost nginx]# vim nginx.conf
修改配置文件的内容如下,使用变量
3 worker_processes {{ ansible_processor_vcpus }};
36 listen {{ ngx_port}};
3. 3 编写playbook文件
playbook文件内容如下:
[root@localhost nginx]# vim nginx.yml
---
- hosts: webservers
vars: #定义变量,nginx监听端口为8000
ngx_port: 8000
tasks:
- name: "分发nginx源码包"
unarchive: src=/root/nginx-1.18.0.tar.gz dest=/root/
- name: "安装依赖环境"
yum: name=gcc,pcre-devel,openssl-devel state=latest
- name: "编译安装nginx"
shell: cd /root/nginx-1.18.0/ && ./configure && make && make install
- name: "推送配置文件模板"
template: src=/root/nginx/nginx.conf dest=/usr/local/nginx/conf/nginx.conf
tags: #给"推送配置文件模板"任务打标签
- cfg-nginx
notify: #引用handlers触发任务,若nginx配置文件内容发生变化则触发
- reload-nginx
- name: "开启nginx"
shell: /usr/local/nginx/sbin/nginx
- name: "关闭防火墙"
service: name=firewalld state=stopped
handlers: #定义触发任务为重新读取nginx配置文件
- name: "reload-nginx"
shell: /usr/local/nginx/sbin/nginx -s reload
3. 4 测试并执行playbook文件
3.4.1 测试
[root@localhost nginx]# ansible-playbook --syntax-check nginx.yml
playbook: nginx.yml
[root@localhost nginx]#
3.4.2 执行
[root@localhost nginx]# ansible-playbook nginx.yml
PLAY [webservers] *******************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.153.184]
TASK [分发nginx源码包] *******************************************************************************************************************
changed: [192.168.153.184]
TASK [安装依赖环境] ***********************************************************************************************************************
changed: [192.168.153.184]
TASK [编译安装nginx] ********************************************************************************************************************
changed: [192.168.153.184]
TASK [推送配置文件模板] *********************************************************************************************************************
changed: [192.168.153.184]
TASK [开启nginx] **********************************************************************************************************************
changed: [192.168.153.184]
RUNNING HANDLER [reload-nginx] ******************************************************************************************************
changed: [192.168.153.184]
PLAY RECAP **************************************************************************************************************************
192.168.153.184 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost nginx]#
4. 验证:
4.1 浏览器输入IP地址和定义的8000端口访问成功
4.2 修改playbook文件验证
4.2.1 将定义变量varsi部分nginx监听端口设为80
vars:
ngx_port: 8000
4.2.2 再次执行推送配置文件的任务
[root@localhost nginx]# ansible-playbook -t cfg-nginx nginx.yml
PLAY [webservers] *******************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.153.184]
TASK [推送配置文件模板] *********************************************************************************************************************
changed: [192.168.153.184]
配置文件的内容发生变化,触发handlers的nginx重启任务
RUNNING HANDLER [reload-nginx] ******************************************************************************************************
changed: [192.168.153.184]
PLAY RECAP **************************************************************************************************************************
192.168.153.184 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost nginx]#
4.2.36 浏览器验证
端口已经发生变化
4.2.3 不修改配置文件执行推送配置文件的任务
[root@localhost nginx]# ansible-playbook -t cfg-nginx nginx.yml
PLAY [webservers] *******************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.153.184]
##配置文件的内容没有发生变化,则不会触发handlers的任务
TASK [推送配置文件模板] *********************************************************************************************************************
ok: [192.168.153.184]
PLAY RECAP **************************************************************************************************************************
192.168.153.184 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost nginx]#