在Ansible中一起使用set_facts和with_items

时间:2015-04-01 19:52:01

标签: ansible

我目前正在使用Ansible 1.7.2。我有以下测试剧本:

---
- hosts: localhost
  tasks:
  - name: set fact 1
    set_fact: foo="[ 'zero' ]"

  - name: set fact 2
    set_fact: foo="{{ foo }} + [ 'one' ]"

  - name: set fact 3
    set_fact: foo="{{ foo }} + [ 'two', 'three' ]"

  - name: set fact 4
    set_fact: foo="{{ foo }} + [ '{{ item }}' ]"
    with_items:
      - four
      - five
      - six

  - debug: var=foo

第一项任务设定了一个事实,即列表中包含一个项目。后续任务将附加更多值附加到该列表。前三个任务按预期工作,但最后一个任务不起作用。这是我运行时的输出:

PLAY [localhost] **************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [set fact 1] ************************************************************
ok: [localhost]

TASK: [set fact 2] ************************************************************
ok: [localhost]

TASK: [set fact 3] ************************************************************
ok: [localhost]

TASK: [set fact 4] ************************************************************
ok: [localhost] => (item=four)
ok: [localhost] => (item=five)
ok: [localhost] => (item=six)

TASK: [debug var=foo] *********************************************************
ok: [localhost] => {
    "foo": [
        "zero",
        "one",
        "two",
        "three",
        "six"
    ]
}

PLAY RECAP ********************************************************************
localhost                  : ok=6    changed=0    unreachable=0    failed=0

鉴于任务4中的with_items以及输出显示任务在该列表中的项目上正确迭代的事实,我希望结果包含从0到6的所有数字。但是最后一个任务似乎只是用列表中的最后一项来评估set_fact。这可能是Ansible中的错误吗?

编辑:我也刚刚在ansible 1.8上进行了测试,输出结果相同。

6 个答案:

答案 0 :(得分:74)

有一种解决方法可能有所帮助。您可以为每个set_fact迭代“注册”结果,然后将结果映射到列表:

---
- hosts: localhost
  tasks:
  - name: set fact
    set_fact: foo_item="{{ item }}"
    with_items:
      - four
      - five
      - six
    register: foo_result

  - name: make a list
    set_fact: foo="{{ foo_result.results | map(attribute='ansible_facts.foo_item') | list }}"

  - debug: var=foo

输出:

< TASK: debug var=foo >
 ---------------------
    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||


ok: [localhost] => {
    "var": {
        "foo": [
            "four", 
            "five", 
            "six"
        ]
    }
}

答案 1 :(得分:9)

正如其他人的评论中所提到的,这里给出的最佳解决方案在Ansible 2.2中并不适合我,特别是在使用with_items时。

看来OP的预期方法现在可以正常工作,但引用item略有变化。

- set_fact: something="{{ something + [ item ] }}"
  with_items:
    - one
    - two
    - three

还有一个较长的例子,我处理了列表的初始情况未定义并添加了一个可选的when因为这也让我感到悲伤:

- set_fact: something="{{ something|default([]) + [ item ] }}"
  with_items:
    - one
    - two
    - three
  when: item.name in allowed_things.item_list

答案 2 :(得分:7)

Jinja 2.6没有地图功能。因此,另一种方法是:

set_fact: foo="{% for i in bar_result.results %}{{ i.ansible_facts.foo_item }}{%endfor%}"

答案 3 :(得分:3)

更新2018-06-08:我之前的回答是有点黑客,所以我回来再看一遍。这是一种更清洁的Jinja2方法。

- name: Set fact 4
  set_fact:
    foo: "{% for i in foo_result.results %}{% do foo.append(i) %}{% endfor %}{{ foo }}"

我正在添加此答案,因为Ansible 2.2+的当前最佳答案并未完全涵盖原始问题。感谢Russ Huguley的回答,这让我朝着正确的方向前进,但它给我留下了一个串联的字符串而不是列表。这个解决方案得到一个列表,但变得更加hacky。我希望以更清洁的方式解决这个问题。

- name: build foo_string
  set_fact:
    foo_string: "{% for i in foo_result.results %}{{ i.ansible_facts.foo_item }}{% if not loop.last %},{% endif %}{%endfor%}"

- name: set fact foo
  set_fact:
    foo: "{{ foo_string.split(',') }}"

答案 4 :(得分:3)

我正在寻找这个问题的答案。我发现这很有帮助。该模式在with_items的文档中并不明显。

https://github.com/ansible/ansible/issues/39389

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: set_fact
      set_fact:
        foo: "{{ foo }} + [ '{{ item }}' ]"
      with_items:
        - "one"
        - "two"
        - "three"
      vars:
        foo: []

    - name: Print the var
      debug:
        var: foo

答案 5 :(得分:1)

看起来这种行为是Ansible目前的工作方式,尽管有很多兴趣将其修复为按需运行。目前有一个pull request具有所需的功能,所以希望这最终会被整合到Ansible中。