New to ansible -- Looping through files with wildcards

1,215 views
Skip to first unread message

John Harmon

unread,
Aug 11, 2017, 4:42:17 PM8/11/17
to Ansible Project
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


John Harmon

unread,
Aug 11, 2017, 4:59:29 PM8/11/17
to Ansible Project
Trying this (which doesn't delete the lines yet).... but the other problem is that the shell task is not idempotent

---
# 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

- 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 }}"

--------------------RESULTS------------------------

PLAY [test] ******************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
ok: [ansibletest-rhel7]
ok: [ansibletest-rhel6]
ok: [ansibletest-oel6]

TASK [dns_update : Retrieve ifcfg files in /etc/sysconfig/network-scripts] ***************************************************************************************************************************************************************
changed: [ansibletest-rhel7]
changed: [ansibletest-rhel6]
changed: [ansibletest-oel6]

TASK [dns_update : Removing all DNS entries in ifcfg-* files] ****************************************************************************************************************************************************************************
ok: [ansibletest-rhel7] => (item=ifcfg-bond0)
ok: [ansibletest-rhel6] => (item=ifcfg-bond0)
ok: [ansibletest-oel6] => (item=ifcfg-bond0)
ok: [ansibletest-rhel7] => (item=ifcfg-eth0)
ok: [ansibletest-rhel6] => (item=ifcfg-eth0)
ok: [ansibletest-oel6] => (item=ifcfg-eth0)
ok: [ansibletest-rhel7] => (item=ifcfg-eth1)
ok: [ansibletest-rhel6] => (item=ifcfg-eth1)
ok: [ansibletest-rhel7] => (item=ifcfg-eth2)
ok: [ansibletest-oel6] => (item=ifcfg-eth1)
ok: [ansibletest-rhel6] => (item=ifcfg-eth2)
ok: [ansibletest-rhel7] => (item=ifcfg-eth3)
ok: [ansibletest-oel6] => (item=ifcfg-eth2)
ok: [ansibletest-rhel7] => (item=ifcfg-eth4)
ok: [ansibletest-rhel6] => (item=ifcfg-eth3)
ok: [ansibletest-rhel7] => (item=ifcfg-eth5)
ok: [ansibletest-oel6] => (item=ifcfg-eth3)
ok: [ansibletest-rhel6] => (item=ifcfg-eth4)
ok: [ansibletest-rhel7] => (item=ifcfg-lo)
ok: [ansibletest-rhel6] => (item=ifcfg-eth5)
ok: [ansibletest-oel6] => (item=ifcfg-eth4)
ok: [ansibletest-rhel6] => (item=ifcfg-lo)
ok: [ansibletest-oel6] => (item=ifcfg-eth5)
ok: [ansibletest-oel6] => (item=ifcfg-lo)

PLAY RECAP *******************************************************************************************************************************************************************************************************************************
ansibletest-oel6           : ok=3    changed=1    unreachable=0    failed=0
ansibletest-rhel6          : ok=3    changed=1    unreachable=0    failed=0
ansibletest-rhel7          : ok=3    changed=1    unreachable=0    failed=0





Kai Stian Olstad

unread,
Aug 11, 2017, 5:43:06 PM8/11/17
to ansible...@googlegroups.com
On 11. aug. 2017 22:59, John Harmon wrote:
> 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 }}'


--
Kai Stian Olstad

John Harmon

unread,
Aug 11, 2017, 5:43:10 PM8/11/17
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages