Create dynamic inventory for OpenStack

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?

You say “I suspect the “with_dict” calls because without this it works, weiird!”. What are you saying “works”?

The registered var from your os_server task will have the form http://docs.ansible.com/ansible/latest/playbooks_loops.html#iterating-over-the-results-of-a-program-execution . I don’t think you want to use with_dict here.