每个列表的第一项附加到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