ibellinf
ibellinf

Reputation: 13

using ansible facts or json_query, how do i get this mac address out of this json?

so i got json back from an openstack server query, but need to extract the mac address for the server. Getting the name and IP is easy enough but the mac address is hidden behind the vlan and a colon. Can you please help to identify either a json_query or maybe ansible fact play that will get me just the mac address as plain text? Below actual play for this, but getting the mac address fails.

---

- name: Test getting data from openstack
  gather_facts: true
  hosts: localhost
  become: true
  vars:
   server_name: haproxy1
  tasks:
   - name: Retrieve list of all servers in this project
     os_server_info:
       auth:
         auth_url: '{{ os_auth_url }}'
         username: '{{ os_username }}'
         password: '{{ os_password }}'
         project_name: '{{ os_project_name }}'
         user_domain_name: '{{ os_user_domain_name }}'
         project_domain_name: "{{ os_project_domain_name }}"
       validate_certs: false
     register: servers


   - name: get the server MAC address table
     set_fact:
       server_mac_table: "{{servers | community.general.json_query(jmespath_mac) }}"
     vars:
       jmespath_mac:  "servers[?name == '{{ server_name }}'].addresses"
     register: results

   - debug:
       var=results

   - name: get only the server MAC address
     set_fact:
       any_server_mac: "{{servers | community.general.json_query(jmespath_mac) | replace('[','' ) | replace(']','' ) }}"
     vars:
       jmespath_mac:  "servers[?name == '{{ server_name }}'].{addresses.VLAN-199.0S-EXT-IPS-MAC:mac_addr}"
     register: results

   - debug:
       var=results

so the task to get the mac address json produces the below output.

TASK [get the server MAC address table] **************************************************************************************************************************
ok: [localhost]

TASK [debug] *****************************************************************************************************************************************************
ok: [localhost] => {
    "results": {
        "ansible_facts": {
            "server_mac_table": [
                {
                    "VLAN-199": [
                        {
                            "OS-EXT-IPS-MAC:mac_addr": "fa:0b:ab:f2:9a:20",
                            "OS-EXT-IPS:type": "fixed",
                            "addr": "192.128.120.131",
                            "version": 4
                        }
                    ]
                }
            ]
        },
        "changed": false,
        "failed": false
    }
}

So, I need to construct a play for json_query or ansible fact to replace this play

your help would be greatly appreciated

Upvotes: 0

Views: 63

Answers (0)

Related Questions