How to turn a mac address into a usable variable

63 views Asked by At

This being my first time posting, I messed some things up. Hopefully I can correct that. By correcting the question and providing more info.

I am needing to use a mac address as a variable. I have tasks that gather information from a client on a network, mac, ip and name. I need to use each one of them as variables, and i can figure out how to use the ip and name, but not the mac.

I have tried to use set_fact, but i am not get it to loop over the clients pulled to be able to use as a variable. Any help would be appreciated.

    - name: Get Vlan 30 Clients
      networks_clients_info:
        meraki_api_key: "{{ MERAKI_KEY }}"
        vlan: 30
        networkId: "{{ store_id.msg[0] }}"
        recentDeviceConnections: Wired
        os: Windows
      register: client_list

    - debug:
        msg: "{{client_list}}"

    - name: Get Client Info
      debug:
        msg: "{{ client_list | 
          community.general.json_query(query) }}"
      vars: 
        query: " meraki_response[*].
           {mac: mac, ip: ip, name: description} "
      register: client_info

    
      

    - name: Get MacAddress
      vars:
        mac_name: client_mac
        mac_value: "{{ client_list | 
         community.general.json_query('meraki_response[].mac') 
          }}"
      set_fact:
        "{{ mac_name }}": "{{ mac_value }}"

    - debug:
        msg: "client_mac = {{ client_mac }}"

    - name: Update a VLAN.
      networks_appliance_vlans:
        meraki_api_key: "{{ MERAKI_KEY }}"
        networkId: "{{ store_id.msg[0] }}"
        state: present
        vlanId: 30
        fixedIpAssignments:
          22:33:44:55:66:77: <-- "{{client_mac}}
            ip: 1.2.3.4 <-- "{{client_ip}}"
            name: Some client name <-- "{{cleint_name}}"

Output:

ok: [localhost] => {
"msg": {
    "changed": false,
    "failed": false,
    "meraki_response": [
        {
            "adaptivePolicyGroup": ,
            "description": "some-device1",
            "deviceTypePrediction": null,
            "firstSeen": "",
            "groupPolicy8021x": null,
            "id": "",
            "ip": "10.0.0.1",
            "ip6": null,
            "ip6Local": "",
            "lastSeen": "",
            "mac": "00:00:00:00:00:01",
            "manufacturer": "",
            "notes": ,
            "os": "",
            "pskGroup": null,
            "recentDeviceConnection": "",
            "recentDeviceMac": "",
            "recentDeviceName": "",
            "recentDeviceSerial": "",
            "smInstalled": false,
            "ssid": null,
            "status": "",
            "switchport": null
            "user": null,
            "vlan": "30",
            "wirelessCapabilities": ""
        },
        {
            "adaptivePolicyGroup": null,
            "description": "some-device2",
            "deviceTypePrediction": null,
            "firstSeen": "",
            "groupPolicy8021x": null,
            "id": "",
            "ip": "10.0.0.2",
            "ip6": null,
            "ip6Local": "",
            "lastSeen": "",
            "mac": "00:00:00:00:00:02",
            "manufacturer": "",
            "notes": null,
            "os": "",
            "pskGroup": null,
            "recentDeviceConnection": "",
            "recentDeviceMac": "",
            "recentDeviceName": "",
            "recentDeviceSerial": "",
            "smInstalled": false,
            "ssid": null,
            "status": "Online",
            "switchport": null,
            "user": null,
            "vlan": "30",
            "wirelessCapabilities": ""
        }
    ],
    "result": ""
  }
}

TASK [Get Client Info] *************************
ok: [localhost] => {
"msg": [
    {
        "ip": "10.0.0.1",
        "mac": "00:00:00:00:00:01",
        "name": "some-device1"
    },
    {
        "ip": "10.0.0.2",
        "mac": "00:00:00:00:00:02",
        "name": "some-device2"
    }
  ]
}

TASK [Get MacAddress] ************************
ok: [localhost]

TASK [debug] *******************
ok: [localhost] => {
  "msg": "client_mac = ['00:00:00:00:00:01', '00:00:00:00:00:02']"
}
1

There are 1 answers

1
Vladimir Botka On

For example, given the data for testing

  meraki_response:
    - {description: d1, ip: 10.1.0.1, mac: aa:bb:cc:11:22:01}
    - {description: d2, ip: 10.1.0.2, mac: aa:bb:cc:11:22:02}
    - {description: d3, ip: 10.1.0.3, mac: aa:bb:cc:11:22:03}

The below expressions

  meraki_query: '[].[mac, {ip: ip, name: description}]'
  result: "{{ dict(meraki_response|
                   community.general.json_query(meraki_query)) }}"

give what you (probably) want

  result:
    aa:bb:cc:11:22:01:
      ip: 10.1.0.1
      name: d1
    aa:bb:cc:11:22:02:
      ip: 10.1.0.2
      name: d2
    aa:bb:cc:11:22:03:
      ip: 10.1.0.3
      name: d3

Example of a complete playbook for testing

- hosts: all

  vars:

    meraki_response:
      - {description: d1, ip: 10.1.0.1, mac: aa:bb:cc:11:22:01}
      - {description: d2, ip: 10.1.0.2, mac: aa:bb:cc:11:22:02}
      - {description: d3, ip: 10.1.0.3, mac: aa:bb:cc:11:22:03}

    meraki_query: '[].[mac, {ip: ip, name: description}]'
    result: "{{ dict(meraki_response|
                     community.general.json_query(meraki_query)) }}"

  tasks:

    - debug:
        var: result