【问题标题】:Ansible multiple include with multiple tasks?Ansible 多个包含多个任务?
【发布时间】:2015-11-18 13:53:17
【问题描述】:

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

太好了。现在我需要在包含文件中执行第二个任务:

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"

只有在它们之前的包含文件正好有一行时才会输出调试消息。

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

【问题讨论】:

    标签: ansible


    【解决方案1】:

    问题首先出现在 git here 中。

    This优化导致问题。

    【讨论】:

      【解决方案2】:

      我建议将此作为针对核心项目@https://github.com/ansible/ansible/issues 的问题提交。在 2.0 中对动态任务队列的内容进行了很多更改,这听起来像是一个合理的错误。

      【讨论】:

      • 这是一个错误,但他们现在已经修复了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多