Ansible loop over variables -


I am using the responsible for updating the configuration of the newly added NIC for which I have different yml file Some variables are defined in

/tmp/ip.yml

  # Interface Interface 1: Ath 1 bootproof1: Fixed IP address 1 : 1982.168.211.249 Netmask 1: 255.255.255.0 Gateway: 1 9 02.168 211.2 DNS1: 192.168.211.2 #second Interface Interface 2: AT2 bootproof2: Static IP address 2: 10.0.0.100 Netmask 2: 255.0.0.0    Playbook  

  - include_vars: /tmp/ip.yml-name: Configuring Interface Lineline File: State = Create = Yes dest = / etc / Sysconfig / network-scripts / ifcfg - {{interface1}} regexp = "{{item (Regexp: '^ BOOTPROTO =. *', Line: 'BOOTPROTO = {{interface1}}'} - {regexp: '^ IPADDR = . * ', Line:' ipadrr = {{ipaddress1} '} - {regexp:' ^ NETMASK =. * ', Line:' netmask = {{netmask 1}} '} - {regexp:' ^ gate = * ', 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 duplicated for other interfaces.

Even though this method works for 2 NICs, it is very difficult to manage, that every new NIC has said that I am trying to modify playback and / tmp There is a need to update related variables in /ip.yml .

Is there a way to add variables to /tmp/ip.yml and it can use some separator for plugging in the new NIC to modify the playbook each time for the playbook.

There's a lot to say here. First, try to avoid the lineinfile like the plague. This is actually a last-resort solution. lineinfile makes compelling and idempotents difficult to write playbooks.

Now, since you want to pop RH style interface files, this is very easy to do.

Organize Your Variables

The first thing is that there should be proper structure for your variable. You want a loop on your interface, so you have to make the stuff 'loop' interface 1 , interface 2 ... interface n Not to mention.

Here's a suggestion:

  interfaces_ipv4: - name: eth0 bootprooto: static ipaddress: 192.168.211.249 netmask: 255.255.255.0 gateway: 192.168.211.2 DNS: 192.168. 211.2 - Name: eth2 bootproof: static ipadad: 10.0.0.100 Netmask: 255.0.0.0  

Write your template

Now that you have your own If you have data, you need a template to create your OS config file. BOOTPROTO = {{item.bootproto}} IPADRR = {{item.ipaddress}} NETMASK = {{item.netmask}} {% if item.gateway is defined} Gateway = {{Item.gateway}} {% endif%} PEERDNS = No DNS1 = {{item.dns}} ONBOOT = {{item.onboot | Default ('no')}}

I've included two variations: when it is not set (not creating {% if ...%} ) Or provide the default value (for example {{item.onboot | default} ('no')}} ).

If you want to use the default or if you want to leave it with construction, then your profit can be. Finally, there is such a task that will create interface configuration files for each interface:

  - Name: Enter template template: src = / path / to / the / above / Template.j2 dest = / etc / sysconfig / network-script / ifcfg - {{item.name}}. Cfg with_items: - "{{interfaces_ipv4}}"  

All this should do.

Of course, the best way to use this task is to add it to the "network" role, and call it from a playbook.

Good luck.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -