福福不服 福福不服
深圳 --°
正在加载音乐 请稍候

Ansible剧本流程控制

handlers触发器

notify:监控上面的模块执行是否发生变化

chufa:自定义的任务名称,自动去handlers里面匹配相对应的任务名称执行动作

下方是copy了/data/copy/下的文件到server2组里的主机/data/monster/handlers/下,notify监控发生了变化后触发chufa任务。

- hosts: server2
  gather_facts: no
  tasks:
    - name: handlers触发器demo
      copy:
        src: /data/copy/
        dest: /data/monster/handlers/
      notify:
        - chufa
  handlers:
    - name: chufa
      shell: 'date >> /data/monster/handlers/time.log'
[root@monster2 handlers]# cat /data/monster/handlers/time.log 
Wed Dec 28 17:20:15 EST 2022
[root@monster2 handlers]# 

when条件

下面剧本是所有主机里面,主机名叫monster2或monster3的就删除handlers目录

gather_facts需要设置开启或者直接不写这一行默认是开启,因为需要使用内置变量ansible_hostname验证主机名

如果只判断一台主机可以直接ansible_hostname == “monster2”

- hosts: all
  gather_facts: yes
  tasks:
    - name: when使用
      shell: rm -rf /data/monster/handlers
      when: (ansible_hostname is match ("monster2|monster3"))

警告无视就行,它建议不要直接使用rm删除,通过文件模块用state=absent的方式
下面可以看见,138(mosnter2),139(mosnter3)两台主机都执行成功了,但是只有只有138有修改状态,因为monster3上面没有这个目录,skipped跳过。

with_items循环

删除所有/data/monster/下的monster.log 和monster.yml文件,变量名默认item没有s,with_items里面的变量全匹配后任务才执行完

- hosts: all
  gather_facts: no
  tasks:
    - name: with_items使用
      shell: rm -rf /data/monster/{{item}}
      with_items:
        - monster.log
        - monster.yml

with_items多变量循环

同样with_items里面的变量全匹配后任务才执行完
使用键值对key:value的形式
touch:创建,absent:删除

- hosts: all
  gather_facts: no
  tasks:
    - name: 创建目录with
      file:
        path: /data/monster/with
        state: directory
    - name: 循环创建文件monster1-5,最后再删掉文件2。
      file:
        path: /data/monster/with/{{item.mulu}}
        state: "{{item.wenjian}}"
      with_items:
        - { mulu: 'monster1', wenjian: 'touch' }
        - { mulu: 'monster2', wenjian: 'touch' }
        - { mulu: 'monster3', wenjian: 'touch' }
        - { mulu: 'monster4', wenjian: 'touch' }
        - { mulu: 'monster5', wenjian: 'touch' }
        - { mulu: 'monster2', wenjian: 'absent' }

分发主机查看with目录结构,monster2最后被删了,只有1345

[root@monster2 with]# tree
.
├── monster1
├── monster3
├── monster4
└── monster5

[root@mosnter3 with]# tree
.
├── monster1
├── monster3
├── monster4
└── monster5

tags标签

写法和上面一样,和模块同级对其,使用 - 方式设置标签
可打多个标签,也可以重复标签

- hosts: server2
  gather_facts: no
  tasks:
    - name: 分发到monster2主机
      copy:
        src: /data/www/monster.cn/v0.0.10
        dest: /data/www/monster.cn/
        backup: yes
      tags:
        - v0.0.10
- hosts: server3
  gather_facts: no
  tasks:
    - name: 分发到monster2主机
      copy:
        src: /data/www/monster.cn/v0.0.9
        dest: /data/www/monster.cn/
        backup: yes
      tags:
        - v0.0.9

单独安标签执行剧本
-t 指定标签 剧本文件

[root@monster1 ~]# ansible-playbook -t v0.0.10 juben.yml 

查看目录结构

[root@monster2 www]# tree
.
└── monster.cn
    └── v0.0.10
        └── index.html

2 directories, 1 file

指定多个标签,标签之间用逗号隔开

[root@monster1 ~]# ansible-playbook -t v0.0.10,v0.0.9 juben.yml 

刚才v0.0.10执行过了,138未做更改
如果目录没有修改,只修改里面的文件内容同样会识别到,重新分发

查看目录结构

[root@mosnter3 www]# tree
.
└── monster.cn
    └── v0.0.9
        └── index.html

2 directories, 1 file
[root@mosnter3 www]# 

ignore_errors忽略错误

忽略错误使用:同样在模块同级对其,ignore_errors:yes
如下在第一个任务创建目录的时候故意弄错,先把忽略错误注释掉,执行看看

- hosts: server2
  gather_facts: no
  tasks:
    - name: monster2故意创建错的目录
      file:
        path: /data/monster/error.log/
        state: touch
      tags:
          - test_error
#      ignore_errors: yes
- hosts: server3
  gather_facts: no
  tasks:
    - name: 忽略monster2错误会执行此任务
      file:
        path: /data/monster/error.log
        state: touch
      tags:
        - test_error

提示致命错误,然后就结束剧本了

把注释去掉后,仍能提示任务1的致命错误,但是紧接着后面…ignoring(忽略),然后任务2的接着执行了

[root@monster1 ~]# ansible-playbook -t test_error juben.yml 
 ________________
< PLAY [server2] >
 ----------------
      \                    / \  //\
       \    |\___/|      /   \//  \\
            /0  0  \__  /    //  | \ \    
           /     /  \/_/    //   |  \  \  
           @_^_@'/   \/_   //    |   \   \ 
           //_^_/     \/_ //     |    \    \
        ( //) |        \///      |     \     \
      ( / /) _|_ /   )  //       |      \     _\
    ( // /) '/,_ _ _/  ( ; -.    |    _ _\.-~        .-~~~^-.
  (( / / )) ,-{        _      `-.|.-~-.           .~         `.
 (( // / ))  '/\      /                 ~-. _ .-~      .-~^-.  \
 (( /// ))      `.   {            }                   /      \  \
  (( / ))     .----~-.\        \-'                 .~         \  `. \^-.
             ///.----..>        \             _ -~             `.  ^-`  ^-_
               ///-._ _ _ _ _ _ _}^ - - - - ~                     ~-- ,.-~
                                                                  /.-~

 _________________________
< TASK [monster2故意创建错的目录] >
 -------------------------
      \                    / \  //\
       \    |\___/|      /   \//  \\
            /0  0  \__  /    //  | \ \    
           /     /  \/_/    //   |  \  \  
           @_^_@'/   \/_   //    |   \   \ 
           //_^_/     \/_ //     |    \    \
        ( //) |        \///      |     \     \
      ( / /) _|_ /   )  //       |      \     _\
    ( // /) '/,_ _ _/  ( ; -.    |    _ _\.-~        .-~~~^-.
  (( / / )) ,-{        _      `-.|.-~-.           .~         `.
 (( // / ))  '/\      /                 ~-. _ .-~      .-~^-.  \
 (( /// ))      `.   {            }                   /      \  \
  (( / ))     .----~-.\        \-'                 .~         \  `. \^-.
             ///.----..>        \             _ -~             `.  ^-`  ^-_
               ///-._ _ _ _ _ _ _}^ - - - - ~                     ~-- ,.-~
                                                                  /.-~

fatal: [192.168.71.138]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Error, could not touch target: [Errno 21] Is a directory: '/data/monster/error.log/'", "path": "/data/monster/error.log/"}
...ignoring
 ________________
< PLAY [server3] >
 ----------------
      \                    / \  //\
       \    |\___/|      /   \//  \\
            /0  0  \__  /    //  | \ \    
           /     /  \/_/    //   |  \  \  
           @_^_@'/   \/_   //    |   \   \ 
           //_^_/     \/_ //     |    \    \
        ( //) |        \///      |     \     \
      ( / /) _|_ /   )  //       |      \     _\
    ( // /) '/,_ _ _/  ( ; -.    |    _ _\.-~        .-~~~^-.
  (( / / )) ,-{        _      `-.|.-~-.           .~         `.
 (( // / ))  '/\      /                 ~-. _ .-~      .-~^-.  \
 (( /// ))      `.   {            }                   /      \  \
  (( / ))     .----~-.\        \-'                 .~         \  `. \^-.
             ///.----..>        \             _ -~             `.  ^-`  ^-_
               ///-._ _ _ _ _ _ _}^ - - - - ~                     ~-- ,.-~
                                                                  /.-~

 ___________________________
< TASK [忽略monster2错误会执行此任务] >
 ---------------------------
      \                    / \  //\
       \    |\___/|      /   \//  \\
            /0  0  \__  /    //  | \ \    
           /     /  \/_/    //   |  \  \  
           @_^_@'/   \/_   //    |   \   \ 
           //_^_/     \/_ //     |    \    \
        ( //) |        \///      |     \     \
      ( / /) _|_ /   )  //       |      \     _\
    ( // /) '/,_ _ _/  ( ; -.    |    _ _\.-~        .-~~~^-.
  (( / / )) ,-{        _      `-.|.-~-.           .~         `.
 (( // / ))  '/\      /                 ~-. _ .-~      .-~^-.  \
 (( /// ))      `.   {            }                   /      \  \
  (( / ))     .----~-.\        \-'                 .~         \  `. \^-.
             ///.----..>        \             _ -~             `.  ^-`  ^-_
               ///-._ _ _ _ _ _ _}^ - - - - ~                     ~-- ,.-~
                                                                  /.-~

changed: [192.168.71.139]
 ____________
< PLAY RECAP >
 ------------
      \                    / \  //\
       \    |\___/|      /   \//  \\
            /0  0  \__  /    //  | \ \    
           /     /  \/_/    //   |  \  \  
           @_^_@'/   \/_   //    |   \   \ 
           //_^_/     \/_ //     |    \    \
        ( //) |        \///      |     \     \
      ( / /) _|_ /   )  //       |      \     _\
    ( // /) '/,_ _ _/  ( ; -.    |    _ _\.-~        .-~~~^-.
  (( / / )) ,-{        _      `-.|.-~-.           .~         `.
 (( // / ))  '/\      /                 ~-. _ .-~      .-~^-.  \
 (( /// ))      `.   {            }                   /      \  \
  (( / ))     .----~-.\        \-'                 .~         \  `. \^-.
             ///.----..>        \             _ -~             `.  ^-`  ^-_
               ///-._ _ _ _ _ _ _}^ - - - - ~                     ~-- ,.-~
                                                                  /.-~

192.168.71.138             : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   
192.168.71.139             : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@monster1 ~]#