[已关闭]这是Ansible v2.5.1中的错误,请参见下面的评论。
我想根据字典建立一个新列表。因此,我尝试使用set_fact
和loop
,但变量仅包含最后一个值(而不是列表)
我尝试不带字典的简单示例。我使用以下网站:https://ttl255.com/ansible-appending-to-lists-and-dictionaries/。而且这是行不通的。
---
- name: Append to list
hosts: localhost
vars:
devices: []
cisco:
- CiscoRouter01
- CiscoRouter02
- CiscoRouter03
- CiscoSwitch01
arista:
- AristaSwitch01
- AristaSwitch02
- AristaSwitch03
tasks:
- name: Add Cisco and Airsta devices to the list
set_fact:
devices: "{{ devices + [item] }}"
with_items:
- "{{ cisco }}"
- "{{ arista }}"
- name: Debug list
debug:
var: devices
verbosity: 0
输出提取:
TASK [Debug list] *********************************************************************************************************
ok: [localhost] => {
"devices": [
"AristaSwitch03"
]
}
预期:
TASK [Debug list] *********************************************************************************************************
ok: [localhost] => {
"devices": [
"CiscoRouter01",
"CiscoRouter02",
"CiscoRouter03",
"CiscoSwitch01",
"AristaSwitch01",
"AristaSwitch02",
"AristaSwitch03"
]
}
我使用的是ansible版本:2.5.1