# Lineinfile - Skip/Update an item inside "with\_items" using conditions

**URL:** <https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950>\
**Category:** Ansible Project\
**Created:** [August 5, 2022, 6:43am UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950 "2022-08-05T06:43:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![farrukh\_ahmed](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@farrukh\_ahmed](https://forum.ansible.com/u/farrukh_ahmed)\
**Post date:** [August 5, 2022, 6:43am UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/1 "2022-08-05T06:43:59Z")

</div>

I have a question.

Can an item inside “with\_items” or “loop” be skipped? using condition “when some\_variable is not defined” ?

Here is my play.

**- name: “Update text in the file”**  
**lineinfile:**  
**path: /path-to-the-file/**  
**backrefs: yes**  
**regexp: “{{ item.regexp }}”**  
**line: “{{ item.line }}”**  
**with\_items:**  
**- { regexp: ‘^text1’, line: “text1 = {{ VAR1 }}”, when: VAR1 is defined }**  
**- { regexp: ‘^text2’, line: “text2 = {{ VAR2 }}” }**  
**- { regexp: ‘^text3’, line: “text3 = {{ VAR3 }}” }**

What I’m trying to achieve is, it should replace the line only when VAR1 value is defined, else it should skip this item and move on to 2nd item.

---

<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:** [August 5, 2022, 9:17am UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/2 "2022-08-05T09:17:42Z")

</div>

> with\_items:  
> &nbsp;&nbsp;- { regexp: '^text1', line: "text1 = {{ VAR1 }}", when: VAR1 is defined }  
> &nbsp;&nbsp;- { regexp: '^text2', line: "text2 = {{ VAR2 }}" }  
> &nbsp;&nbsp;- { regexp: '^text3', line: "text3 = {{ VAR3 }}" }

Set \*default\* to avoid errors, add attribute \*def\*, and select lines.  
For example,

---

<div class="post-metadata">

**Author:** ![farrukh\_ahmed](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@farrukh\_ahmed](https://forum.ansible.com/u/farrukh_ahmed)\
**Post date:** [August 5, 2022, 11:31am UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/3 "2022-08-05T11:31:53Z")

</div>

Thanks for your reply Vladimir Botka.

This is my actual case:

defaults \> main.yml

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [August 5, 2022, 11:49am UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/4 "2022-08-05T11:49:09Z")

</div>

> Thanks for your reply Vladimir Botka.
> 
> This is my actual case:
> 
> defaults \> main.yml  
> ---  
> # defaults file for redis\_configs  
> redis\_config\_path: /etc/redis/redis.conf
> 
> # Default Params Values  
> MAX\_MEM: 3000  
> TCP\_KEEPALIVE: 0  
> TCP\_BACKLOG: 511  
> MAX\_CLIENTS: 15000  
> TIMEOUT: 1500
> 
> And this is:
> 
> Tasks \> main.yml
> 
> ---
> 
> - name: "Update Redis Parameters in {{ redis\_config\_path }}"  
> lineinfile:  
> path: "{{ redis\_config\_path }}"  
> backrefs: yes  
> regexp: "{{ item.regexp }}"  
> line: "{{ item.line }}"  
> with\_items:  
> - { regexp: '^maxmemory', line: "maxmemory {{ MAX\_MEM }}mb", when: {{ MAX\_MEM }} is defined }  
> - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP\_KEEPALIVE }}", when: {{ TCP\_KEEPALIVE }} is defined }  
> - { regexp: '^tcp-backlog', line: "tcp-backlog {{ TCP\_BACKLOG }}", when: {{ TCP\_BACKLOG }} is defined}  
> - { regexp: '^maxclients', line: "maxclients {{ MAX\_CLIENTS }}", when: {{ MAX\_CLIENTS }} is defined}  
> - { regexp: '^timeout', line: "timeout {{ TIMEOUT }}" }
> 
> I want this to check if each variable inside an item is defined then make the changes to the line in the file, otherwise skip only those items which variables is not defined.  
> For Instance, If I comment out the variable MAX\_MEM from the defaults \> main.yml. like below:
> 
> # Default Params Values  
> ##MAX\_MEM: 3000  
> TCP\_KEEPALIVE: 0  
> TCP\_BACKLOG: 511  
> MAX\_CLIENTS: 15000  
> TIMEOUT: 1500
> 
> Then the execution should skip changes to the line where MAX\_MEM is undefined, and the rest lines should be changed as they defined.
> 
> Is it possible?

Sorry, but it eludes me why you didn't come up with the actual case in the first place. I see no point in wasting everyone's time.

Regards

```
         Racke

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 5, 2022, 12:03pm UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/5 "2022-08-05T12:03:19Z")

</div>

```auto
    - name: "Update Redis Parameters in {{ redis_config_path }}"
      lineinfile:
        path: "{{ redis_config_path }}"
        backrefs: yes
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
      when: item.line is match(omit)
      with_items:
        - { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM | d(omit) }}mb" }
        - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE | d(omit) }}" }
        - { regexp: '^tcp-backlog', line: "tcp-backlog {{ TCP_BACKLOG | d(omit) }}" }
        - { regexp: '^maxclients', line: "maxclients {{ MAX_CLIENTS | d(omit) }}" }
        - { regexp: '^timeout', line: "timeout {{ TIMEOUT }}" }

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 5, 2022, 12:22pm UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/6 "2022-08-05T12:22:03Z")

</div>

That “when:” should be

when: item.line is not search(omit)

---

<div class="post-metadata">

**Author:** ![farrukh\_ahmed](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@farrukh\_ahmed](https://forum.ansible.com/u/farrukh_ahmed)\
**Post date:** [August 5, 2022, 3:27pm UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/7 "2022-08-05T15:27:04Z")

</div>

Thanks @utoddl for your time and reply.

when: item.line is not search(omit)

This solution actually worked for me.

Thanks & regards,

FARRUKH

---

<div class="post-metadata">

**Author:** ![farrukh\_ahmed](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@farrukh\_ahmed](https://forum.ansible.com/u/farrukh_ahmed)\
**Post date:** [December 20, 2022, 11:46am UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/8 "2022-12-20T11:46:46Z")

</div>

Can we do the same with blockinfile?  
For instance, if the variable is not found then omit only that item and move on to the next item and update it.

Here is my play

- name: insert/update php configurations in App\_config\_file  
blockinfile:  
path: “/etc/php/7.4/fpm/php.ini\_bak”  
marker: “;# {mark} ANSIBLE MANAGED BLOCK ###”  
block: |  
{% for items in item %}  
{{ items }}  
{% endfor %}  
when: item is not search(omit)  
with\_items:
- { “display\_errors = {{service\_configurations.display\_errors | d(omit) }}” }
- { “memory\_limit = {{service\_configurations.memory\_limit | d(omit) }}M” }

Here is the output I’m getting:

root@aws-farrukhahmed-518:/etc/php/7.4/fpm# cat php.ini\_bak

;# BEGIN ANSIBLE MANAGED BLOCK ###  
memory\_limit = {{service\_configurations.memory\_limit | d(omit) }}M  
;# END ANSIBLE MANAGED BLOCK ###

root@aws-farrukhahmed-518:/etc/php/7.4/fpm#

Here is the command I’m using:  
ansible-playbook -i hosts exec.yml -e ‘{“service\_configurations”:{“display\_errors”:“On”,“memory\_limit”:“128”}}’ -vvv

The expected output should be:

;# BEGIN ANSIBLE MANAGED BLOCK ###  
display\_errors = On  
memory\_limit = 128M  
;# END ANSIBLE MANAGED BLOCK ###

PS: It is mandatory for me to do this with the blockinfile module.

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [December 20, 2022, 12:14pm UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/9 "2022-12-20T12:14:41Z")

</div>

Hii

> Can we do the same with blockinfile?

Maybe. But, what one can do, is not always what one should do - see below.

> PS: It is mandatory for me to do this with the blockinfile module.

This is not correct - the file should not be edited at all, because it  
is a package provided file.  
So, any updates will overwrite your changes. Plus you need to resort  
to kludges (imho) like blockinfile.  
What you should do, is put your local changes in a properly named ini  
file below /etc/php/7.4/fpm/conf.d, which won't be touched by anyone  
else.

Dick

---

<div class="post-metadata">

**Author:** ![farrukh\_ahmed](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@farrukh\_ahmed](https://forum.ansible.com/u/farrukh_ahmed)\
**Post date:** [December 21, 2022, 11:30am UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/10 "2022-12-21T11:30:03Z")

</div>

Hello Team,

I have progressed but still not achieved my target with **blockinfile** module.  
It should be updated the both items, as I passed both variables but It just updated the last item only i.e (memory\_limit)

Here is my play:

- name: update php configurations  
blockinfile:  
path: “{{ini\_file\_path}}” # set in defaults  
marker: “;# {mark} ANSIBLE MANAGED BLOCK ###”  
block: “{{ item.param }}”  
loop:
- { param: “display\_errors = {{service\_configurations.display\_errors | d(omit) }}”}
- { param: “memory\_limit = {{service\_configurations.memory\_limit | d(omit) }}M”}  
when: item.param is not search(omit)

The output I’m getting:

;# BEGIN ANSIBLE MANAGED BLOCK ###  
memory\_limit = 128M  
;# END ANSIBLE MANAGED BLOCK ###

The output should be like this, if I passed both variables from the command line.

;# BEGIN ANSIBLE MANAGED BLOCK ###  
display\_errors = On  
memory\_limit = 128M  
;# END ANSIBLE MANAGED BLOCK ###

The Command I’m using:  
ansible-playbook -i hosts exec.yml -e ‘{“service\_configurations”:{“display\_errors”:“On”,“memory\_limit”:“128”}}’

I would be thankful for any help or ideas.

Thanks.  
FARRUKH AHMED

---

<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:** [December 21, 2022, 3:14pm UTC](https://forum.ansible.com/t/lineinfile-skip-update-an-item-inside-with-items-using-conditions/36950/11 "2022-12-21T15:14:21Z")

</div>

> blockinfile:  
> path: "{{ini\_file\_path}}" # set in defaults  
> marker: ";# {mark} ANSIBLE MANAGED BLOCK ###"  
> block: "{{ item.param }}"  
> loop:  
> - { param: "display\_errors = {{service\_configurations.display\_errors |  
> d(omit) }}"}  
> - { param: "memory\_limit = {{service\_configurations.memory\_limit | d(omit)  
> }}M"}  
> when: item.param is not search(omit)

The \*marker\* must be unique for each block. Otherwise the the blocks  
will overwrite each other. For this purpose it would be practical to  
change the structure of the iterated data. For example, the  
simplified playbook below

&nbsp;&nbsp;\> cat pb.yml  
&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- blockinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /tmp/test.ini  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;marker: "# {mark} {{ item.param }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.param }} = {{ item.value }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- param: 'display\_errors'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: "{{ conf.errors }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- param: 'memory\_limit'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: "{{ conf.limit }}"

> ansible-playbook pb.yml -e '{"conf":{"errors":"On","limit":"128"}}'

works as expected and creates the file

&nbsp;&nbsp;\> cat /tmp/test.ini  
&nbsp;&nbsp;# BEGIN display\_errors  
&nbsp;&nbsp;display\_errors = On  
&nbsp;&nbsp;# END display\_errors  
&nbsp;&nbsp;# BEGIN memory\_limit  
&nbsp;&nbsp;memory\_limit = 128  
&nbsp;&nbsp;# END memory\_limit

Fit the data and \*marker\* to your needs.
