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

查看:87
本文介绍了在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天全站免登陆