# How would you approach this?  Modifying DNS entries in ifcfg-\* files

**URL:** <https://forum.ansible.com/t/how-would-you-approach-this-modifying-dns-entries-in-ifcfg-files/25345>\
**Category:** Ansible Project\
**Created:** [August 28, 2017, 4:52pm UTC](https://forum.ansible.com/t/how-would-you-approach-this-modifying-dns-entries-in-ifcfg-files/25345 "2017-08-28T16:52:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [August 28, 2017, 4:52pm UTC](https://forum.ansible.com/t/how-would-you-approach-this-modifying-dns-entries-in-ifcfg-files/25345/1 "2017-08-28T16:52:23Z")

</div>

I have been working on this for days. I don’t mind because I am learning a ton of stuff (as I am new to ansible), but the only way I can get this to work properly is to use a shell: module (utilizing sed). I would prefer to avoid that if I can.

Tasks:  
Gather list of ifcfg-\* files (except ifcfg-lo)  
**Add DNS entries if missing; otherwise replace DNS entries if they don’t match or**

`

- name: Gather list of ifcfg-\* files  
shell: ls “{{ net\_path }}” | grep ^ifcfg- | grep -ve ifcfg-lo -e @ # @ excludes ansible backup files  
register: ifcfg\_list  
changed\_when: false  
`

The above works fine… it is the second task that is getting me.

Using the above I have tried all sorts of things. I have learned that lineinfile regexp only matches the last found match, replace matches all. blockinfile also appears to only to affect the last found match.

Anyway, how would you approach this without shell:?

As an example:  
I wish to use the following vars as my dns entries:  
dns1=10.10.10.10  
dns2=20.20.20.20

ifcfg-eth0 contains:  
DNS1=1.2.3.4  
DNS2=2.3.4.5

ifcfg-eth1 doesn’t contain any dns entries

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [August 28, 2017, 7:06pm UTC](https://forum.ansible.com/t/how-would-you-approach-this-modifying-dns-entries-in-ifcfg-files/25345/2 "2017-08-28T19:06:54Z")

</div>

Hmmm… I am trying it this way… and am really close. I just don’t know how to properly reference variables in a regexp statement within ansible:

`

- name: TEST  
replace:  
path: “{{ net\_path }}{{ item }}”  
regexp: “^DNS(?!._(?:10.10.10.1[12]))._”  
with\_items: “{{ ifcfg\_list.stdout\_lines }}”  
`

I want to replace 10.10.10.1[12] with the variables dns1 and dns2

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [August 28, 2017, 7:13pm UTC](https://forum.ansible.com/t/how-would-you-approach-this-modifying-dns-entries-in-ifcfg-files/25345/3 "2017-08-28T19:13:34Z")

</div>

Got it… but if someone has a better way of doing this, I am all ears. I will add dns through the lineinfile module

`  
Enter code here…- name: TEST  
replace:  
path: “{{ net\_path }}{{ item }}”  
regexp: “^DNS(?!._(?:{{ dns1 }}|{{ dns2 }}))._”  
with\_items: “{{ ifcfg\_list.stdout\_lines }}”

`

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [August 28, 2017, 7:15pm UTC](https://forum.ansible.com/t/how-would-you-approach-this-modifying-dns-entries-in-ifcfg-files/25345/4 "2017-08-28T19:15:32Z")

</div>

I have a template for ifcfg-\* files where I not only fill in DNS entries but also all the other network configuration  
stuff. I put this into a role and all variables are in host\_vars/\*.yaml files. Every time I wish to change the  
configuration I change the host\_vars file and reapply the role (which creates a new ifcfg file from the template with  
the new settings).

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [August 28, 2017, 7:18pm UTC](https://forum.ansible.com/t/how-would-you-approach-this-modifying-dns-entries-in-ifcfg-files/25345/5 "2017-08-28T19:18:35Z")

</div>

Yes that sounds very close to what I am trying to do. I can see how doing that with a template and a host\_vars file would simplify it. Thank you for your input
