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?