Here is the example playbook to ensure file /tmp/testfile has PROFILE set to my_new_profile and OTHER set to my_new_other on both server1 and server2:
- hosts: server1, server2
tasks:
- lineinfile: dest=/tmp/testfile regexp={{item.regex}} line={{item.replace_with}}
with_items:
- regex: ^PROFILE=
replace_with: PROFILE=my_new_profile
- regex: ^OTHER=
replace_with: OTHER=my_new_other
Using dynamic group and nested loop to create two files: testfile_host1 and testfile_host2 with lines present on all servers in my_dynamic_group group created earlier in playbook.
- hosts: localhost
tasks:
- add_host: name={{ item }} groups=my_dynamic_group
with_items:
- server1
- server2
- hosts: my_dynamic_group
tasks:
- lineinfile: dest=/tmp/testfile_{{ item[0] }} regexp={{item[1].regex}} line={{item[1].replace_with}} create=yes
with_nested:
- [ 'host1', 'host2' ]
- [
{ regex: '^PROFILE=', replace_with: 'PROFILE=my_new_profile' },
{ regex: '^OTHER=', replace_with: 'OTHER=my_new_other' }
]