I have a playbook with is creating vms and then i want to add the ip address to the inventory file to when it comes time to provision i can just call the updated inventory file. lineinfile will only add to the found lines at the end of the file. replace will update all the matches. What is the recommended way to do this?
===playbook===
---
- name: create instances
hosts: all
gather_facts: no
connection: local
sudo: no
tasks:
- name: "create instance and assign floating-ip (if available)"
os_server:
state: present
auth:
username: "{{openstack_username | mandatory }}"
password: "{{openstack_password | mandatory }}"
project_name: cups
name: "{{ inventory_hostname | mandatory }}"
image: "{{ openstack_image | mandatory }}"
key_name: "{{ openstack_key_name | mandatory }}"
flavor: "{{ openstack_flavor | mandatory }}"
register: result
- debug: var=result
- debug: var=inventory_hostname
- lineinfile: backrefs=yes state=present dest="{{inventory_dir}}/test" regexp='{{inventory_hostname}}\s' line='{{inventory_hostname}} ansible_ssh_host={{result.openstack.accessIPv4}}'
===inventory file====
[tomcat]
tomcat
[jenkins]
jenkins
[downloads]
downloads
[project:children]
tomcat
jenkins
downloads
[cups:vars]
ansible_ssh_user=centos