I have a file /home/ec2-user/hosts in which there is a group called [sample_word_test] with the square brackets and I want to add an Ip address under this group using ansible playbook. I want to match [sample_word_test] using lineinfile regex expression and add Ip address under that matching group
Following is my playbook code
lineinfile:
path: /home/ec2-user/hosts
regexp: "\[.*?sample_word_test.*$\]"
line: "{{ new_server_ip }}"
backup: yes
The very simple solution is to use ini_file. The task below does what is requested.
- ini_file:
path: /home/ec2-user/hosts
section: sample_word_test
option: "{{ new_server_ip }}"
allow_no_value: yes
backup: yes
For example
$ cat hosts
[all]
test1
test2
[sample_word_test]
192.168.1.99
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With