• Linux之ansible(playbook)超详解


    目录

    1.给定数据如下: 使用loop来输出My name is zhangsan/lisi My age is 18/20

    给定数据Services,要求使用loop来重启服务:提示:将services定义为变量         可以使用lookup('dict', services)来进行转换或者使用{{ services | dict2items }}

    2.使用when,当条件成立时才执行任务:        测试给定一个0/1

    给定一个未定义的变量

    给定一个变量当变量>10时才执行

    使用and 和 or来连接两个条件: True and False , True or False

    loop和when联合使用 1 中,当name == firewalld时不执行任务

    3.notify和handler的使用定义一个任务:使用shell模块执行 echo "123", 使用notify通知handler 任务 debug info

    定义handler: 包含一个任务:debug info: 执行 输出: I handled the notify

    4.tags使用:定义三个任务:分别打上标签:tag1, tag2, tag2执行playbook, 且指定只执行tag2

    5.处理任务失败:    ignore_errors的使用: 定义任务使用command模块执行 

    再定义一个任务:使用debug模块输出: This is test for ignore errors(确保这个任务可以正常执行)

    failed_when: 定义一个任务: 使用shell模块执行echo 123, 将此任务设置为执行失败

    changed_when: 定义一个任务: 使用shell模块执行echo 123 > /root/changed_test, 将此任务的changed状态改为0

    block, rescue, always: 在block定义两个任务,在rescue中定义两个任务,在always中定义两个任务,去执行

    让rescue中的任务可以执行

    6.forks和serial的区别

    Forks

    Serial

    7.导入playbook和task

    建立一个import_playbook.yml 然后将其导入另一个playbook: main_playbook.yml 

    建立一个import_task.yml里面只写任务:将其导入main_task_playbook.yml中


    1.给定数据如下: 使用loop来输出My name is zhangsan/lisi My age is 18/20

      users:
         - name: zhangsan
           age: 18
         - name: lisi
            age: 20

    1. [root@rhcsa ~]# vim playbook1.yml
    2. - name:
    3. hosts: rhce
    4. tasks:
    5. - name:
    6. shell: "{{ item }}"
    7. loop:
    8. - name: zhangsan
    9. age: 18
    10. - name: lisi
    11. age: 20
    1. [root@rhcsa ~]# ansible-playbook -C playbook1.yml
    2. PLAY [rhce] ********************************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [shell] *******************************************************************
    6. skipping: [rhce] => (item={'name': 'zhangsan', 'age': 18})
    7. skipping: [rhce] => (item={'name': 'lisi', 'age': 20})
    8. PLAY RECAP *********************************************************************
    9. rhce : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

    给定数据Services,要求使用loop来重启服务:
    提示:将services定义为变量
             可以使用lookup('dict', services)来进行转换或者使用{{ services | dict2items }}

    services: 
           httpd:
               name: httpd
               state: restarted
           firewalld: 
             name: firewalld
             state: restarted 

    1. [root@rhcsa ~]# vim playbook2.yml
    2. - name:
    3. hosts: rhce
    4. tasks:
    5. - name:
    6. service:
    7. name: "{{ item.name }}"
    8. state: "{{ item.state}}"
    9. loop:
    10. - name: httpd
    11. state: restarted
    12. - name: firewalld
    13. state: restarted
    1. [root@rhcsa ~]# ansible-playbook -C playbook2.yml
    2. PLAY [rhce] ********************************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [service] *****************************************************************
    6. changed: [rhce] => (item={'name': 'httpd', 'state': 'restarted'})
    7. changed: [rhce] => (item={'name': 'firewalld', 'state': 'restarted'})
    8. PLAY RECAP *********************************************************************
    9. rhce : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    2.使用when,当条件成立时才执行任务:
            测试给定一个0/1

    1. #给1
    2. - name: Simple Boolean task Demo
    3. hosts: rhce
    4. vars:
    5. run_my_task: 1
    6. tasks:
    7. - name: httpd package is installed
    8. yum:
    9. name: httpd
    10. when: run_my_task
    11. #给0
    12. - name: Simple Boolean task Demo
    13. hosts: rhce
    14. vars:
    15. run_my_task: 0
    16. tasks:
    17. - name: httpd package is installed
    18. yum:
    19. name: httpd
    20. when: run_my_task
    1. [root@rhcsa ~]# ansible-playbook -C playbook3.yml
    2. PLAY [Simple Boolean task Demo] ************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [httpd package is installed] **********************************************
    6. ok: [rhce]
    7. PLAY RECAP *********************************************************************
    8. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    9. [root@rhcsa ~]# vim playbook3.yml
    10. [root@rhcsa ~]# ansible-playbook -C playbook3.yml
    11. PLAY [Simple Boolean task Demo] ************************************************
    12. TASK [Gathering Facts] *********************************************************
    13. ok: [rhce]
    14. TASK [httpd package is installed] **********************************************
    15. skipping: [rhce]
    16. PLAY RECAP *********************************************************************
    17. rhce : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

    给定一个未定义的变量

    1. - name: Simple Boolean task Demo
    2. hosts: rhce
    3. #vars:
    4. # run_my_task:
    5. tasks:
    6. - name: httpd package is installed
    7. yum:
    8. name: httpd
    9. when: run_my_task
    1. [root@rhcsa ~]# ansible-playbook -C playbook3.yml
    2. PLAY [Simple Boolean task Demo] ************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [httpd package is installed] **********************************************
    6. fatal: [rhce]: FAILED! => {"msg": "The conditional check 'run_my_task' failed. The error was: error while evaluating conditional (run_my_task): 'run_my_task' is undefined\n\nThe error appears to be in '/root/playbook3.yml': line 7, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: httpd package is installed\n ^ here\n"}
    7. PLAY RECAP *********************************************************************
    8. rhce : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

    给定一个变量当变量>10时才执行

    1. [root@rhcsa ~]# vim playbook3.yml
    2. #给10
    3. - name: Simple Boolean task Demo
    4. hosts: rhce
    5. vars:
    6. run_my_task: 10
    7. tasks:
    8. - name: httpd package is installed
    9. yum:
    10. name: httpd
    11. when: run_my_task > 10
    12. #给11
    13. - name: Simple Boolean task Demo
    14. hosts: rhce
    15. vars:
    16. run_my_task: 11
    17. tasks:
    18. - name: httpd package is installed
    19. yum:
    20. name: httpd
    21. when: run_my_task > 10
    1. [root@rhcsa ~]# ansible-playbook -C playbook3.yml
    2. PLAY [Simple Boolean task Demo] ************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [httpd package is installed] **********************************************
    6. skipping: [rhce]
    7. PLAY RECAP *********************************************************************
    8. rhce : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
    9. [root@rhcsa ~]# vim playbook3.yml
    10. [root@rhcsa ~]# ansible-playbook -C playbook3.yml
    11. PLAY [Simple Boolean task Demo] ************************************************
    12. TASK [Gathering Facts] *********************************************************
    13. ok: [rhce]
    14. TASK [httpd package is installed] **********************************************
    15. ok: [rhce]
    16. PLAY RECAP *********************************************************************
    17. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    使用and 和 or来连接两个条件: True and False , True or False

    1. #and
    2. - name:
    3. hosts: rhce
    4. vars:
    5. run_my_task: qwe
    6. test: 121
    7. tasks:
    8. - name:
    9. yum:
    10. name: httpd
    11. when: run_my_task == 'qwe' and test == '123'
    12. #or
    13. - name:
    14. hosts: rhce
    15. vars:
    16. run_my_task: qwe
    17. test: 121
    18. tasks:
    19. - name:
    20. yum:
    21. name: httpd
    22. when: run_my_task == 'qwe' or test == '123'
    1. #and
    2. [root@rhcsa ~]# ansible-playbook -C playbook3.yml
    3. PLAY [rhce] ********************************************************************
    4. TASK [Gathering Facts] *********************************************************
    5. ok: [rhce]
    6. TASK [yum] *********************************************************************
    7. skipping: [rhce]
    8. PLAY RECAP *********************************************************************
    9. rhce : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
    10. #or
    11. [root@rhcsa ~]# ansible-playbook -C playbook3.yml
    12. PLAY [rhce] ********************************************************************
    13. TASK [Gathering Facts] *********************************************************
    14. ok: [rhce]
    15. TASK [yum] *********************************************************************
    16. ok: [rhce]
    17. PLAY RECAP *********************************************************************
    18. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    loop和when联合使用 1 中,当name == firewalld时不执行任务

    1. - name:
    2. hosts: rhce
    3. tasks:
    4. - name:
    5. service:
    6. name: "{{ item.name }}"
    7. state: stopped
    8. loop:
    9. - name: httpd
    10. - name: firewalld
    11. when: item.name == 'httpd'
    1. [root@rhcsa ~]# ansible-playbook -C playbook3.yml
    2. PLAY [rhce] ********************************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [service] *****************************************************************
    6. ok: [rhce] => (item={'name': 'httpd'})
    7. skipping: [rhce] => (item={'name': 'firewalld'})
    8. PLAY RECAP *********************************************************************
    9. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    3.notify和handler的使用
    定义一个任务:使用shell模块执行 echo "123", 使用notify通知handler 任务 debug info

    1. - name:
    2. hosts: rhce
    3. tasks:
    4. - name:
    5. debug:
    6. notify:
    7. - debug info
    8. handlers:
    9. - name: debug info
    10. shell: echo"123"
    1. [root@rhcsa ~]# ansible-playbook -C playbook4.yml
    2. PLAY [rhce] ********************************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [debug] *******************************************************************
    6. ok: [rhce] => {
    7. "msg": "Hello world!"
    8. }
    9. PLAY RECAP *********************************************************************
    10. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    定义handler: 包含一个任务:debug info: 执行 输出: I handled the notify

    1. - name:
    2. hosts: rhce
    3. tasks:
    4. - name:
    5. debug:
    6. msg: I handled the notify
    7. notify:
    8. - debug info
    9. handlers:
    10. - name: debug info
    11. shell: echo"123"
    1. [root@rhcsa ~]# ansible-playbook -C playbook4.yml
    2. PLAY [rhce] ********************************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [debug] *******************************************************************
    6. ok: [rhce] => {
    7. "msg": "I handled the notify"
    8. }
    9. PLAY RECAP *********************************************************************
    10. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    4.tags使用:定义三个任务:分别打上标签:tag1, tag2, tag2
    执行playbook, 且指定只执行tag2

    1. - hosts: cache
    2. remote_user: root
    3. tasks:
    4. - name: copy
    5. copy: content="apple" dest=/tmp/mama.txt
    6. tags: tag1 # 标签名是 tag1,在下面执行文件时会用到
    7. - name: copy
    8. copy: content="banana" dest=/tmp/mama.txt
    9. tags: tag2 # 标签名是 tag2,在下面执行文件时会用到- name: copy
    10. copy: content="egg" dest=/tmp/mama.txt
    11. tags: tag3 # 标签名是 tag3,在下面执行文件时会用到
    #ansible-playbook -t tag1 pbook.yml #执行文件中第二条 tag1 命令

    5.处理任务失败:
        ignore_errors的使用: 定义任务使用command模块执行 

    1. - name:
    2. hosts: rhce
    3. tasks:
    4. - name:
    5. command:
    6. ignore_errors: yes
    7. - name:
    8. service:
    9. name: httpd
    10. state: started
    1. [root@rhcsa ~]# ansible-playbook -C playbook5.yml
    2. PLAY [rhce] ********************************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [command] *****************************************************************
    6. fatal: [rhce]: FAILED! => {"changed": false, "cmd": null, "delta": null, "end": null, "msg": "no command given", "rc": 256, "start": null, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
    7. ...ignoring
    8. TASK [service] *****************************************************************
    9. changed: [rhce]
    10. PLAY RECAP *********************************************************************
    11. rhce : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1

    再定义一个任务:使用debug模块输出: This is test for ignore errors(确保这个任务可以正常执行)

    1. - name:
    2. hosts: rhce
    3. tasks:
    4. - name:
    5. command:
    6. ignore_errors: yes
    7. - name:
    8. debug:
    9. msg: This is test for ignore errors
    1. [root@rhcsa ~]# ansible-playbook -C playbook6.yml
    2. PLAY [rhce] ********************************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [command] *****************************************************************
    6. fatal: [rhce]: FAILED! => {"changed": false, "cmd": null, "delta": null, "end": null, "msg": "no command given", "rc": 256, "start": null, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
    7. ...ignoring
    8. TASK [debug] *******************************************************************
    9. ok: [rhce] => {
    10. "msg": "This is test for ignore errors"
    11. }
    12. PLAY RECAP *********************************************************************
    13. rhce : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1

    failed_when: 定义一个任务: 使用shell模块执行echo 123, 将此任务设置为执行失败

    1. - name:
    2. remote_user: root
    3. hosts: rhce
    4. tasks:
    5. - name:
    6. shell: echo "123"
    7. register: command_result
    8. failed_when: "'123' in command_result.stdout"
    1. [root@rhcsa ~]# ansible-playbook playbook6.yml
    2. PLAY [rhce] ********************************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [shell] *******************************************************************
    6. fatal: [rhce]: FAILED! => {"changed": true, "cmd": "echo \"123\"", "delta": "0:00:00.003329", "end": "2022-08-09 11:36:31.884746", "failed_when_result": true, "msg": "", "rc": 0, "start": "2022-08-09 11:36:31.881417", "stderr": "", "stderr_lines": [], "stdout": "123", "stdout_lines": ["123"]}
    7. PLAY RECAP *********************************************************************
    8. rhce : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

    changed_when: 定义一个任务: 使用shell模块执行echo 123 > /root/changed_test, 将此任务的changed状态改为0

    1. - name:
    2. hosts: rhce
    3. tasks:
    4. - name:
    5. shell: echo "123" > /root/changed_test
    6. changed_when: false
    1. [root@rhcsa ~]# ansible-playbook playbook7.yml
    2. PLAY [rhce] ********************************************************************
    3. TASK [Gathering Facts] *********************************************************
    4. ok: [rhce]
    5. TASK [shell] *******************************************************************
    6. ok: [rhce]
    7. PLAY RECAP *********************************************************************
    8. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    block, rescue, always: 在block定义两个任务,在rescue中定义两个任务,在always中定义两个任务,去执行

    • block: 定义要运行的主要的任务。
    • rescue: 定义要在 block 子句中定义的任务失败时运行的任务。
    • always:定义始终都独立运行的任务,不论 block 和 rescue 子句中定义的任务
    是否成功还是失败。

    1. [root@rhcsa ~]# vim playbook1.yml
    2. msg: "This is block1"
    3. - name:
    4. debug:
    5. msg: "This is block2"
    6. rescue:
    7. - name:
    8. debug:
    9. msg: "This is rescue1"
    10. - name:
    11. debug:
    12. msg: "This is rescue2"
    13. always:
    14. - name:
    15. debug:
    16. msg: "This is always1"
    17. - name:
    18. debug:
    19. msg: "This is always2"
    1. [root@rhcsa ~]# ansible-playbook playbook1.yml
    2. PLAY [rhce] **********************************************************************
    3. TASK [Gathering Facts] ***********************************************************
    4. ok: [rhce]
    5. TASK [debug] *********************************************************************
    6. ok: [rhce] => {
    7. "msg": "This is block1"
    8. }
    9. TASK [debug] *********************************************************************
    10. ok: [rhce] => {
    11. "msg": "This is block2"
    12. }
    13. TASK [debug] *********************************************************************
    14. ok: [rhce] => {
    15. "msg": "This is always1"
    16. }
    17. TASK [debug] *********************************************************************
    18. ok: [rhce] => {
    19. "msg": "This is always2"
    20. }
    21. PLAY RECAP ***********************************************************************
    22. rhce : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    让rescue中的任务可以执行

    1. [root@rhcsa ~]# vim playbook1.yml
    2. - name:
    3. hosts: rhce
    4. tasks:
    5. - name:
    6. block:
    7. - name:
    8. debug:
    9. msg: "This is block1"
    10. failed_when: 'This is block1'
    11. - name:
    12. debug:
    13. msg: "This is block2"
    14. failed_when: 'This is block2'
    15. rescue:
    16. - name:
    17. debug:
    18. msg: "This is rescue1"
    19. - name:
    20. debug:
    21. msg: "This is rescue2"
    1. [root@rhcsa ~]# ansible-playbook playbook1.yml
    2. PLAY [rhce] **********************************************************************
    3. TASK [Gathering Facts] ***********************************************************
    4. ok: [rhce]
    5. TASK [debug] *********************************************************************
    6. fatal: [rhce]: FAILED! => {
    7. "msg": "This is block1"
    8. }
    9. TASK [debug] *********************************************************************
    10. ok: [rhce] => {
    11. "msg": "This is rescue1"
    12. }
    13. TASK [debug] *********************************************************************
    14. ok: [rhce] => {
    15. "msg": "This is rescue2"
    16. }
    17. TASK [debug] *********************************************************************
    18. ok: [rhce] => {
    19. "msg": "This is always1"
    20. }
    21. TASK [debug] *********************************************************************
    22. ok: [rhce] => {
    23. "msg": "This is always2"
    24. }
    25. PLAY RECAP ***********************************************************************
    26. rhce : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0

    6.forks和serial的区别

    Forks

    forks用来设置同一时刻与目的主机连接数,也可以理解为主机并行数,默认值比较保守为5。在生产中,多数情况下我们会更改这个参数。如果控制节点的CPU和网络性能够用,设置几十上百个也是可以的。

    ansible.cfg设置forks的全局默认值:

    1. # ansible.cfg
    2. [defaults]
    3. forks = 15

    命令行设置forks的数量,即在执行playbook时,通过「--forks」或「-f」指定: 

    lab-ansible ansible-playbook playbooks/test_forks.yaml --fork 10

    Serial

    serial用于控制一个play内的主机并行数,这个并行数不能超过forks,超过后则serial不会生效。

    定义方法如下:

    1. --
    2. - hosts: nodes
    3. serial: 2
    4. tasks:

    本质上,serial作用范围是一个play,受限于forks,但比forks控制的更加细节。假如我们的forks设置为100,但是想让某个play里的所有任务并行数为50的执行,此时我们应该想到serial这个调优方法。

    7.导入playbook和task

    建立一个import_playbook.yml 然后将其导入另一个playbook: main_playbook.yml 

    1. [root@rhcsa ~]# vim import_playbook.yml
    2. ---
    3. - name: this is import_play
    4. hosts: rhce
    5. tasks:
    6. - name: stopped httpd
    7. service:
    8. name: httpd
    9. state: stopped
    10. #####################################
    11. [root@rhcsa ~]# vim main_playbook.yml
    12. - name: import
    13. import_playbook: import_playbook.yml
    14. - name: this is main_play
    15. hosts: all
    16. tasks:
    17. - name: start firewalld
    18. service:
    19. name: firewalld
    20. state: stopped
    21. enabled: no
    1. [root@rhcsa ~]# ansible-playbook main_playbook.yml -C
    2. PLAY [this is import_play] *********************************************************
    3. TASK [Gathering Facts] *************************************************************
    4. ok: [rhce]
    5. TASK [stopped httpd] ***************************************************************
    6. ok: [rhce]
    7. PLAY [this is main_play] ***********************************************************
    8. TASK [Gathering Facts] *************************************************************
    9. ok: [rhel]
    10. ok: [rhce]
    11. TASK [start firewalld] *************************************************************
    12. ok: [rhel]
    13. ok: [rhce]
    14. PLAY RECAP *************************************************************************
    15. rhce : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    16. rhel : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    建立一个import_task.yml里面只写任务:将其导入main_task_playbook.yml中

    1. [root@rhcsa ~]# vim import_task.yml
    2. - name: this is import_play
    3. hosts: rhce
    4. tasks:
    5. - name: stopped httpd
    6. service:
    7. name: httpd
    8. state: stopped
    9. ######################################
    10. [root@rhcsa ~]# vim main_task_playbook.yml
    11. - name: import
    12. import_playbook: import_task.yml
    13. - name: this is main_task_play
    14. hosts: rhce
    15. tasks:
    16. - name: start firewalld
    17. service:
    18. name: firewalld
    19. state: stopped
    20. enabled: no
    1. [root@rhcsa ~]# ansible-playbook main_task_playbook.yml -C
    2. PLAY [this is import_play] *********************************************************
    3. TASK [Gathering Facts] *************************************************************
    4. ok: [rhce]
    5. TASK [stopped httpd] ***************************************************************
    6. ok: [rhce]
    7. PLAY [this is main_task_play] ******************************************************
    8. TASK [Gathering Facts] *************************************************************
    9. ok: [rhce]
    10. TASK [start firewalld] *************************************************************
    11. ok: [rhce]
    12. PLAY RECAP *************************************************************************
    13. rhce : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  • 相关阅读:
    【PAT甲级 - C++题解】1088 Rational Arithmetic
    「项目管理」甘特图制定项目计划的方法
    JS学习762~780(注册事件+删除事件+DOM事件流+事件对象+阻止事件冒泡+事件委托鼠标事件+键盘事件)
    【Python】《Python编程:从入门到实践 (第2版) 》笔记-Chapter2-变量和简单数据类型
    基于Springboot实现校园新闻网站管理系统演示【项目源码+论文说明】分享
    关于wParam和lParam
    华为云云耀云服务器L实例评测|华为云上安装etcd
    Java类加载器详解
    [yolo系列:yolov7添加可变形卷积Deformable Conv V2]
    mysql数据增删改
  • 原文地址:https://blog.csdn.net/weixin_64051859/article/details/126231537