我使用ansible更新新添加的NIC 的,我已经在不同的YML文件中定义的一些变量的配置文件Ansible遍历变量
/tmp/ip.yml
#first interface
interface1: eth1
bootproto1: static
ipaddress1: 192.168.211.249
netmask1: 255.255.255.0
gateway: 192.168.211.2
DNS1: 192.168.211.2
#second interface
interface2: eth2
bootproto2: static
ipaddress2: 10.0.0.100
netmask2: 255.0.0.0
剧本
- include_vars: /tmp/ip.yml
- name: configuring interface
lineinfile:
state=present
create=yes
dest=/etc/sysconfig/network-scripts/ifcfg-{{interface1}}
regexp="{{ item.regexp }}"
line="{{ item.line }}"
with_items:
- { regexp: '^BOOTPROTO=.*', line: 'BOOTPROTO={{interface1}}' }
- { regexp: '^IPADDR=.*', line: 'IPADDR={{ipaddress1}' }
- { regexp: '^NETMASK=.*', line: 'NETMASK={{netmask1}}' }
- { regexp: '^GATEWAY=.*', line: 'GATEWAY={{gateway}}' }
- { regexp: '^PEERDNS=.*', line: 'PEERDNS=no' }
- { regexp: '^DNS1=.*', line: 'DNS1={{DNS1}}' }
- { regexp: '^ONBOOT=.*', line: 'ONBOOT={{onboot}}' }
when: bootproto1 == 'static'
- name: configuring for DHCP
lineinfile:
state=present
create=yes
dest=/etc/sysconfig/network-scripts/ifcfg-{{interface1}}
regexp="{{ item.regexp }}"
line="{{ item.line }}"
with_items:
- { regexp: '^BOOTPROTO=.*',line: 'BOOTPROTO={{bootproto1}}' }
- {regexp: '^PEERDNS=.*',line: 'PEERDNS=yes' }
- { regexp: '^ONBOOT=.*', line: 'ONBOOT={{onboot}}' }
when: bootproto1 == 'dhcp'
类似地重复用于第二接口。
尽管这种方法适用于2个NIC,但这太难管理了,即每增加一个新NIC,我都需要修改Playbook并更新/tmp/ip.yml中的相应变量。
有没有办法将变量添加到/tmp/ip.yml,并且可能使用某些分隔符将其解析为playbook,而无需每次修改playbook以插入新的NIC。
感谢@leucos如果我像你提到的http://pastebin.com/ev8gSnH6更改var文件,那么我仍然可以使用现有的方法。 ,因为该变量文件将包含更多的功能。 – 2014-10-29 05:13:47
在你的pastebin中,你覆盖了你的变量:你将'interface'定义为'eth1',然后将它重新定义为'eth2'。这就是为什么变量必须在列表中。见上面的'interfaces_ipv4'(注意:这部分已经被编辑,因为它是错误的)。 – leucos 2014-10-29 08:59:46