Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每个列表的第一项附加到ansible playbook中的另一个列表中_Ansible_Jinja2 - Fatal编程技术网

每个列表的第一项附加到ansible playbook中的另一个列表中

每个列表的第一项附加到ansible playbook中的另一个列表中,ansible,jinja2,Ansible,Jinja2,我似乎做不到以上这些 --- ... vars: sg: - hostname: test26081 skip: this1 - hostname: test26082 skip: this2 - set_fact: sg_dest: "{{ sg[0]['hostname'] }}" 我上面的解决方案只会将第一个主机名(test26081)附加到我的set_事实上,我想包括第二个

我似乎做不到以上这些

---
...
  vars:
    sg:
      - hostname: test26081
        skip: this1
      - hostname: test26082
        skip: this2

    - set_fact:
        sg_dest: "{{ sg[0]['hostname'] }}"
我上面的解决方案只会将第一个主机名(test26081)附加到我的set_事实上,我想包括第二个主机名test26082,这样我就只有一个简单的主机名列表。我试过各种各样的方法,但都没有用


我无法硬编码第二个值
“{{sg[1]['hostname']}}”
,因为它需要为新条目进行缩放。

您可以在
set\u fact
任务中循环您的列表
sg
,并将其附加到新列表中(
sg\u dest


谢谢你。我只是想这样做,但我的语法是不正确的,读了你的评论后,我修复了它,现在它的工作。谢谢你的意见。
--- 
- hosts: localhost
  vars:
    sg:
    - hostname: test26081
      skip: this1
    - hostname: test26082
      skip: this2
  tasks: 
  - debug: var=sg
  - name: Initialize an empty list for our hostnames
    set_fact:
      sg_dest: []
  - set_fact:
      sg_dest: "{{ sg_dest + [ item.hostname ] }}"
    with_items: "{{ sg }}"
  - debug: var=sg_dest