ansible: 判断的使用when(not、 ignore_errors和.rc)


1. when

在这里插入图片描述

[root@server4 pd]# cat pd.yml    # 当item大于1时输出
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug: 
      msg: "{{item}}"
    with_items:
    - 1
    - 2
    - 3
   when: item > 1

测试:

[root@server4 pd]# ansible-playbook pd.yml 

PLAY [testB] *******************************************************************

TASK [debug] *******************************************************************
skipping: [172.25.60.3] => (item=1) 
ok: [172.25.60.3] => (item=2) => {
    "msg": 2
}
ok: [172.25.60.3] => (item=3) => {
    "msg": 3
}

2. 获取ansible主机的信息

[root@server4 pd]# ansible testB -m setup| grep ansible_distribution
        "ansible_distribution": "RedHat", 
        "ansible_distribution_file_parsed": true, 
        "ansible_distribution_file_path": "/etc/redhat-release", 
        "ansible_distribution_file_search_string": "Red Hat", 
        "ansible_distribution_file_variety": "RedHat", 
        "ansible_distribution_major_version": "7", 
        "ansible_distribution_release": "Maipo", 
        "ansible_distribution_version": "7.5",

3. 判断主机信息是否一致

写法1:

[root@server4 pd]# ls
pd1.yml  pd.yml
[root@server4 pd]# cat pd1.yml 
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "system release is redhat7"
    when: ansible_distribution == "RedHat" and ansible_distribution_major_version == "7"

写法2:


 - [ ] [root@server4 pd]# cat pd1.yml 
       ---
       - hosts: testB   remote_user: root   tasks:
         - debug:
             msg: "system release is redhat7"
           when: 
           - ansible_distribution == "RedHat"
           - ansible_distribution_major_version == "7"

测试:

[root@server4 pd]# ansible-playbook pd1.yml 

PLAY [testB] *************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [172.25.60.3]

TASK [debug] *************************************************************************************************************************************
ok: [172.25.60.3] => {
    "msg": "system release is redhat7"
}

4. not的使用

[root@server4 pd]# cat pd1.yml 
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "system release is not windows"
    when: not ansible_distribution == "Windows"

测试:

[root@server4 pd]# ansible-playbook pd1.yml 

PLAY [testB] *******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [172.25.60.3]

TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
    "msg": "system release is not windows"
}

5. ignore_errors和.rc判断

[root@server4 pd]# cat pd2.yml 
---
- hosts: testB
  remote_user: root
  tasks:
  - name: task1
    shell: "ls /testhaha"
    register: returnmsg
    ignore_errors: true   # 如果return的返回结果有误,则忽略
  - name: task2
    debug:
      msg: "successful"
    when: returnmsg.rc == 0   # rc==0表示shell的内容执行成功
  - name: task3
    debug:
      msg: "failed"
    when: returnmsg.rc != 0  # rc==0表示shell的内容执行不成功

测试:

[root@server4 pd]# ansible-playbook pd2.yml 

PLAY [testB] *******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [172.25.60.3]

TASK [task1] *******************************************************************
fatal: [172.25.60.3]: FAILED! => {"changed": true, "cmd": "ls /testhaha", "delta": "0:00:00.003328", "end": "2020-03-29 11:35:02.442537", "msg": "non-zero return code", "rc": 2, "start": "2020-03-29 11:35:02.439209", "stderr": "ls: cannot access /testhaha: No such file or directory", "stderr_lines": ["ls: cannot access /testhaha: No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [task2] *******************************************************************
skipping: [172.25.60.3]

TASK [task3] *******************************************************************
ok: [172.25.60.3] => {
    "msg": "failed"
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值