创建一个动态列表,其中多个文件的内容都是元素
Create a dynamic list where the content of multiple files are the elements
我遇到一个问题,我需要读取目录的内容并创建一个列表,其中每个项目都是该目录中 yaml 文件的内容。
目录结构是这样的
/vars/
abc.yml
def.yml
...
xyz.yml
各文件内容与此类似
---
manufacturer: F5
model: BIG-IP 3900
slug: big-ip-3900
part_number: '3900'
u_height: 1
is_full_depth: true
我的任务是这样的
---
- name: Include device types of .yaml files in directory into the 'device_types' variable
include_vars:
dir: vars/
ignore_unknown_extensions: True
extensions:
- "yaml"
- "yml"
name: device_types
- name: Create NetBox Device Types
netbox.netbox.netbox_device_type:
netbox_token: "{{ netbox_token }}"
netbox_url: "{{ netbox_url }}"
validate_certs: False
data:
model: "{{ item.model }}"
slug: "{{ item.slug }}"
part_number: "{{ item.part_number }}"
manufacturer: "{{ item.manufacturer | default(omit) }}"
u_height: "{{ item.u_height | default(omit) }}"
is_full_depth: "{{ item.is_full_depth | default(False)}}"
subdevice_role: "{{ item.subdevice_role | default(omit) }}"
comments: "{{ item.comments | default(omit) }}"
state: present
loop: "{{ device_types }}"
when: device_types is defined
run_once: yes
tags: netbox_device_types
但是,当我随后尝试遍历 device_types 变量时,它只是遍历每个文件内容的元素。
我无法更改文件格式,因此我必须更改导入以使数据适合。
`fatal: [localhost]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: {'manufacturer': 'Palo Alto', 'model': 'PA-850', 'slug': 'pa-850', 'part_number': 'PA-850', 'ight': 1, 'is_full_depth': False, 'interfaces': [{'name': 'management', 'type': '1000base-t', 'mgmt_only': True}, {'name': 'dedicated-ha1', 'type': '1000base-t'}, {'name': 'dedicated-ha2', 'type': '1000-t'}, {'name': 'loopback', 'type': 'virtual'}, {'name': 'ethernet1/1', 'type': '1000base-t'}, {'name': 'ethernet1/2', 'type': '1000base-t'}, {'name': 'ethernet1/3', 'type': '1000base-t'},..
给定最小化内容的文件进行测试
shell> tree test_vars
test_vars/
├── abc.yml
├── def.yml
└── xyz.yml
shell> cat test_vars/abc.yml
---
manufacturer: F5
model: BIG-IP 3900
shell> cat test_vars/def.yml
---
manufacturer: F5
model: BIG-IP 3901
shell> cat test_vars/xyz.yml
---
manufacturer: F5
model: BIG-IP 3902
查找文件
- find:
paths: test_vars
patterns: '*.yml,*.yaml'
register: result
给予
result.files|map(attribute='path')|list:
- test_vars/xyz.yml
- test_vars/abc.yml
- test_vars/def.yml
然后创建包含一个文件并将项目添加到列表的任务的文件,例如
shell> cat include_device_type.yml
- include_vars:
file: "{{ item }}"
name: device_type
- set_fact:
device_types: "{{ device_types|d([]) + [device_type] }}"
迭代文件并将数据包含在列表中
- include_tasks: include_device_type.yml
loop: "{{ result.files|map(attribute='path')|list }}"
给出设备类型列表
device_types:
- manufacturer: F5
model: BIG-IP 3902
- manufacturer: F5
model: BIG-IP 3900
- manufacturer: F5
model: BIG-IP 3901
我遇到一个问题,我需要读取目录的内容并创建一个列表,其中每个项目都是该目录中 yaml 文件的内容。
目录结构是这样的
/vars/
abc.yml
def.yml
...
xyz.yml
各文件内容与此类似
---
manufacturer: F5
model: BIG-IP 3900
slug: big-ip-3900
part_number: '3900'
u_height: 1
is_full_depth: true
我的任务是这样的
---
- name: Include device types of .yaml files in directory into the 'device_types' variable
include_vars:
dir: vars/
ignore_unknown_extensions: True
extensions:
- "yaml"
- "yml"
name: device_types
- name: Create NetBox Device Types
netbox.netbox.netbox_device_type:
netbox_token: "{{ netbox_token }}"
netbox_url: "{{ netbox_url }}"
validate_certs: False
data:
model: "{{ item.model }}"
slug: "{{ item.slug }}"
part_number: "{{ item.part_number }}"
manufacturer: "{{ item.manufacturer | default(omit) }}"
u_height: "{{ item.u_height | default(omit) }}"
is_full_depth: "{{ item.is_full_depth | default(False)}}"
subdevice_role: "{{ item.subdevice_role | default(omit) }}"
comments: "{{ item.comments | default(omit) }}"
state: present
loop: "{{ device_types }}"
when: device_types is defined
run_once: yes
tags: netbox_device_types
但是,当我随后尝试遍历 device_types 变量时,它只是遍历每个文件内容的元素。 我无法更改文件格式,因此我必须更改导入以使数据适合。
`fatal: [localhost]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: {'manufacturer': 'Palo Alto', 'model': 'PA-850', 'slug': 'pa-850', 'part_number': 'PA-850', 'ight': 1, 'is_full_depth': False, 'interfaces': [{'name': 'management', 'type': '1000base-t', 'mgmt_only': True}, {'name': 'dedicated-ha1', 'type': '1000base-t'}, {'name': 'dedicated-ha2', 'type': '1000-t'}, {'name': 'loopback', 'type': 'virtual'}, {'name': 'ethernet1/1', 'type': '1000base-t'}, {'name': 'ethernet1/2', 'type': '1000base-t'}, {'name': 'ethernet1/3', 'type': '1000base-t'},..
给定最小化内容的文件进行测试
shell> tree test_vars
test_vars/
├── abc.yml
├── def.yml
└── xyz.yml
shell> cat test_vars/abc.yml
---
manufacturer: F5
model: BIG-IP 3900
shell> cat test_vars/def.yml
---
manufacturer: F5
model: BIG-IP 3901
shell> cat test_vars/xyz.yml
---
manufacturer: F5
model: BIG-IP 3902
查找文件
- find:
paths: test_vars
patterns: '*.yml,*.yaml'
register: result
给予
result.files|map(attribute='path')|list:
- test_vars/xyz.yml
- test_vars/abc.yml
- test_vars/def.yml
然后创建包含一个文件并将项目添加到列表的任务的文件,例如
shell> cat include_device_type.yml
- include_vars:
file: "{{ item }}"
name: device_type
- set_fact:
device_types: "{{ device_types|d([]) + [device_type] }}"
迭代文件并将数据包含在列表中
- include_tasks: include_device_type.yml
loop: "{{ result.files|map(attribute='path')|list }}"
给出设备类型列表
device_types:
- manufacturer: F5
model: BIG-IP 3902
- manufacturer: F5
model: BIG-IP 3900
- manufacturer: F5
model: BIG-IP 3901