我有一个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