How can I use with_items twice in a single tasks?

39 views
Skip to first unread message

dudu.c...@gmail.com

unread,
Mar 8, 2021, 9:33:04 AM3/8/21
to Ansible Project

How can I use with_items twice in a single tasks?

For example , using line-in-file , I wish to update few parameters on a file that can be found on 2 locations


- name: update file
  lineinfile:
    path: {{ item }} <-- This should describe the files
    regexp: "{{ item.from }}"
    line: "{{ item.replace_with }}"
  with_items:
     - { from: parameter1= , replace_with: parameter1=test1 }
     - { from: parameter1= , replace_with: parameter1=test2 }
  with_items:  -- > This should be for the path
     - “/opt/file1”   
     - “/opt/file2”

 

 

Vladimir Botka

unread,
Mar 8, 2021, 9:58:19 AM3/8/21
to dudu.c...@gmail.com, ansible...@googlegroups.com
On Mon, 8 Mar 2021 06:33:03 -0800 (PST)
"dudu.c...@gmail.com" <dudu.c...@gmail.com> wrote:

> - name: update file
> lineinfile:
> path: {{ item }} *<-- This should describe the files*
> regexp: "{{ item.from }}"
> line: "{{ item.replace_with }}"
> with_items:
> - { from: parameter1= , replace_with: parameter1=test1 }
> - { from: parameter1= , replace_with: parameter1=test2 }
> with_items: *-- > This should be for the path*
> - “/opt/file1”
> - “/opt/file2”

Try this

- name: update file
lineinfile:
path: "{{ item.0 }}"
regexp: "{{ item.1.from }}"
line: "{{ item.1.replace_with }}"
with_nested:
- - “/opt/file1”
- “/opt/file2”
- - {from: parameter1=, replace_with: parameter1=test1}
- {from: parameter1=, replace_with: parameter1=test2}

(not tested)

--
Vladimir Botka

Vladimir Botka

unread,
Mar 8, 2021, 10:12:09 AM3/8/21
to dudu.c...@gmail.com, ansible...@googlegroups.com
On Mon, 8 Mar 2021 06:33:03 -0800 (PST)
"dudu.c...@gmail.com" <dudu.c...@gmail.com> wrote:

- name: update file
lineinfile:
path: {{ my_path }}
regexp: "{{ item.from }}"
line: "{{ item.replace_with }}"
with_items:
- {from: parameter1= , replace_with: parameter1=test1}
- {from: parameter1= , replace_with: parameter1=test2}

This will replace the "last line found". Twice. Quoting from "regexp"
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-regexp

"The regular expression to look for in every line of the file ...
Only the last line found will be replaced."

You might want to mark the blocks and use *blockinfile* if you can't
use *template*.

--
Vladimir Botka

Chris Bidwell - NOAA Federal

unread,
Mar 8, 2021, 1:01:22 PM3/8/21
to ansible...@googlegroups.com
I think this would be your syntax:

    - name: setup directory permissions
      file:  
        path: "{{ item.path }}"
        state: "{{ item.state }}"
        mode: "{{ item.mode }}"
        owner: "{{ item.owner }}"
        group: "{{ item.group }}"
      with_items:
        - { path: "/data/gong", state: "directory", mode: '2775', owner: "conductor", group: "pipe" }
        - { path: "/data/gong/pipe", state: "directory", mode: '0775', owner: "conductor", group: "pipe" }
        - { path: "/data/gong/pipe/conductor", state: "directory", mode: '0755', owner: "conductor", group: "pipe" }
        - { path: "/opt/db", state: "directory", mode: '0775', owner: "conductor", group: "pipe" }
        - { path: "/iraf", state: "directory", mode: '0775', owner: "gong", group: "pipe" }
        - { path: "/local", state: "directory", mode: '0775', owner: "gong", group: "pipe" }
        - { path: "/data/ftps_in", state: "directory", mode: '0775', owner: "conductor", group: "pipe" }
        - { path: "/localpipe", state: "directory", mode: '0775', owner: "conductor", group: "pipe" }
        - { path: "/var/log/local0.log", state: "touch", mode: '0664', owner: 'root', group: "pipe" }
        - { path: "/chroot/files/idl", state: "directory", mode: '0775', owner: 'root', group: "root" }


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20210308155804.17e2826e%40gmail.com.


--

Chris Bidwell, CISSP
Space Weather Prediction Center
National Oceanic Atmospheric Administration
email: chris.b...@noaa.gov
office: 303-497-3204
mobile: 720-496-3126
Reply all
Reply to author
Forward
0 new messages