自动化运维专题(二):Ansible批量自动化管理工具进阶

 

七,ansible的script模块批量运行脚本

ansible的script模块能够实现远程服务器批量运行本地的shell脚本。

  1. #操作示例-->远程批量分发并自动部署nginx
  2. #所有被管理端需要挂载光盘,并创建本地yum配置文件
  3. [root@ansible scripts]# pwd
  4. /service/scripts
  5. [root@ansible scripts]# ls | xargs -n1
  6. auto_nginx.sh #自动安装nginx脚本
  7. fenfa.sh #批量分发脚本
  8. nginx-1.10.2.tar.gz #nginx源码包
  9. ```
  10. [root@ansible scripts]# cat auto_nginx.sh #nginx安装脚本
  11. #!/bin/sh
  12. #nginx install shell scripts
  13. test -d /media/cdrom || mkdir -p /media/cdrom
  14. mount /dev/sr0 /media/cdrom &>/dev/null
  15. yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/null
  16. test -d /service/scripts || exit 3
  17. cd /service/scripts/
  18. tar xf nginx-1.10.2.tar.gz -C /usr/src/
  19. cd /usr/src/nginx-1.10.2/
  20. ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/null
  21. make &>/dev/null
  22. make install &>/dev/null
  23. exit 0
  24. ```
  25. [root@ansible scripts]# cat fenfa.sh #源码包和安装脚本的批量分发脚本
  26. #!/bin/sh
  27. #批量分发脚本
  28. Group=$1
  29. ansible $Group -m copy -a "src=/service/scripts/ dest=/service/scripts/"
  30. ansible $Group -m script -a "/service/scripts/auto_nginx.sh"
  31. [root@ansible scripts]# sh fenfa.sh all #激活脚本

此脚本只是个演示示例,工作中需要写的尽量严谨一些。

八,ansible-playbook的初步使用

playbook的使用,playbook可以把ansible的模块进行组合

8.1 playbook的简单shell模块的使用

  1. [root@ansible scripts]# cat test_shell.yaml #playbook的执行模板
  2. --- #开头三个小-开头
  3. - hosts: webB
  4. tasks:
  5. - name: test
  6. shell: echo "welcome to yunjisaun" >> /tmp/username
  7. - name: test2
  8. shell: echo "welcome to yunjisuan" >> /tmp/username
  9. 模板说明:
  10. --- #开头必须有三个小-,顶格写
  11. - hosts #正文配置代码的第一级,必须有两个空格(-占一个空格位)
  12. - host: webB #webB是host参数的值,值和hosts:之间要有一个空格
  13. tasks: #tasks:表示接下来要执行的具体任务
  14. - name: #相对于tasks再多缩进两个格(-占一个空格位),表示属于tasks的下一级
  15. - name: test #test只是要执行的具体命令的名字可以随便写。name:后还是有一个空格要注意
  16. shell: #表示调用shell模块执行命令相对于tasks仍旧要多缩进两个空格
  17. shell: echo "xxx" >> xxx #shell:后边还是要有个空格,需要注意。

执行playbook配置文件

  1. [root@ansible scripts]# ansible-playbook test_shell.yaml #执行playbook配置文件
  2. PLAY [webB] ********************************************************************************************************
  3. TASK [Gathering Facts] *********************************************************************************************
  4. ok: [webB]
  5. TASK [test] ********************************************************************************************************
  6. changed: [webB]
  7. TASK [test2] *******************************************************************************************************
  8. changed: [webB]
  9. PLAY RECAP *********************************************************************************************************
  10. webB : ok=3 changed=2 unreachable=0 failed=0

8.2 playbook的简单copy模块的使用

  1. [root@ansible scripts]# echo "welcom to yunjisuan" >> /tmp/test_copy
  2. [root@ansible scripts]# cat test_copy.yaml
  3. ---
  4. - hosts: all
  5. tasks:
  6. - name: test copy
  7. copy: src=/tmp/copy_test dest=/tmp/
  8. [root@ansible scripts]# ansible-playbook /service/scripts/test_copy.yaml
  9. PLAY [all] *********************************************************************************************************
  10. TASK [Gathering Facts] *********************************************************************************************
  11. ok: [webA]
  12. ok: [webB]
  13. TASK [test copy] ***************************************************************************************************
  14. changed: [webA]
  15. changed: [webB]
  16. PLAY RECAP *********************************************************************************************************
  17. webA : ok=2 changed=1 unreachable=0 failed=0
  18. webB : ok=2 changed=1 unreachable=0 failed=0

8.3 playbook使用register输出命令运行结果

我们在用playbook进行ansible模块操作的时候,并没有命令的执行结果输出,默认被隐藏了。
我们可以通过register模块最加输出命令的执行结果

  1. [root@ansible scripts]# cat test_register.yaml
  2. ---
  3. - hosts: all
  4. tasks:
  5. - name: test register
  6. shell: echo "welcome to yunjisuan"
  7. register: print_result #将之前命令的输出结果保存在变量print_result里
  8. - debug: var=print_result #将变量的值作为debug输出出来。
  9. [root@ansible scripts]# ansible-playbook test_register.yaml
  10. PLAY [all] *********************************************************************************************************
  11. TASK [Gathering Facts] *********************************************************************************************
  12. ok: [webA]
  13. ok: [webB]
  14. TASK [test register] ***********************************************************************************************
  15. changed: [webA]
  16. changed: [webB]
  17. TASK [debug] *******************************************************************************************************
  18. ok: [webA] => { #命令的执行结果有输出了
  19. "print_result": {
  20. "changed": true,
  21. "cmd": "echo \"welcome to yunjisuan\"",
  22. "delta": "0:00:00.002269",
  23. "end": "2018-06-15 10:28:14.693883",
  24. "failed": false,
  25. "rc": 0,
  26. "start": "2018-06-15 10:28:14.691614",
  27. "stderr": "",
  28. "stderr_lines": [],
  29. "stdout": "welcome to yunjisuan",
  30. "stdout_lines": [
  31. "welcome to yunjisuan"
  32. ]
  33. }
  34. }
  35. ok: [webB] => {
  36. "print_result": {
  37. "changed": true,
  38. "cmd": "echo \"welcome to yunjisuan\"",
  39. "delta": "0:00:00.002633",
  40. "end": "2018-06-15 10:28:14.710242",
  41. "failed": false,
  42. "rc": 0,
  43. "start": "2018-06-15 10:28:14.707609",
  44. "stderr": "",
  45. "stderr_lines": [],
  46. "stdout": "welcome to yunjisuan",
  47. "stdout_lines": [
  48. "welcome to yunjisuan"
  49. ]
  50. }
  51. }
  52. PLAY RECAP *********************************************************************************************************
  53. webA : ok=3 changed=1 unreachable=0 failed=0
  54. webB : ok=3 changed=1 unreachable=0 failed=0

8.4 nginx配置下发并检测

  1. [root@ansible scripts]# cat test_nginx_conf.yaml
  2. ---
  3. - hosts: all
  4. tasks:
  5. - name: copy nginx.conf
  6. copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes
  7. - name:
  8. shell: /usr/local/nginx/sbin/nginx -t
  9. register: nginx_result
  10. - debug: var=nginx_result

九,playbook的自定义变量和内置变量

9.1 在Playbook中使用自定义变量

  1. [root@localhost scripts]# cat test_vars.yaml
  2. ---
  3. - hosts: all
  4. vars: #定义变量
  5. - name: "yunjisuan" #第一个name变量
  6. age: "3" #第二个age变量
  7. tasks:
  8. - name: "{{ name }}" #{{}}两对大括号引用变量,变量名两头空格
  9. shell: echo "myname {{ name }},myage {{ age }}"
  10. register: var_result
  11. - debug: var=var_result
  12. 特别提示:
  13. 引用变量需要在双引号中引用。
  14. [root@localhost scripts]# ansible-playbook /service/scripts/test_vars.yaml
  15. [WARNING]: Found variable using reserved name: name #这里提示,name是一个保留的内置变量,我们在自定义时不能用
  16. PLAY [all] *********************************************************************************************************
  17. TASK [Gathering Facts] *********************************************************************************************
  18. ok: [webA]
  19. ok: [webB]
  20. TASK [yunjisuan] ***************************************************************************************************
  21. changed: [webA]
  22. changed: [webB]
  23. TASK [debug] *******************************************************************************************************
  24. ok: [webA] => {
  25. "var_result": {
  26. "changed": true,
  27. "cmd": "echo \"myname yunjisuan,myage 3\"",
  28. "delta": "0:00:00.002320",
  29. "end": "2018-06-19 10:45:16.175728",
  30. "failed": false,
  31. "rc": 0,
  32. "start": "2018-06-19 10:45:16.173408",
  33. "stderr": "",
  34. "stderr_lines": [],
  35. "stdout": "myname yunjisuan,myage 3",
  36. "stdout_lines": [
  37. "myname yunjisuan,myage 3"
  38. ]
  39. }
  40. }
  41. ok: [webB] => {
  42. "var_result": {
  43. "changed": true,
  44. "cmd": "echo \"myname yunjisuan,myage 3\"",
  45. "delta": "0:00:00.002518",
  46. "end": "2018-06-19 10:45:10.552331",
  47. "failed": false,
  48. "rc": 0,
  49. "start": "2018-06-19 10:45:10.549813",
  50. "stderr": "",
  51. "stderr_lines": [],
  52. "stdout": "myname yunjisuan,myage 3",
  53. "stdout_lines": [
  54. "myname yunjisuan,myage 3"
  55. ]
  56. }
  57. }
  58. PLAY RECAP *********************************************************************************************************
  59. webA : ok=3 changed=1 unreachable=0 failed=0
  60. webB : ok=3 changed=1 unreachable=0 failed=0
  61. #我们修改一下name这个变量再发送,就不会出警告了
  62. [root@localhost scripts]# cat test_vars.yaml
  63. ---
  64. - hosts: all
  65. vars:
  66. - names: "yunjisuan"
  67. age: "3"
  68. tasks:
  69. - name: "{{ names }}"
  70. shell: echo "myname {{ names }},myage {{ age }}"
  71. register: var_result
  72. - debug: var=var_result

在使用自定义变量时,我们要特别注意不要和系统的内置保留变量同名,容易引发问题。

9.2 在playbook中使用ansible内置变量

我们可以使用ansible all -m setup | less查看ansible内置变量

  1. [root@localhost scripts]# cat test_setupvars.yaml
  2. ---
  3. - hosts: all
  4. gather_facts: True #使用ansible内置变量
  5. tasks:
  6. - name: setup var
  7. shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}"
  8. register: var_result
  9. - debug: var=var_result
  10. [root@localhost scripts]#
  11. [root@localhost scripts]# ansible-playbook test_setupvars.yaml
  12. PLAY [all] *********************************************************************************************************
  13. TASK [Gathering Facts] *********************************************************************************************
  14. ok: [webA]
  15. ok: [webB]
  16. TASK [setup var] ***************************************************************************************************
  17. changed: [webA]
  18. changed: [webB]
  19. TASK [debug] *******************************************************************************************************
  20. ok: [webA] => {
  21. "var_result": {
  22. "changed": true,
  23. "cmd": "echo \"ip 192.168.200.132 cpu 1\"",
  24. "delta": "0:00:00.002408",
  25. "end": "2018-06-19 11:32:44.540658",
  26. "failed": false,
  27. "rc": 0,
  28. "start": "2018-06-19 11:32:44.538250",
  29. "stderr": "",
  30. "stderr_lines": [],
  31. "stdout": "ip 192.168.200.132 cpu 1",
  32. "stdout_lines": [
  33. "ip 192.168.200.132 cpu 1"
  34. ]
  35. }
  36. }
  37. ok: [webB] => {
  38. "var_result": {
  39. "changed": true,
  40. "cmd": "echo \"ip 192.168.200.138 cpu 1\"",
  41. "delta": "0:00:00.002102",
  42. "end": "2018-06-19 11:32:44.526875",
  43. "failed": false,
  44. "rc": 0,
  45. "start": "2018-06-19 11:32:44.524773",
  46. "stderr": "",
  47. "stderr_lines": [],
  48. "stdout": "ip 192.168.200.138 cpu 1",
  49. "stdout_lines": [
  50. "ip 192.168.200.138 cpu 1"
  51. ]
  52. }
  53. }
  54. PLAY RECAP *********************************************************************************************************
  55. webA : ok=3 changed=1 unreachable=0 failed=0
  56. webB : ok=3 changed=1 unreachable=0 failed=0

简单演示一下ansible内置变量的取用方法ansible all -m setup | less

  1. [root@localhost scripts]# cat test_setupvars.yaml
  2. ---
  3. - hosts: all
  4. gather_facts: True
  5. tasks:
  6. - name: setup var
  7. shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}" >> /tmp/test
  8. - name: setup var2
  9. shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test
  10. register: var_result
  11. - debug: var=var_result

十,Playbook下发可变配置文件

配置文件如果使用copy模块去下发的话,那配置都是一样的;
如果下发的配置文件里有可变的配置,需要用到template模块。

10.1 利用template模块下发可变的配置文件

  1. [root@localhost scripts]# cat /tmp/test
  2. my name is {{ myname }} #自定义变量
  3. my name is {{ ansible_all_ipv4_addresses[0] }} #系统变量
  4. [root@localhost scripts]# cat test_filevars.yaml
  5. ---
  6. - hosts: all
  7. gather_facts: True #开启系统变量
  8. vars:
  9. - myname: "yunjisuan" #自定义变量
  10. tasks:
  11. - name: template test
  12. template: src=/tmp/test dest=/root/test #使用template下发可变配置文件
  13. [root@localhost scripts]# ansible-playbook test_filevars.yaml

10.2 下发配置文件里面使用判断语法

  1. [root@localhost scripts]# cat /tmp/if.j2
  2. {% if PORT %} #if PORT存在
  3. ip=0.0.0.0:{{ PORT }}
  4. {% else %} #否则的话
  5. ip=0.0.0.0:80
  6. {% endif %} #结尾
  7. [root@localhost scripts]# cat test_ifvars.yaml
  8. ---
  9. - hosts: all
  10. gather_facts: True #开启系统内置变量
  11. vars:
  12. - PORT: 90 #自定义变量
  13. tasks:
  14. - name: jinja2 if test
  15. template: src=/tmp/if.j2 dest=/root/test
  16. [root@localhost scripts]# ansible-playbook test_ifvars.yaml

如果我们将变量PORT值为空的话,就会是另外的结果

  1. [root@localhost scripts]# cat test_ifvars.yaml
  2. ---
  3. - hosts: all
  4. gather_facts: True
  5. vars:
  6. - PORT: #置空
  7. tasks:
  8. - name: jinja2 if test
  9. template: src=/tmp/if.j2 dest=/root/test
  10. [root@localhost scripts]# ansible-playbook test_ifvars.yaml

十一,Playbook的notify通知和下发nginx配置

  1. #实战下发可执行动作的可变的nginx配置文件
  2. [root@localhost scripts]# head -1 /tmp/nginx.j2
  3. worker_processes {{ ansible_processor_count }}; #可变的参数
  4. [root@localhost scripts]# cat test_nginxvars.yaml
  5. ---
  6. - hosts: all
  7. gather_facts: True #开启系统内置变量
  8. tasks:
  9. - name: nginx conf
  10. template: src=/tmp/nginx.j2 dest=/usr/local/nginx/conf/nginx.conf
  11. notify:
  12. - reload nginx #下发通知给handlers模块执行名字叫做reload nginx的动作
  13. handlers: #定义动作
  14. - name: reload nginx #动作的名字
  15. shell: /usr/local/nginx/sbin/nginx -s reload
  16. [root@localhost scripts]# ansible-playbook test_nginxvars.yaml

十二,使用roles标准化Playbook

roles功能可以用来规范playbook的编写

12.1 创建所需要的roles原型目录结构

  1. #创建roles基本原型的目录结构
  2. [root@ansible myroles]# tree /myroles/
  3. /myroles/
  4. ├── nginx.yaml #入口触发配置文件
  5. └── roles #playbook的原型配置目录
  6. └── nginx #nginx相关模组配置目录
  7. ├── files #copy模块和script模块的参数src默认会从这个文件夹查找
  8. ├── handlers #用来存放notify的
  9. ├── tasks #用来存放ansible模块任务的
  10. ├── templates #用来存放j2的
  11. └── vars #用来存放变量的
  12. 7 directories, 1 file
  13. #入口触发配置文件
  14. [root@ansible myroles]# cat /myroles/nginx.yaml
  15. ---
  16. - hosts: all #执行的主机范围
  17. gather_facts: True #开启系统内置变量
  18. roles: #启用roles原型配置
  19. - nginx #执行nginx原型模组

12.2 roles中tasks任务编排模组的使用

  1. #在nginx模组添加tasks任务配置文件
  2. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  3. ---
  4. - name: check alived #任务1的名字
  5. ping: #执行ping模块
  6. - name: #任务2的名字
  7. shell: ls / #执行shell模块
  8. register: ls_result #将执行结果保存给变量
  9. - debug: var=ls_result #变量的值赋值给debug进行输出
  10. #完成后的目录结构如下所示
  11. [root@ansible myroles]# tree /myroles/
  12. /myroles/
  13. ├── nginx.yaml #nginx模组入口配置文件
  14. └── roles
  15. └── nginx #nginx原型模组目录
  16. ├── files
  17. ├── handlers
  18. ├── tasks
  19.    └── main.yaml #nginx模组的tasks任务配置文件
  20. ├── templates
  21. └── vars
  22. 7 directories, 2 files

12.3 执行简单的roles任务模型

  1. #执行nginx入口配置文件
  2. [root@ansible myroles]# ansible-playbook nginx.yaml
  3. PLAY [all] ****************************************************************************************************
  4. TASK [Gathering Facts] ****************************************************************************************
  5. ok: [webA]
  6. ok: [webB]
  7. TASK [nginx : check alived] ***********************************************************************************
  8. ok: [webA]
  9. ok: [webB]
  10. TASK [nginx : shell] ******************************************************************************************
  11. changed: [webA]
  12. changed: [webB]
  13. TASK [nginx : debug] ******************************************************************************************
  14. ok: [webA] => {
  15. "ls_result": {
  16. "changed": true,
  17. "cmd": "ls /",
  18. "delta": "0:00:00.002805",
  19. "end": "2018-06-21 11:52:29.343592",
  20. "failed": false,
  21. "rc": 0,
  22. "start": "2018-06-21 11:52:29.340787",
  23. "stderr": "",
  24. "stderr_lines": [],
  25. "stdout": "bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroo\nroot\nrun\nsbin\nservice\nsrv\nsys\ntmp\nusr\nvar",
  26. "stdout_lines": [
  27. "bin",
  28. "boot",
  29. "dev",
  30. "etc",
  31. "home",
  32. "lib",
  33. "lib64",
  34. "media",
  35. "mnt",
  36. "opt",
  37. "proc",
  38. "roo",
  39. "root",
  40. "run",
  41. "sbin",
  42. "service",
  43. "srv",
  44. "sys",
  45. "tmp",
  46. "usr",
  47. "var"
  48. ]
  49. }
  50. }
  51. ok: [webB] => {
  52. "ls_result": {
  53. "changed": true,
  54. "cmd": "ls /",
  55. "delta": "0:00:00.002708",
  56. "end": "2018-06-21 11:52:29.359754",
  57. "failed": false,
  58. "rc": 0,
  59. "start": "2018-06-21 11:52:29.357046",
  60. "stderr": "",
  61. "stderr_lines": [],
  62. "stdout": "bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroo\nroot\nrun\nsbin\nservice\nsrv\nsys\ntmp\nusr\nvar",
  63. "stdout_lines": [
  64. "bin",
  65. "boot",
  66. "dev",
  67. "etc",
  68. "home",
  69. "lib",
  70. "lib64",
  71. "media",
  72. "mnt",
  73. "opt",
  74. "proc",
  75. "roo",
  76. "root",
  77. "run",
  78. "sbin",
  79. "service",
  80. "srv",
  81. "sys",
  82. "tmp",
  83. "usr",
  84. "var"
  85. ]
  86. }
  87. }
  88. PLAY RECAP ****************************************************************************************************
  89. webA : ok=4 changed=1 unreachable=0 failed=0
  90. webB : ok=4 changed=1 unreachable=0 failed=0
  • ansible-playbook执行入口配置文件nginx.yaml后,它会自动在roles目录下查找nginx目录并进入后查找tasks任务目录并执行main.yaml的任务配置文件。
  • 其实,这个roles的操作等效于以下配置
  1. #本配置和之前的roles配置等效
  2. [root@ansible myroles]# cat /service/scripts/test.yaml
  3. ---
  4. - hosts: all
  5. gather_facts: True
  6. tasks: #其实roles的本质就是将tasks任务单独写了。
  7. - name: check alived #并在入口文件里追加了roles去查找tasks配置文件路径
  8. ping:
  9. - name:
  10. shell: ls /
  11. register: ls_result
  12. - debug: var=ls_result

12.4 roles中vars自定义变量模组的使用

  1. #创建自定义变量vars模组的配置文件
  2. [root@ansible myroles]# cat roles/nginx/vars/main.yaml
  3. ---
  4. my_name: yunjisuan
  5. phone: 1800000000
  6. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  7. ---
  8. - name: check alived
  9. ping:
  10. - name:
  11. shell: ls /
  12. register: ls_result
  13. - debug: var=ls_result
  14. - name: #添加对变量引用的任务编排
  15. shell: echo my phone is {{ phone }}
  16. register: echo_result
  17. - debug: var=echo_result
  18. [root@ansible myroles]# ansible-playbook nginx.yaml #执行入口配置文件

12.5 使用copy,script模块的标准化

roles模型里使用copy,script模块,默认从roles/nginx/files这里面找

  1. [root@ansible myroles]# cat roles/nginx/files/test
  2. welcome to yunjisuan
  3. [root@ansible myroles]# cat roles/nginx/files/test.sh
  4. echo "aaa" >> /tmp/test
  5. [root@ansible myroles]# chmod +x roles/nginx/files/test.sh
  6. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  7. ---
  8. - name: check alived
  9. ping:
  10. - name:
  11. shell: ls /
  12. register: ls_result
  13. - debug: var=ls_result
  14. - name:
  15. shell: echo my phone is {{ phone }}
  16. register: echo_result
  17. - debug: var=echo_result
  18. - name: #添加copy模块
  19. copy: src=test dest=/root/
  20. - name: #添加script模块(自动在目标IP机器上执行脚本)
  21. script: test.sh
  22. [root@ansible myroles]# ansible-playbook nginx.yaml

12.6 roles中template模块的使用

roles模型里使用template模块,默认从roles/nginx/template里面找

  1. [root@ansible myroles]# cat roles/nginx/templates/test.j2
  2. myname is {{ my_name }},my phone is {{ phone }} #引用自定义变量
  3. my ipaddress is {{ansible_all_ipv4_addresses[0]}} #引用内置变量
  4. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  5. ---
  6. - name: check alived
  7. ping:
  8. - name:
  9. shell: ls /
  10. register: ls_result
  11. - debug: var=ls_result
  12. - name:
  13. shell: echo my phone is {{ phone }}
  14. register: echo_result
  15. - debug: var=echo_result
  16. - name:
  17. copy: src=test dest=/root/
  18. - name:
  19. script: test.sh
  20. - name:
  21. template: src=test.j2 dest=/root/test2 #下发可变配置文件
  22. [root@ansible myroles]# ansible-playbook nginx.yaml

12.7 roles中notify模块的使用

roles使用notify模块,默认从roles/nginx/handles里面找

  1. [root@ansible myroles]# cat roles/nginx/handlers/main.yaml
  2. ---
  3. - name: start_nginx #定义handlers的动作类型
  4. shell: /usr/local/nginx/sbin/nginx
  5. - name: stop_nginx #定义handlers的动作类型
  6. shell: /usr/local/nginx/sbin/nginx -s stop
  7. - name: reload_nginx #定义handlers的动作类型
  8. shell: /usr/local/nginx/sbin/nginx -s reload
  9. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  10. ---
  11. - name: check alived
  12. ping:
  13. - name:
  14. shell: ls /
  15. register: ls_result
  16. - debug: var=ls_result
  17. - name:
  18. shell: echo my phone is {{ phone }}
  19. register: echo_result
  20. - debug: var=echo_result
  21. - name:
  22. copy: src=test dest=/root/
  23. - name:
  24. script: test.sh
  25. - name:
  26. template: src=test.j2 dest=/root/test2
  27. notify: start_nginx #执行template任务后下发通知给handlers执行start_nginx
  28. [root@ansible myroles]# ansible-playbook nginx.yaml

特别提示:
notify下发通知只有当之前的任务造成了变化那么才会被执行,如果没有发生任何改变,则notify不会被执行。例如:

    1. #tasks任务造成改变,触发notify
    2. [root@ansible myroles]# cat /tmp/test.yaml
    3. ---
    4. - hosts: webA
    5. gather_facts: True
    6. tasks:
    7. - name:
    8. copy: src=/tmp/test dest=/root/ #这步造成目标改变才能出发notify
    9. notify: start_nginx
    10. handlers:
    11. - name: start_nginx
    12. shell: /usr/local/nginx/sbin/nginx
    13. [root@ansible myroles]# ansible-playbook /tmp/test.yaml
    14. PLAY [webA] ***************************************************************************************************
    15. TASK [Gathering Facts] ****************************************************************************************
    16. ok: [webA]
    17. TASK [copy] ***************************************************************************************************
    18. changed: [webA] #发生了改变
    19. RUNNING HANDLER [start_nginx] #触发notify *********************************************************************************
    20. changed: [webA]
    21. PLAY RECAP ****************************************************************************************************
    22. webA : ok=3 changed=2 unreachable=0 failed=0
    23. #我们再次执行/tmp/test.yaml
    24. [root@ansible myroles]# ansible-playbook /tmp/test.yaml
    25. PLAY [webA] ***************************************************************************************************
    26. TASK [Gathering Facts] ****************************************************************************************
    27. ok: [webA]
    28. TASK [copy] ***************************************************************************************************
    29. ok: [webA] #没有造成任务改变,未触发notify通知
    30. PLAY RECAP ****************************************************************************************************
    31. webA : ok=2 changed=0 unreachable=0 failed=0
posted @ 2021-09-15 22:10  你好我叫阿成  阅读(277)  评论(0)    收藏  举报