在 Ansible 中一起使用 set_facts 和 with_items [英] Using set_facts and with_items together in Ansible

查看:16
本文介绍了在 Ansible 中一起使用 set_facts 和 with_items的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是 Ansible 1.7.2.我有以下测试手册:

I'm currently using Ansible 1.7.2. I have the following test playbook:

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

第一个任务设置一个事实,即一个包含一个项目的列表.后续任务将附加到具有更多值的列表中.前三个任务按预期工作,但最后一个没有.这是我运行时的输出:

The first task sets a fact that's a list with one item in it. The subsequent tasks append to that list with more values. The first three tasks work as expected, but the last one doesn't. Here's the output when I run this:

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 以及输出显示任务在该列表中的项目上正确迭代的事实,我本希望结果包含从零到六的所有数字.但是最后一个任务似乎只用列表中的最后一项来评估 set_fact .这可能是 Ansible 中的错误吗?

Given the with_items in task 4 and the fact that the output shows the task properly iterated over the items in that list, I would have expected the result to contain all the numbers zero through six. But that last task seems to only be evaluating set_fact with the last item in the list. Is this possibly a bug in Ansible?

我也刚刚在 ansible 1.8 上测试过这个,输出是相同的.

I also just tested this on ansible 1.8 and the output was identical.

推荐答案

看起来这种行为是 Ansible 当前的工作方式,尽管有很多兴趣修复它以按需要工作.目前有一个具有所需功能的拉取请求,所以希望这最终会被纳入 Ansible.

Looks like this behavior is how Ansible currently works, although there is a lot of interest in fixing it to work as desired. There's currently a pull request with the desired functionality so hopefully this will get incorporated into Ansible eventually.

这篇关于在 Ansible 中一起使用 set_facts 和 with_items的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆