New to ansible -- Looping through files with wildcards

Hi all,

I am new to ansible and am looking for some help. I don’t mind finding the answer on my own; however, I could use a shove in the right direction.

I need to modify some files (/etc/sysconfig/network-scripts/ifcfg-* files). I was trying to use with_fileglob (among other options), but I read somewhere that wont work ( https://stackoverflow.com/questions/29831690/ansible-with-fileglob-not-matching-files-with-only-wildcard )

Basically, I am looking to run a loop against all ifcfg-* files. I want to modify some entries (which I can do individually) and delete some entries (which I can also do individually), I just don’t know how to approach looping through all of the files. I am even unsure if fileglob is the right approach. I would appreciate some education on the matter. How would you approach it?

Thanks,
john

Trying this (which doesn’t delete the lines yet)… but the other problem is that the shell task is not idempotent

Trying this (which doesn't delete the lines yet).... but the other problem
is that the shell task is not idempotent

What do you mean by "is not idempotent" it's just listing files and it need to do that every time.

---
# tasks file for dns_update
- name: Retrieve ifcfg files in /etc/sysconfig/network-scripts
   shell: ls /etc/sysconfig/network-scripts | grep ^ifcfg-
   register: path_files

Alternative is the find module
- name: Retrieve ifcfg files in /etc/sysconfig/network-scripts
   find:
     paths: ls /etc/sysconfig/network-scripts
     pattern: ifcfg-*
   register: path_files

- name: Removing all DNS entries in ifcfg-* files
   lineinfile:
     path: /etc/sysconfig/network-scripts/{{ path_files.stdout_lines }}
     state: absent
     regexp: '^DNS'
   with_items: "{{ path_files.stdout_lines }}"

The value from with_items i in a variable called item so the path sould be
   path: /etc/sysconfig/network-scripts/{{ item }}

With the find module above this task should look like this
- name: Removing all DNS entries in ifcfg-* files
    lineinfile:
      path: '{{ item.path }}'
      state: absent
      regexp: '^DNS'
    with_items: '{{ path_files.files }}'

Forgive me for responding to myself… I just hope that someone in the future might find this relevant/useful.

added “changed_when: false” to the shell task. That took care of the idempotent issue I was seeing by overriding the change result.

Still plugging away on the other issue

  • name: Disabling NetworkManager in ifcfg-* files
    lineinfile:
    path: /etc/sysconfig/network-scripts/{{path_files.stdout_lines}}
    regexp: ‘^NM_CONTROLLED’
    line: “NM_CONTROLLED=no”

The above is now returning:
TASK [dns_update : Disabling NetworkManager in ifcfg-* files] ****************************************************************************************************************************************************************************
fatal: [ansibletest-rhel7]: FAILED! => {“changed”: false, “failed”: true, “msg”: “Destination /etc/sysconfig/network-scripts/[u’ifcfg-bond0’, u’ifcfg-eth0’, u’ifcfg-eth1’, u’ifcfg-eth2’, u’ifcfg-eth3’, u’ifcfg-eth4’, u’ifcfg-eth5’, u’ifcfg-lo’] does not exist !”, “rc”: 257}
fatal: [ansibletest-rhel6]: FAILED! => {“changed”: false, “failed”: true, “msg”: “Destination /etc/sysconfig/network-scripts/[u’ifcfg-bond0’, u’ifcfg-eth0’, u’ifcfg-eth1’, u’ifcfg-eth2’, u’ifcfg-eth3’, u’ifcfg-eth4’, u’ifcfg-eth5’, u’ifcfg-lo’] does not exist !”, “rc”: 257}
fatal: [ansibletest-oel6]: FAILED! => {“changed”: false, “failed”: true, “msg”: “Destination /etc/sysconfig/network-scripts/[u’ifcfg-bond0’, u’ifcfg-eth0’, u’ifcfg-eth1’, u’ifcfg-eth2’, u’ifcfg-eth3’, u’ifcfg-eth4’, u’ifcfg-eth5’, u’ifcfg-lo’] does not exist !”, “rc”: 257}

Notice the ifcfg* names are messed up. I am trying to narrow down the reason