变量的Ansible循环 [英] Ansible loop over variables

查看:73
本文介绍了变量的Ansible循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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个NIC,也很难管理,也就是说,对于添加的每个新NIC,我都需要修改剧本并更新/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中,并且可能使用一些分隔符将其解析为剧本,而无需每次插入新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.这确实是一个 last-resort 解决方案. 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.

组织变量

要做的第一件事是为您的变量建立一个适当的结构.您将需要遍历您的接口,因此必须使内容可循环".如前所述,具有interface1interface2 ... 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆