我在 RHEL 7 主机上使用 ansible 2.0.1。通过剧本调用时,以某种方式跟随角色会跳过获取黄色主要版本。在下面附加输出。看起来 when 条件有问题。
- set_fact: is_yos="true"
- name: Fetch yellow version
shell: /bin/gawk '{ for(col = 1; col <= NF; col++) if ($col ~ /[0-9.]+/) { print $col; } }' /etc/yellow-release
register: yel_ver_str
failed_when: false
changed_when: false
- set_fact:
is_yos: "false"
when: yel_ver_str.rc != 0
- debug: var=yel_ver_str
- debug: var=is_yos
- name: Get yellow major version.
shell: echo {{ yel_ver_str.stdout }} | awk -F. '{print $1}'
register: yel_major_ver
when: is_yos == "true"
delegate_to: localhost
changed_when: false
输出
ansible-playbook -i hosts test.yml -c local
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [172.31.83.23]
TASK [test : set_fact] *********************************************************
ok: [172.31.83.23]
TASK [test : Fetch yellow version] *********************************************
ok: [172.31.83.23]
TASK [test : set_fact] *********************************************************
skipping: [172.31.83.23]
TASK [test : debug] ************************************************************
ok: [172.31.83.23] => {
"yel_ver_str": {
"changed": false,
"cmd": "/bin/gawk '{ for(col = 1; col <= NF; col++) if ($col ~ /[0-9.]+/) { print $col; } }' /etc/yellow-release",
"delta": "0:00:00.004225",
"end": "2016-04-22 13:37:29.517570",
"failed": false,
"failed_when_result": false,
"rc": 0,
"start": "2016-04-22 13:37:29.513345",
"stderr": "",
"stdout": "7.2.0",
"stdout_lines": [
"7.2.0"
],
"warnings": []
}
}
TASK [test : debug] ************************************************************
ok: [172.31.83.23] => {
"is_yos": true
}
TASK [test : Get yellow major version.] ****************************************
skipping: [172.31.83.23]
PLAY RECAP *********************************************************************
172.31.83.23 : ok=5 changed=0 unreachable=0 failed=0