Ansible multiple包含多个任务?

Ansible multiple包含多个任务?,ansible,Ansible,我有一个Ansible playbook,它包含一个文件两次,并传入一个参数以更改行为: site.yml: --- - tasks: - include: test.yml parm=AAA - include: test.yml parm=BBB - debug: msg="dbg 1 {{ parm }}" localhost ansible_connection=local - debug: msg="dbg 1 {{ parm }}" - debug: msg="dbg

我有一个Ansible playbook,它包含一个文件两次,并传入一个参数以更改行为:

site.yml:

---
- tasks:
  - include: test.yml parm=AAA
  - include: test.yml parm=BBB
- debug: msg="dbg 1 {{ parm }}"
localhost ansible_connection=local
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
---
- tasks:
  - include: test.yml parm=AAA
  - include: test2.yml parm=BBB
---
- tasks:
  - include: test.yml parm=AAA
  - debug: msg="1"
  - include: test2.yml parm=BBB
  - debug: msg="2"
  - debug: msg="3"
include文件仅打印参数值:

test.yml:

---
- tasks:
  - include: test.yml parm=AAA
  - include: test.yml parm=BBB
- debug: msg="dbg 1 {{ parm }}"
localhost ansible_connection=local
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
---
- tasks:
  - include: test.yml parm=AAA
  - include: test2.yml parm=BBB
---
- tasks:
  - include: test.yml parm=AAA
  - debug: msg="1"
  - include: test2.yml parm=BBB
  - debug: msg="2"
  - debug: msg="3"
清单文件设置为在本地主机上运行:

库存:

---
- tasks:
  - include: test.yml parm=AAA
  - include: test.yml parm=BBB
- debug: msg="dbg 1 {{ parm }}"
localhost ansible_connection=local
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
---
- tasks:
  - include: test.yml parm=AAA
  - include: test2.yml parm=BBB
---
- tasks:
  - include: test.yml parm=AAA
  - debug: msg="1"
  - include: test2.yml parm=BBB
  - debug: msg="2"
  - debug: msg="3"
结果就是我所期望的,include文件运行两次,一次是parm=AAA,一次是parm=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   
太好了。现在,我需要在include文件中执行第二个任务:

test.yml:

---
- tasks:
  - include: test.yml parm=AAA
  - include: test.yml parm=BBB
- debug: msg="dbg 1 {{ parm }}"
localhost ansible_connection=local
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
---
- tasks:
  - include: test.yml parm=AAA
  - include: test2.yml parm=BBB
---
- tasks:
  - include: test.yml parm=AAA
  - debug: msg="1"
  - include: test2.yml parm=BBB
  - debug: msg="2"
  - debug: msg="3"
我所期望的是,include文件将像以前一样执行两次,首先执行原始的“dbg1aaa”任务,然后执行新的“dbg2aaa”任务,然后执行原始的“dbg1bbb”任务,然后执行新的“dbg2bbb”任务

而是这样做的:

>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:

---
- tasks:
  - include: test.yml parm=AAA
  - include: test.yml parm=BBB
- debug: msg="dbg 1 {{ parm }}"
localhost ansible_connection=local
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
---
- tasks:
  - include: test.yml parm=AAA
  - include: test2.yml parm=BBB
---
- tasks:
  - include: test.yml parm=AAA
  - debug: msg="1"
  - include: test2.yml parm=BBB
  - debug: msg="2"
  - debug: msg="3"
并将剧本调整为包含以下内容:

site.yml:

---
- tasks:
  - include: test.yml parm=AAA
  - include: test.yml parm=BBB
- debug: msg="dbg 1 {{ parm }}"
localhost ansible_connection=local
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
---
- tasks:
  - include: test.yml parm=AAA
  - include: test2.yml parm=BBB
---
- tasks:
  - include: test.yml parm=AAA
  - debug: msg="1"
  - include: test2.yml parm=BBB
  - debug: msg="2"
  - debug: msg="3"
然后,如果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   
我错过了什么?没有错误或失败,include文件中的两行几乎相同。为什么在第一个包含文件中有多行会导致跳过第二个包含

如果我在剧本中添加更多调试行:

site.yml:

---
- tasks:
  - include: test.yml parm=AAA
  - include: test.yml parm=BBB
- debug: msg="dbg 1 {{ parm }}"
localhost ansible_connection=local
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
- debug: msg="dbg 1 {{ parm }}"
- debug: msg="dbg 2 {{ parm }}"
---
- tasks:
  - include: test.yml parm=AAA
  - include: test2.yml parm=BBB
---
- tasks:
  - include: test.yml parm=AAA
  - debug: msg="1"
  - include: test2.yml parm=BBB
  - debug: msg="2"
  - debug: msg="3"
只有在调试消息前面的include文件只有一行时,才会输出调试消息


我正从git://github.com/ansible/ansible.git devel分支,在测试之前立即更新。

问题首先出现在git中


优化导致了问题。

我建议将此作为核心项目的一个问题归档。在2.0中,动态任务队列的include有很多更改,这听起来像是一个合法的bug。

这是一个bug,但他们现在已经修复了。