2016-04-22 33 views
0

我在RHEL 7主機上使用ansible 2.0.1。在通過劇本進行調用時跳過獲取黃色主要版本時按某種方式執行角色。 將輸出連接到下面。看起來什麼時候有什麼問題。set_fact不存在


- 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 
+0

這表明'yel_ver_str.rc == 0',這就是爲什麼你的'set_fact'任務正在被跳過。該任務只會在'yel_ver_str.rc!= 0'時運行。 – larsks

+0

當「獲取黃色主要版本」的條件是:is_yos ==「true」,而不是yel_ver_str.rc!= 0。另外,我將is_yos的默認值設置爲true –

回答

0

看看從你debug任務的輸出:

TASK [test : debug] ************************************************************ 
ok: [172.31.83.23] => { 
    "is_yos": true 
} 

is_yos其實是布爾變量(true是一個布爾值,而"true"將是一個字符串值)。所以,當你測試:

when: is_yos == "true" 

支票是假,因爲你是比較一個布爾到字符串。相反使用:

when: is_yos 
+0

非常感謝您使用它的魅力: ) –