Is there a better way to iterate through multiple files on the node machine using Ansible playbook and search n replace a particular line.
I have the following files in my directory and it needs to iterate through these files and check and replace a particular line in the file.
/opt/a1.conf
/opt/a2.con.f
/var/app1/conf/a3.conf
/etc/a5.conf
/etc/a6.conf
/etc/a7.conf
/etc/a8.conf
/etc/a9.conf
My Ansible Playbook can be formatted as follows:
-
name: Install nginx and other binaries using with_item and variables.
gather_facts: yes
hosts: aws1
become_method: sudo
become: yes
tasks:
- name: Modify line to include Timeout
become: yes
become_method: sudo
lineinfile:
path: {{ item }}
regexp: 'http\s+Timeout\s+\='
line: 'http Timeout = 10'
backup: yes
with-items
- /opt/a1.conf
- /opt/a2.con.f
- /var/app1/conf/a3.conf
- /etc/a5.conf
- /etc/a6.conf
- /etc/a7.conf
- /etc/a8.conf
- /etc/a9.conf
This will actually work and help me. I could also create a vars.yaml file and add all these files and use them in "with_items" syntax. However this is actually making the playbook look to lengthy as the number of files to search are higher
We possibly can achieve the same thing effectively by using the jinja2 template using the "for" loop. EX: {% for item in vars.yml %}
and that would rather be an effective way to do it and wont make my Ansible playbook clumsy but I'm unable to figure the exact command for looping it.
Is there a jinja command to achieve the same or a better way to iterate through multiple files and not writing each of them into the playbook.
Thank you