# Lineinfile - Remove line if the regexp match but the variable inside item is not defined.

**URL:** <https://forum.ansible.com/t/lineinfile-remove-line-if-the-regexp-match-but-the-variable-inside-item-is-not-defined/36961>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [August 8, 2022, 12:19pm UTC](https://forum.ansible.com/t/lineinfile-remove-line-if-the-regexp-match-but-the-variable-inside-item-is-not-defined/36961 "2022-08-08T12:19:15Z")\
**Posts on this page:** 2\
**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 8, 2022, 12:19pm UTC](https://forum.ansible.com/t/lineinfile-remove-line-if-the-regexp-match-but-the-variable-inside-item-is-not-defined/36961/1 "2022-08-08T12:19:15Z")

</div>

Hello Team,

Hope you all are doing well.

I’m stuck at lineinfile module.

how to check if a variable inside each item is defined then add the line in the file, otherwise if the regex found the match in the remote host file but the variable inside an item is not defined then remove only that regex matched line from the file. And continue to the next item (should not terminate the module execution and continue to next item if variable is not defined).

CASE:

### Here is my defaults \> main.yml

```auto
command: /home/farrukh/idle.sh
autostart: true
autorestart: true
stderr_logfile: /var/log/idle.err.log
stdout_logfile: /var/log/idle.out.log
numprocs: 2

```

### Here is tasks \> main.yml

```auto
- name: "Update Job Parameters in .conf file"
lineinfile:
path: "{{ supervisor_config_path }}/job_{{ job_name }}.conf"
backrefs: yes
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
when: item.line is not search(omit)
with_items:
- { regexp: '^command', line: "command={{ command | d(omit) }}M" }
- { regexp: '^autostart', line: "autostart={{ autostart | d(omit) }}" }
- { regexp: '^autorestart', line: "autorestart={{ autorestart | d(omit) }}M" }
- { regexp: '^stderr_logfile', line: "stderr_logfile={{ stderr_logfile | d(omit) }}" }
- { regexp: '^stdout_logfile', line: "stdout_logfile={{ stdout_logfile | d(omit) }}" }
- { regexp: '^numprocs', line: "numprocs={{ numprocs | d(omit) }}" }

```

### Here is the file on the remote host “job\_idle.conf”

```auto
[program:job_idle]
command=/home/ubuntu/idle.sh
autostart=true
autorestart=true
stderr_logfile=/var/log/idle.err.log
stdout_logfile=/var/log/idle.out.log

```

Now, for instance, If I comment out the variable “autorestart” from the defaults \> main.yml. like below:

### defaults \> main.yml

```auto
command: /home/farrukh/idle.sh
autostart: true
#autorestart: true
stderr_logfile: /var/log/idle.err.log
stdout_logfile: /var/log/idle.out.log
numprocs: 2

```

Then running playbook, should get the desired result of job\_idle.conf like below:

## The file on the remote host “job\_idle.conf”

```auto
[program:job_idle]
command=/home/ubuntu/idle.sh
autostart=true
stderr_logfile=/var/log/idle.err.log
stdout_logfile=/var/log/idle.out.log

```

It should remove the line autorestart. As the variable “autorestart” was commented out from defaults \> main.yml and not defined.

It is this case achievable with this module?

---

<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 8, 2022, 12:26pm UTC](https://forum.ansible.com/t/lineinfile-remove-line-if-the-regexp-match-but-the-variable-inside-item-is-not-defined/36961/2 "2022-08-08T12:26:31Z")

</div>

> Hello Team,
> 
> Hope you all are doing well.
> 
> I'm stuck at lineinfile module.
> 
> how to check if a variable inside each item is defined then add the line in the file, otherwise if the regex found the match in the remote host file but the variable inside an item is not defined then remove only that regex matched line from the file. And continue to the next item (should not terminate the module execution and continue to next item if variable is not defined).
> 
> CASE:
> 
> ### Here is my defaults \> main.yml
> 
> ```auto
> command: /home/farrukh/idle.sh
> autostart: true
> autorestart: true
> stderr_logfile: /var/log/idle.err.log
> stdout_logfile: /var/log/idle.out.log
> numprocs: 2
> 
> ```
> 
> ### Here is tasks \> main.yml
> 
> ```auto
> - name: "Update Job Parameters in .conf file"
> lineinfile:
> path: "{{ supervisor_config_path }}/job_{{ job_name }}.conf"
> backrefs: yes
> regexp: "{{ item.regexp }}"
> line: "{{ item.line }}"
> when: item.line is not search(omit)
> with_items:
> - { regexp: '^command', line: "command={{ command | d(omit) }}M" }
> - { regexp: '^autostart', line: "autostart={{ autostart | d(omit) }}" }
> - { regexp: '^autorestart', line: "autorestart={{ autorestart | d(omit) }}M" }
> - { regexp: '^stderr_logfile', line: "stderr_logfile={{ stderr_logfile | d(omit) }}" }
> - { regexp: '^stdout_logfile', line: "stdout_logfile={{ stdout_logfile | d(omit) }}" }
> - { regexp: '^numprocs', line: "numprocs={{ numprocs | d(omit) }}" }
> 
> ```
> 
> ### Here is the file on the remote host "job\_idle.conf"
> 
> ```auto
> [program:job_idle]
> command=/home/ubuntu/idle.sh
> autostart=true
> autorestart=true
> stderr_logfile=/var/log/idle.err.log
> stdout_logfile=/var/log/idle.out.log
> 
> ```
> 
> Now, for instance, If I comment out the variable "autorestart" from the defaults \> main.yml. like below:
> 
> ### defaults \> main.yml
> 
> ```auto
> command: /home/farrukh/idle.sh
> autostart: true
> #autorestart: true
> stderr_logfile: /var/log/idle.err.log
> stdout_logfile: /var/log/idle.out.log
> numprocs: 2
> 
> ```
> 
> Then running playbook, should get the desired result of job\_idle.conf like below:  
> ## The file on the remote host "job\_idle.conf"
> 
> ```auto
> [program:job_idle]
> command=/home/ubuntu/idle.sh
> autostart=true
> stderr_logfile=/var/log/idle.err.log
> stdout_logfile=/var/log/idle.out.log
> 
> ```
> 
> It should remove the line autorestart. As the variable "autorestart" was commented out from defaults \> main.yml and not defined.
> 
> It is this case achievable with this module?

Looking at your output file it is a real candidate for the template module. lineinfile is a bit tricky, but it can be useful to replace  
a few lines in a larger file.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
