i am trying to write an extra line into my /etc/ansible/hosts file using an ansible playbook. Sadly I can not find an regex that matches my problem.
Here is my /etc/ansible/hosts file (example):
all:
hosts:
localhost:
ansible_user: admin
children:
ABC:
children:
abc_D1:
hosts:
abc_D2:
hosts:
DAC:
children:
I am trying to add and new line here:
all:
hosts:
localhost:
ansible_user: admin
children:
ABC:
children:
abc_D1:
hosts:
**THIS IS MY NEW LINE**
abc_D2:
hosts:
DAC:
children:
I can not figure out what kind of regular expression I need. This is my current playbook:
name: Insert comment in YAML file
hosts: localhost
connection: local
vars:
inventory_file: /etc/ansible/hosts
tasks:
- name: Ensure hosts section exists under abc_D1
lineinfile:
path: "{{ inventory_file }}"
line: " hosts:"
insertafter: ' abc_D1:'
- name: Insert new Host
lineinfile:
path: "{{ inventory_file }}"
line: "THIS IS MY NEW LINE"
insertafter: $' abc_D1:n hosts:'
my regex from insertafter is working when used in cat /etc/ansible/hosts | grep $’ abc_D1:n hosts:’ , but not in my ansible playbook.
Any ideas?