RoseQuartz
RoseQuartz

Reputation: 33

Ansible loop over inventory file (two dimensional list)

I would like to parse a two dimensional list from the inventory file with ansible playbook

Inventory file: .ini would have a list of macs and IPs

mac1=b8:27:eb:12:53:1b ip1=192.168.8.101
mac2=b8:27:eb:f1:65:32 ip2=192.168.8.102
...

and the ansible task would be to add a line everytime in the `/etc/ethers``file in this form

b8:27:eb:f1:65:32 192.168.8.102

this is the task

- name: Assign static IPs to MACs
  lineinfile:
    path: /etc/ethers
    line: "{{  mac  }} {{  ip  }}"
    mode: 0644
  loop: "{{ listname }}"
  become: yes

Any recommendations please on how to set my list in the inventory that it will work with the playbook? Thank you!

Upvotes: 0

Views: 666

Answers (1)

Frode Nilsen
Frode Nilsen

Reputation: 26

I would add the list as a variable in group_var folder or in your playbook.

list:
  - mac: b8:27:eb:12:53:1b
    ip: 192.168.8.101
  - mac: b8:27:eb:f1:65:32
    ip: 192.168.8.102

Your task can then look something like this:

- name: Assign static IPs to MACs
  lineinfile:
    path: /etc/ethers
    line: "{{  item.mac  }} {{  item.ip  }}"
    mode: 0644
  loop: "{{ list }}"
  become: yes

Upvotes: 1

Related Questions