# How can I use with\_items twice in a single tasks?

**URL:** <https://forum.ansible.com/t/how-can-i-use-with-items-twice-in-a-single-tasks/34739>\
**Category:** Ansible Project\
**Created:** [March 8, 2021, 2:33pm UTC](https://forum.ansible.com/t/how-can-i-use-with-items-twice-in-a-single-tasks/34739 "2021-03-08T14:33:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [March 8, 2021, 2:33pm UTC](https://forum.ansible.com/t/how-can-i-use-with-items-twice-in-a-single-tasks/34739/1 "2021-03-08T14:33:03Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 8, 2021, 2:58pm UTC](https://forum.ansible.com/t/how-can-i-use-with-items-twice-in-a-single-tasks/34739/2 "2021-03-08T14:58:04Z")

</div>

Try this

- name: update file  
&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item.0 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: "{{ item.1.from }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "{{ item.1.replace\_with }}"  
&nbsp;&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- - “/opt/file1”  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- “/opt/file2”  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- - {from: parameter1=, replace\_with: parameter1=test1}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {from: parameter1=, replace\_with: parameter1=test2}

(not tested)

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 8, 2021, 3:11pm UTC](https://forum.ansible.com/t/how-can-i-use-with-items-twice-in-a-single-tasks/34739/3 "2021-03-08T15:11:53Z")

</div>

- name: update file  
&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: {{ my\_path }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: "{{ item.from }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "{{ item.replace\_with }}"  
&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {from: parameter1= , replace\_with: parameter1=test1}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {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](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-regexp)

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

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

---

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [March 8, 2021, 6:01pm UTC](https://forum.ansible.com/t/how-can-i-use-with-items-twice-in-a-single-tasks/34739/4 "2021-03-08T18:01:00Z")

</div>

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