ansible - 无法附加到 with_items 循环中的列表中 [英] ansible - cannot append into a list in a with_items loop

查看:31
本文介绍了ansible - 无法附加到 with_items 循环中的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以这个剧本为例:

---
- hosts: localhost
  gather_facts: no
  vars:
    in_list:
      - value1
      - value2
      - value3
    final_list: []

  tasks:

    - debug:
        var: in_list

    - name: parse list
      set_fact:
        final_list: "{{ final_list + [{'key': item}] }}"
      with_items: "{{ in_list }}"

    - debug:
        var: final_list

似乎 final_list 在每次迭代中被最后一个 set_fact 替换替换,即它没有在每个循环中附加到它.

it seems that the final_list is replaced on each iteration by the last set_fact replacement, i.e its not appending to it on each loop.

输出:

[root@optima-ansible ILIAS]# ansible-playbook append_to_list.yml 

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

TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "in_list": [
        "value1", 
        "value2", 
        "value3"
    ]
}

TASK [parse list] ***************************************************************************************************************************************************************************************************
ok: [localhost] => (item=value1)
ok: [localhost] => (item=value2)
ok: [localhost] => (item=value3)

TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "final_list": [
        {
            "key": "value3"
        }
    ]
}

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

[root@optima-ansible ILIAS]# 

我使用了来自这个的代码问题

我做错了什么?

更新:我的设置:

[root@optima-ansible ILIAS]# ansible --version
ransible 2.5.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.14 (default, Mar 14 2018, 13:36:31) [GCC 7.3.1 20180303 (Red Hat 7.3.1-5)]
[root@optima-ansible ILIAS]# rpm -qa --last |  grep ansible
ansible-2.5.1-1.fc27.noarch                   Sun 22 Apr 2018 02:46:30 AM EEST
[root@optima-ansible ILIAS]# 

推荐答案

添加了关闭线程的答案,以及我从 Ansible 团队收到的关于我在 Github 上打开的问题的官方回复.显然他们已经知道这一点,他们关闭了我的问题,因为重复将我重定向到这两个:

adding an answer to close the thread, with the official response i got from the Ansible team on the issue i opened at Github. Obviously this was already known to them, they closed my issue as duplicate redirecting me to these 2:

#38302#38075

这篇关于ansible - 无法附加到 with_items 循环中的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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