Loops 在Ansible中构造循环
我正在写一个负责建立网络的角色。对于每种类型的接口(ethernet、bond、bridge和vlan),我都制作了一个包含相关数据的变量。 我的想法是,我必须创建一个循环,循环运行的次数与列表变量(“桥接端口”)中有元素的次数相同,并且通过模板为每次传递创建一个配置文件 桥接接口的部分变量如下所示:Loops 在Ansible中构造循环,loops,ansible,Loops,Ansible,我正在写一个负责建立网络的角色。对于每种类型的接口(ethernet、bond、bridge和vlan),我都制作了一个包含相关数据的变量。 我的想法是,我必须创建一个循环,循环运行的次数与列表变量(“桥接端口”)中有元素的次数相同,并且通过模板为每次传递创建一个配置文件 桥接接口的部分变量如下所示: my_network__bridge_interface: address: "192.168.1.48", bootproto: "static", bridge_por
my_network__bridge_interface:
address: "192.168.1.48",
bootproto: "static",
bridge_ports:
- eth0
- eth1
device: "br-mgmt",
...
为了通过,我尝试了一个with_子元素循环——但这并不顺利
- name: Create the network configuration file for the port on the bridge devices
template:
src: "{{ ansible_os_family }}.bridge_port.j2"
dest: "{{ my_network__ifconf_path }}/ifcfg-{{ item.1 }}"
with_subelements
- "{{ my_network__bridge_interface }}"
- bridge_ports
when: device_conf.type == 'bridge'
register: my_network__bridge_port_result
当我运行代码时,错误消息出现:“在迭代项{}中找不到'bridge_ports'键。”
我可以看出我用错误的方式使用了_子元素,但我真的不知道我需要什么类型的循环。问题在于yml定义。以下yml工程:
my_network__bridge_interface:
- address: "192.168.1.48"
bootproto: static
bridge_ports:
- eth0
- eth1
device: br-mgmt
剧本-->
输出-->
不管好坏看见
---
- hosts: localhost
tasks:
- include_vars: vars.yml
- debug:
msg: "{{ item.1 }}"
with_subelements:
- "{{ my_network__bridge_interface }}"
- bridge_ports
TASK [debug] ****************************************************************************************************************************
ok: [localhost] => (item=[{u'device': u'br-mgmt', u'bootproto': u'static', u'address': u'192.168.1.48'}, u'eth0']) => {
"msg": "eth0"
}
ok: [localhost] => (item=[{u'device': u'br-mgmt', u'bootproto': u'static', u'address': u'192.168.1.48'}, u'eth1']) => {
"msg": "eth1"
}