Ansible运维自动化

Ansible运维自动化

一、Ansible-playbook的初步使用

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

ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin/

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   #webBhost参数的值,值和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 

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

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

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的自定义变量和内置变量

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

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

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]# ansible-playbook test_setupvars.yaml
  11. PLAY [all] *********************************************************************************************************
  12. TASK [Gathering Facts] *********************************************************************************************
  13. ok: [webA]
  14. ok: [webB]
  15. TASK [setup var] ***************************************************************************************************
  16. changed: [webA]
  17. changed: [webB]
  18. TASK [debug] *******************************************************************************************************
  19. ok: [webA] => {
  20. "var_result": {
  21. "changed": true,
  22. "cmd": "echo "ip 192.168.200.132 cpu 1"",
  23. "delta": "0:00:00.002408",
  24. "end": "2018-06-19 11:32:44.540658",
  25. "failed": false,
  26. "rc": 0,
  27. "start": "2018-06-19 11:32:44.538250",
  28. "stderr": "",
  29. "stderr_lines": [],
  30. "stdout": "ip 192.168.200.132 cpu 1",
  31. "stdout_lines": [
  32. "ip 192.168.200.132 cpu 1"
  33. ]
  34. }
  35. }
  36. ok: [webB] => {
  37. "var_result": {
  38. "changed": true,
  39. "cmd": "echo "ip 192.168.200.138 cpu 1"",
  40. "delta": "0:00:00.002102",
  41. "end": "2018-06-19 11:32:44.526875",
  42. "failed": false,
  43. "rc": 0,
  44. "start": "2018-06-19 11:32:44.524773",
  45. "stderr": "",
  46. "stderr_lines": [],
  47. "stdout": "ip 192.168.200.138 cpu 1",
  48. "stdout_lines": [
  49. "ip 192.168.200.138 cpu 1"
  50. ]
  51. }
  52. }
  53. PLAY RECAP *********************************************************************************************************
  54. webA                       : ok=3    changed=1    unreachable=0    failed=0   
  55. 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模块。

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

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
原文地址:https://www.cnblogs.com/heroke/p/9999299.html