On 08/08/2022 14:19, farrukh ahmed wrote:
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
```
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
```
- 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"
```
[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
```
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"
```
[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
         Racke

--
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 
[email protected] 
<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/37833105-7d83-4b60-a5d5-2d90d4366b3en%40googlegroups.com
 
<https://groups.google.com/d/msgid/ansible-project/37833105-7d83-4b60-a5d5-2d90d4366b3en%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper

--
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e745abd2-9a69-64da-4df0-02488ea21d35%40linuxia.de.

Reply via email to