How to add dynamic inventory to Ansible hosts file?

asked 2017-09-02 06:02:32 -0600

Sofiane68 gravatar image

Hi,

I use Ansible to create servers on OpenStack and to install mysql on it. All is done once, so in my code I want to register the public IP of the new server and store it in my Ansible hosts file. But it does not seem to work:

- name: launch an instance
  os_server:
    state: present
    auth:
      auth_url: "{{ item.value.auth_url }}"
      username: "{{ item.value.username }}"
      password: "{{ item.value.password }}"
      project_name: "{{ item.value.project_name }}"
    region_name: "{{ item.value.region_name }}"
    name: "{{ item.value.name }}"
    image: "{{ item.value.image }}"
    state: present
    network: "{{ item.value.network }}"
    flavor: "{{ item.value.flavor }}"
    key_name: "{{ item.value.key_name }}"
    user_data: "{{ item.value.user_data }}"
  with_dict: "{{ openstack_hosts }}"
  register: mysqlserver



#- set_fact: public_v4="{{ mysqlserver.server.public_v4 }}"
- name: output of the registred variable
  debug:
    var: "{{ item.key }}"
  with_dict: "{{ openstack_hosts }}"

If I make a debug of "mysqlserver" variable it shows me a big JSON output where I can see the public IP address but "mysqlserver.server.public_v4" doest not output anything.

I suspect the "with_dict" calls because without this it works, weiird! I need "with_dict" because I want to have many servers.

Any ideas please?

edit retag flag offensive close merge delete