Kevin-Ian Giermann
Kevin-Ian Giermann

Reputation: 65

Ansible Return Value - Need IP Adress Part 2

the question referes to the topic here: Ansible Return Value - Need IP Adress

my answer get deleted so they told me i need to open a new topic for it ... dunno why but nevermind.

so my question refers to the answere of Eric Citaire:

thanks for your quick response, i tryed it and i was a little bit confused about the output.

so here is my file:

 - os_server:
  state: present
   flavor: m1.nano
   auth:
     auth_url: ****
     username: ****
     password: ****
     project_name: admin
     domain_id: ****
   name: ansibletest
   region_name: RegionOne   
   image: 4e7ab5c8-4b39-4c77-b68d-cf2ea7e1df1a
   key_name: ansible
   timeout: 200
   nics:
     - net-id: a5a73ab9-3ee5-49a6-bea0-f44f9e376ca0
   auto_ip: yes  
   register: result    
- debug: var=result

and i get the following output:

>root@ansible1:~/HP# ansible-playbook -i Inventory playbook.yml

>PLAY ***************************************************************************

>TASK [setup] ******************************************************************* ok: [172.20.22.21]

>TASK [openstack : os_server] *************************************************** fatal: [172.20.22.21]: FAILED! => {"changed": false, "failed": true, "msg": "unsupported parameter for module: register"}

>PLAY RECAP ********************************************************************* 172.20.22.21 : ok=1 changed=0 unreachable=0 failed=1

can you help me ? PS: **** means there is content you shoudl better not see ^^

EDIT FROM 27.07.2017:

So i got this working now, the result is now in "var" and i see an output. First of all here is the Updated Playbook:

    - name: create a server
  os_server:
    state: present
    flavor: m1.nano
    auth:
      auth_url: ****
      username: ****
      password: ****
      project_name: admin
      domain_id: ****
    name: ansibletest
#   region_name: RegionOne
    image: 4e7ab5c8-4b39-4c77-b68d-cf2ea7e1df1a
    key_name: ansible
    timeout: 200
    auto_ip: yes
    nics:
      - net-id: a5a73ab9-3ee5-49a6-bea0-f44f9e376ca0    
  register: result
- debug: var=result

And i get a loooong output of this like this one:

TASK [openstack : debug] *******************************************************
ok: [172.20.22.21] => {
    "result": {
        "changed": false,
        "id": "6f40f396-7ef8-4e0e-9769-2b9cea898269",
            .........
            "accessIPv4": "172.20.22.58",
            .......
                    {
                        ..........
                    },
                    {
                        ............
                        "addr": "172.20.22.58",
                        ......
                    }
                ]
            },
            ........
            },
            "interface_ip": "172.20.22.58",
            .................
                    .............
                ]
            },
            "os-extended-volumes:volumes_attached": [],
            "private_v4": "10.0.100.92",
            "progress": 0,
            "public_v4": "172.20.22.58",
            ................
                {
                   ...........
    }
}

So, cause i got this looong output its not that much helpfull. I Only need the IP address (in this case its the 172.20.22.58) - is there ANY possibility to "filter" the output or smth like this? Pls help me :/

Upvotes: 1

Views: 5935

Answers (2)

300D7309EF17
300D7309EF17

Reputation: 24573

Your formatting is slightly off, but basically register is not an argument of the task, it's at the task level. Here's what I mean.

bad:

- name: create a server
  os_server:
    state: present
    flavor: m1.nano
    auth:
      auth_url: ****
      username: ****
      password: ****
      project_name: admin
    register: result

good:

- name: create a server
  os_server:
    state: present
    flavor: m1.nano
    auth:
      auth_url: ****
      username: ****
      password: ****
      project_name: admin
  register: result

Note it's at the same level as os_server, not at os_server's arguments.

edit- return value

There are two ways to get at the result. First is to use {{result.id. accessIPv4}} elsewhere, and the other is to use set_fact that carries it around. That's especially useful if you need the IP in another role, and works well with a meta dependency.

- set_fact: machine_ip="{{result.id.accessIPV4}}"

Upvotes: 3

Abdelaziz Dabebi
Abdelaziz Dabebi

Reputation: 1638

When using Ansible, the output is JSON . So basically you would treat your output as json and access it's keys/values using {} and [].

In your example, you have to tell us the correct structure of your output. But anyway, let's assume that avcessIPv4 is a first level child of result, your big object and you want to access it:

so after registering result variable:

- register: result

you can access its child(s)

- debug: msg="{{ result.accessIPv4 }}" 

Also, you can define variable after registering an output which is an Ansible best practise, this way:

- set_fact: server_IP="{{ result.accessIPv4 }}"

Upvotes: 1

Related Questions