Ansible:需要合并2个文件中的信息,以便以后执行任务
我正在Red Hat Linux 7.4上运行Ansible 2.7.9。在我的剧本中,我需要将两个yml文件中的信息组合成一个数据结构,然后在任务中迭代 例如,在一个.yml文件中,我可能有:Ansible:需要合并2个文件中的信息,以便以后执行任务,ansible,Ansible,我正在Red Hat Linux 7.4上运行Ansible 2.7.9。在我的剧本中,我需要将两个yml文件中的信息组合成一个数据结构,然后在任务中迭代 例如,在一个.yml文件中,我可能有: --- network_interfaces: big_computer: - name: eth0.10 - name: eth1.20 - name: ens224 - name: bond1 type: bond device: inte
---
network_interfaces:
big_computer:
- name: eth0.10
- name: eth1.20
- name: ens224
- name: bond1
type: bond
device: intel
---
eth0.10:
type: vlan
slave: true
master: bond1
subnet: 192.168.10.0/24
eth1.20:
type: vlan
slave: true
master: bond1
subnet: 192.168.20.0/24
ens224:
type: ethernet
subnet: 172.19.22.0/23
bond1:
type: bond
在另一个名为definitions.yml的.yml文件中,我可能有:
---
network_interfaces:
big_computer:
- name: eth0.10
- name: eth1.20
- name: ens224
- name: bond1
type: bond
device: intel
---
eth0.10:
type: vlan
slave: true
master: bond1
subnet: 192.168.10.0/24
eth1.20:
type: vlan
slave: true
master: bond1
subnet: 192.168.20.0/24
ens224:
type: ethernet
subnet: 172.19.22.0/23
bond1:
type: bond
例如,我可以从网络接口[计算机类型]
当computer\u type
为big\u computer
时,我想获取网络接口列表
的名称、来自网络接口列表
的数据和来自定义.yml
的数据,并创建这个组合数据结构。然后在任务中使用该数据结构。请注意,有些信息可能包含在网络接口
文件中,但有些信息可以在定义
文件中找到。或者它可能未定义-例如,bond1
接口没有“从”或“主”设置
网络接口列表最终应如下所示:
- eth0.10:
type: vlan
slave: true
master: bond1
subnet: 192.168.10.0/24
- eth1.20:
type: vlan
slave: true
master: bond1
subnet: 192.168.20.0/24
- ens224:
type: ethernet
subnet: 172.19.22.0/23
- bond1:
type: bond
device: intel
字典中缺少任何给定列表项的条目对于该项都不重要,基本上为空
我为这件事绞尽脑汁了一整天。我可以调试任何一个条目,但试图把它们放在一起——特别是在Ansible没有给我关于缺少字典键的错误的情况下——我发现这是不可能做到的
基本上,我想要的是一个简短的信息文件,对于那些乍一看不重要的项目,我想把它们放在另一层,如果我们愿意,我们可以仔细阅读以收集更多细节
我成功地做到了这一点:
- debug:
msg: "interface_list: {{ network_interfaces_list }}"
- name: MIKE debug
debug:
msg: "thingy: {{ interface['name'] }} \
{{ interface['type']|default('NULL') }} \
{{ interface['device']|default('NULL') }}"
loop: "{{ network_interfaces_list }}"
loop_control:
loop_var: interface
- name: MIKE2 debug
debug:
msg: "thing2: {{ lookup ( 'vars', interface['name'], default='XXX' ) }}"
loop: "{{ network_interfaces_list }}"
loop_control:
loop_var: interface
~~~
But I'm stuck in trying to get merge the members of `interface['name']` from the two data structures.
比如说
-设置事实:
网络接口列表:{{网络接口列表}默认值([])+
[{项目:{u val}]}”
循环:“{{u key}}”
变量:
计算机类型:大型计算机
_键:{{network_interfaces[computer_type]| map(attribute='name')| list}
_VAL:“{{网络接口[计算机类型]}”
_格言:{{dict({keys}zip({vals))}
_val:{{{u dict[item]| combine(definitions[item])}”
给予
网络接口列表:
-eth0.10:
主人:邦德1
名称:eth0.10
奴隶:是的
子网:192.168.10.0/24
类型:vlan
-eth1.20:
主人:邦德1
名称:eth1.20
奴隶:是的
子网:192.168.20.0/24
类型:vlan
-ens224:
姓名:ens224
子网:172.19.22.0/23
类型:以太网
-债券1:
设备:英特尔
姓名:bond1
类型:债券
注释
- 字典保留冗余的属性名
- definitions.yml中变量的名称无效。看见把它们包括在字典里
-包括变量:
文件:definitions.yml
名称:定义
给予
定义:
债券1:
类型:债券
ens224:
子网:172.19.22.0/23
类型:以太网
eth0.10:
主人:邦德1
奴隶:是的
子网:192.168.10.0/24
类型:vlan
eth1.20:
主人:邦德1
奴隶:是的
子网:192.168.20.0/24
类型:vlan
- 在某些情况下,字典可能是更好的结构,例如
-设置事实:
网络接口指令:{{网络接口指令{默认值({})|
组合({item:_val}}}}”
...
会给
network\u interfaces\u dict:
债券1:
设备:英特尔
姓名:bond1
类型:债券
ens224:
姓名:ens224
子网:172.19.22.0/23
类型:以太网
eth0.10:
主人:邦德1
名称:eth0.10
奴隶:是的
子网:192.168.10.0/24
类型:vlan
eth1.20:
主人:邦德1
名称:eth1.20
奴隶:是的
子网:192.168.20.0/24
类型:vlan
vladimir botka,这是个严肃的问题。你在哪里学会了如何玩这样的杂耍?有没有有用的网站或其他资源@弗拉基米尔·博特卡只是练习。我想看看我的收藏。谢谢你的评论。