我有一個Ansible劇本,其包括文件兩次並傳入的參數來改變行爲:Ansible多個包含多個任務?
site.yml:
---
- tasks:
- include: test.yml parm=AAA
- include: test.yml parm=BBB
包含文件只是打印參數值:
test.yml:
- debug: msg="dbg 1 {{ parm }}"
清單文件被設置爲在本地主機上運行:
庫存:
localhost ansible_connection=local
結果是我期待的,包括文件,PARM運行兩次,一次用PARM = AAA,一旦= BBB :
>ansible-playbook -i inventory site.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [include parm=AAA] ********************************************************
included: test.yml for localhost
TASK [debug msg=dbg 1 {{ parm }}] **********************************************
ok: [localhost] => {
"changed": false,
"msg": "dbg 1 AAA"
}
TASK [include parm=BBB] ********************************************************
included: test.yml for localhost
TASK [debug msg=dbg 1 {{ parm }}] **********************************************
ok: [localhost] => {
"changed": false,
"msg": "dbg 1 BBB"
}
PLAY RECAP *********************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0
太好了。現在我需要做的第二個任務中包含文件:
test.yml:
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
我想到的是,包括文件將像以前一樣執行了兩次,第一次做原始的「dbg 1 AAA」任務,然後是新的「dbg 2 AAA」任務,然後執行原始的「dbg 1 BBB」任務,然後執行新的「dbg 2 BBB」任務。
它這樣做,而不是:
>ansible-playbook -i inventory site.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [include parm=AAA] ********************************************************
included: test.yml for localhost
TASK [debug msg=dbg 1 {{ parm }}] **********************************************
ok: [localhost] => {
"changed": false,
"msg": "dbg 1 AAA"
}
TASK [debug msg=dbg 2 {{ parm }}] **********************************************
ok: [localhost] => {
"changed": false,
"msg": "dbg 2 AAA"
}
PLAY RECAP *********************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
它已經跳過第二個包括。我想也許有包括相同的文件多次出現問題,所以我複製了包括使用新名稱的文件:
test2.yml:
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
,並調整了劇本,以包括而不是:
site.yml:
---
- tasks:
- include: test.yml parm=AAA
- include: test2.yml parm=BBB
然後,如果test.yml,只有一個任務,我得到預期的結果:
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [include parm=AAA] ********************************************************
included: test.yml for localhost
TASK [debug msg=dbg 1 {{ parm }}] **********************************************
ok: [localhost] => {
"changed": false,
"msg": "dbg 1 AAA"
}
TASK [include parm=BBB] ********************************************************
included: test2.yml for localhost
TASK [debug msg=dbg 1 {{ parm }}] **********************************************
ok: [localhost] => {
"changed": false,
"msg": "dbg 1 BBB"
}
TASK [debug msg=dbg 2 {{ parm }}] **********************************************
ok: [localhost] => {
"changed": false,
"msg": "dbg 2 BBB"
}
PLAY RECAP *********************************************************************
localhost : ok=6 changed=0 unreachable=0 failed=0
但如果test.yml有兩個任務,它會跳過第二個包括:
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [include parm=AAA] ********************************************************
included: test.yml for localhost
TASK [debug msg=dbg 1 {{ parm }}] **********************************************
ok: [localhost] => {
"changed": false,
"msg": "dbg 1 AAA"
}
TASK [debug msg=dbg 2 {{ parm }}] **********************************************
ok: [localhost] => {
"changed": false,
"msg": "dbg 2 AAA"
}
PLAY RECAP *********************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
我缺少什麼?沒有錯誤或失敗,並且包含文件中的兩行幾乎完全相同。爲什麼第一個包含文件中有多行會導致第二個包含被跳過?
如果我增加更多的調試行的劇本:
site.yml:
---
- tasks:
- include: test.yml parm=AAA
- debug: msg="1"
- include: test2.yml parm=BBB
- debug: msg="2"
- debug: msg="3"
調試消息是僅在包含文件preceeds他們恰好有一個線路輸出。
我從git://github.com/ansible/ansible.git devel分支運行Ansible,在測試之前立即更新。
這是一個錯誤,但他們現在已經修復了。 – DaveK