Ansible 循环变量 [英] Ansible loop over variables
问题描述
我正在使用 ansible 更新新添加的 NIC 的配置文件为此,我在单独的 yml 文件中定义了一些变量
i am using ansible to update configuration file of newly added NIC for that i have defined some variables in separate yml file
/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'
类似地重复第二个界面.
similarly repeated for second interface.
虽然这个方法适用于2个网卡,但是管理起来太难了,就是每添加一个新的网卡我都需要修改playbook并更新/tmp/ip.yml中的相应变量.
Even Though this method works for 2 NIC,this is too difficult to manage ,that is for each new NIC added i need to modify playbook and update corresponding variable in /tmp/ip.yml.
有没有办法将变量添加到/tmp/ip.yml 并且可能使用一些分隔符将其解析为 playbook,而无需每次都修改 playbook 以插入新的 NIC.
Is there a way to add variables to /tmp/ip.yml and may be using some separator parse it to playbook with out modifying playbook each time for plugging in new NIC.
推荐答案
这里有很多话要说.首先,尽量避免 lineinfile
像瘟疫一样.这确实是一个最后的手段解决方案.lineinfile
使得编写一致且幂等的剧本变得困难.
There is a lot to say here.
First, try to avoid lineinfile
like plague. It is really a last-resort solution. lineinfile
makes it hard to write consistent and idempotents playbooks.
现在,由于您正在尝试填充 RH 样式的界面文件,因此很容易做到.
Now, since you're trying to populate RH style interface files, it is quite easy to do.
组织变量
首先要做的是为变量设置合适的结构.你会想要循环你的接口,所以你必须让东西可循环".有 interface1
, interface2
... interfaceN
不能像你提到的那样扩展.
The first thing to do is to have a proper structure for your variables. You'll want to loop over your interfaces so you have to make stuff 'loopable'. Having interface1
, interface2
... interfaceN
is not scalable as you mentioned.
这是一个建议:
interfaces_ipv4:
- name: eth0
bootproto: static
ipaddress: 192.168.211.249
netmask: 255.255.255.0
gateway: 192.168.211.2
dns: 192.168.211.2
- name: eth2
bootproto: static
ipaddress: 10.0.0.100
netmask: 255.0.0.0
编写模板
现在您有了数据,您需要一个模板来创建您的操作系统配置文件.
Now that you have your data, you need a template to create your OS config file.
BOOTPROTO={{item.bootproto}}
IPADDR={{item.ipaddress}}
NETMASK={{item.netmask}}
{% if item.gateway is defined %}
GATEWAY={{item.gateway}}
{% endif %}
PEERDNS=no
DNS1={{item.dns}}
ONBOOT={{item.onboot|default('no')}}
我包含了两个变体:您可以在未设置时跳过输出行({% if ... %}
构造)或提供默认值(例如 {{item.onboot|default('no')}}
).
I included two variations : you can skip outputting a line when it's not set ({% if ... %}
construct) or provide default values (for instance {{item.onboot|default('no')}}
).
您的里程可能会有所不同,具体取决于您是要使用默认值还是跳过 if 结构.
Your mileage may vay, depending if you want to use a default or to skip with the if construct.
创建任务
最后,这是一个将为每个接口创建接口配置文件的任务:
Finally, here is a task that will create interface configuration files for each interface :
- name: Push template
template:
src=/path/to/the/above/template.j2
dest=/etc/sysconfig/network-scripts/ifcfg-{{item.name}}.cfg
with_items:
- "{{ interfaces_ipv4 }}"
这应该就可以了.
当然,使用此任务的最佳方法是将其添加到某个网络"角色,并从剧本中调用它.
Of course, best way to use this task is to add it to some "network" role, and call it from a playbook.
祝你好运.
这篇关于Ansible 循环变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!