How can I use with_items twice in a single tasks?

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”

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)

- 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*.

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