Ansible - loop through multiple files when with_items defined

34 views
Skip to first unread message

John Harmon

unread,
Jan 29, 2018, 1:07:24 PM1/29/18
to Ansible Project
This is similar to another question I posed a few days ago, but different enough I thought it deserved its own topic.  I have a variable of ifcfg-list.stdout_lines that contains a list of ifcfg-* file names (exlcuding loopback).  I need to iterate the following code over this list (apply it to all ifcfg-* files), but don't know how to do it.  with_nested doesn't seem to be the right solution to me.  I know I can do this in multiple tasks (already done), but I am trying to consolidate all of the tasks down to a single task:

- name: Gather list of ifcfg-* files
  shell
: ls "{{ net_path }}" | grep ^ifcfg- | grep 0$ | grep -ve ifcfg-lo -e \@ # \@ excludes ansible backup files
 
register: ifcfg_list
  changed_when
: false

- name: Update ifcfg-* files
  ini_file
:
    path
: "{{ net_path }}{{ ifcfg_list.stdout_lines }}" #<-- I know this is wrong, but I need to iterate through this list and apply the changes to all files.
    no_extra_spaces
: true
    section
: null
    state
: present
    option
: "{{ item.option }}"
    value
: "{{ item.value }}"
  with_items
:
   
- { option: "NM_CONTROLLED", value: "no" }
   
- { option: "PEERDNS", value: "no" }
   
- { option: "DNS1", value: "{{ dns1 }}" }
   
- { option: "DNS2", value: "{{ dns2 }}" }
  notify
:
   
- Networking


Uwe Sauter

unread,
Jan 29, 2018, 1:11:48 PM1/29/18
to ansible...@googlegroups.com
What's wrong with "with_nested"?

#####
- name: Update ifcfg-* files
ini_file:
path: "{{ net_path }}{{ item[1] }}"
no_extra_spaces: true
section: null
state: present
option: "{{ item[0].option }}"
value: "{{ item[0].value }}"
with_nested:
- [{ option: "NM_CONTROLLED", value: "no" }, { option: "PEERDNS", value: "no" }, { option: "DNS1", value: "{{ dns1
}}" }, { option: "DNS2", value: "{{ dns2 }}" }]
- '{{ ifcfg_list.stdout_lines }}'
notify:
- Networking
#####

Am 29.01.2018 um 19:07 schrieb John Harmon:
> |
> -name:Gatherlist of ifcfg-*files
>   shell:ls "{{ net_path }}"|grep ^ifcfg-|grep 0$ |grep -ve ifcfg-lo -e \@# \@ excludes ansible backup files

John Harmon

unread,
Jan 29, 2018, 1:14:26 PM1/29/18
to Ansible Project
Nothing I guess, other than I didn't know how to use it in this scenario.  Thank you for your example, I will go and work with it.

John Harmon

unread,
Jan 29, 2018, 1:32:19 PM1/29/18
to Ansible Project

Uwe Sauter,

It worked perfectly!  Thank you.

Uwe Sauter

unread,
Jan 29, 2018, 1:33:17 PM1/29/18
to ansible...@googlegroups.com
You're welcome.

Am 29.01.2018 um 19:32 schrieb John Harmon:
>
> Uwe Sauter,
>
> It worked perfectly!  Thank you.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/c7193194-0865-4cf2-829c-c6a80af1a4e5%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/c7193194-0865-4cf2-829c-c6a80af1a4e5%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages