- name: Add DNS1 to ifcfg-* files if missing
lineinfile:
path: "{{ net_path }}{{ item }}"
state: present
regexp: "^DNS1"
line: "DNS1={{ dns1 }}"
with_items: "{{ ifcfg_list.stdout_lines }}"
ignore_errors: yes
notify:
- Networking
- name: Add DNS2 to ifcfg-* files if missing
lineinfile:
path: "{{ net_path }}{{ item }}"
state: present
regexp: "^DNS2"
line: "DNS2={{ dns2 }}"
with_items: "{{ ifcfg_list.stdout_lines }}"
ignore_errors: yes
notify:
- Networking
How do I incorporate:
"{{ ifcfg_list.stdout_lines }}"
That allows me to loop through all ifcfg-* files (except ifcfg-lo)
- name: Add DNS to ifcfg-* files if missing
lineinfile:
path: "{{ net_path }}{{ item.nic }}" #<--- This item is no longer reference by ifcfg_list.stdout_lines
state: present
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
with_items:
- {regexp: "^DNS1
", line: "DNS1={{ dns1 }}
", nic: "ifcfg_list.stdout_lines"} #<--- The nic section of these looks incorrect to me, and I doubt it would work
- {regexp: "
^DNS2
", line: "
DNS2={{ dns2 }}
", nic: "ifcfg_list.stdout_lines"}
ignore_errors: yes
notify:
- Networking
- name: Add DNS entries to ifcfg-* files if missing
lineinfile:
path: "{{ net_path }}{{ item[0] }}"
state: present
regexp: "{{ item[1] }}"
line: "{{ item[2] }}"
with_nested:
- ifcfg_list.stdout_lines
- [ '^DNS1', '^DNS2' ]
- [ 'DNS1={{ dns1 }}', 'DNS2={{ dns2 }}' ]
failed: [ansible-oel6] (item=[u'ifcfg_list.stdout_lines', u'^DNS1', u'DNS1=10.253.1.23']) => {"changed": false, "item": ["ifcfg_list.stdout_lines", "^DNS1", "DNS1=10.253.1.23"], "msg": "Destination /etc/sysconfig/network-scripts/ifcfg_list.stdout_lines does not exist !", "rc": 257}
I may have fixed it... change- ifcfg_list.stdout_lines to
- "{{ ifcfg_list.stdout_lines }}". I need to go look at the results to see if they are what I expected.
TASK [dns_update : Add DNS entries to ifcfg-* files if missing] **************************************************************************************************************************************************************************
ok: [ansible-oel7] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS1=10.1.1.10'])
ok: [ansible-oel6] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS1=10.1.1.10'])
ok: [ansible-rhel6] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS1=10.1.1.10'])
ok: [ansible-rhel7] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS1=10.1.1.10'])
changed: [ansible-rhel6] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS2=10.1.1.11'])
changed: [ansible-oel7] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS2=10.1.1.11'])
changed: [ansible-oel6] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS2=10.1.1.11'])
changed: [ansible-rhel7] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS2=10.1.1.11'])
changed: [ansible-oel7] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS1=10.1.1.10'])
changed: [ansible-rhel6] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS1=10.1.1.10'])
changed: [ansible-oel6] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS1=10.1.1.10'])
changed: [ansible-rhel7] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS1=10.1.1.10'])
ok: [ansible-oel7] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS2=10.1.1.11'])
ok: [ansible-oel6] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS2=10.1.1.11'])
ok: [ansible-rhel6] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS2=10.1.1.11'])
ok: [ansible-rhel7] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS2=10.1.1.11'])
line: "{{ item[1] }}={{ vars[item[1] | lower] }}"
If you are editing these files the inifile module is much better btw. Take a look