ansiblemikrotik

Use registered output fields as variables in ansible playbook


Please advise how to use registered data from one task as variables for another task in the same playbook.

Playbook:

---
- name: RouterOS test with API
  hosts: localhost
  vars:
    hostname: "some_host"
    username: "api"
    password: "some_password"
    gather_subset: all
  module_defaults:
    group/community.routeros.api:
      hostname: "{{ hostname }}"
      password: "{{ password }}"
      username: "{{ username }}"
      tls: true
      force_no_cert: false
      validate_certs: false
      validate_cert_hostname: false
  tasks:

  - name: Add wg tuns
    ignore_errors: true
    community.routeros.api:
      path: "interface wireguard"
      add: "name={{ item.name }} listen-port={{ item.port }} comment={{ item.comment }}"
    loop:
      - { name: 'wg1', port: '111', comment: 'com1' }
      - { name: 'wg2', port: '222', comment: 'com2' }
      - { name: 'wg3', port: '333', comment: 'com3' }

  - name: Get wg link-local addrs
    community.routeros.api:
      path: ipv6 address
      extended_query:
        attributes:
          - address
          - interface
        where:
          - attribute: "interface"
            is: "=="
            value: "{{ item.name }}"
    loop:
      - { name: 'wg1' }
      - { name: 'wg2' }
      - { name: 'wg3' }
    register: extended_queryout

  - name: Dump "Extended query example" output
    ansible.builtin.debug:
      msg: '{{ extended_queryout }}'

Playbook is creating 3 (via loop) wireguard interfaces and I need to use these interfaces link-local addresses as variables like wg1.address, wg2.address, wg3.address. Now I can only see a lot of output generated with

  - name: Dump "Extended query example" output
    ansible.builtin.debug:
      msg: '{{ extended_queryout }}'

like this:

TASK [Dump "Extended query example" output] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "add": null,
                        "ca_path": null,
                        "cmd": null,
                        "encoding": "ASCII",
                        "extended_query": {
                            "attributes": [
                                "address",
                                "interface"
                            ],
                            "where": [
                                {
                                    "attribute": "interface",
                                    "is": "==",
                                    "or": null,
                                    "value": "wg1"
                                }
                            ]
                        },
                        "force_no_cert": false,
                        "hostname": "some_host",
                        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "path": "ipv6 address",
                        "port": null,
                        "query": null,
                        "remove": null,
                        "timeout": 10,
                        "tls": true,
                        "update": null,
                        "username": "api",
                        "validate_cert_hostname": false,
                        "validate_certs": false
                    }
                },
                "item": {
                    "name": "wg1"
                },
                "msg": [
                    {
                        "address": "fe80::caa4:e31c:dd63:7598/64",
                        "interface": "wg1"
                    }
                ]
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "add": null,
                        "ca_path": null,
                        "cmd": null,
                        "encoding": "ASCII",
                        "extended_query": {
                            "attributes": [
                                "address",
                                "interface"
                            ],
                            "where": [
                                {
                                    "attribute": "interface",
                                    "is": "==",
                                    "or": null,
                                    "value": "wg2"
                                }
                            ]
                        },
                        "force_no_cert": false,
                        "hostname": "some_host",
                        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "path": "ipv6 address",
                        "port": null,
                        "query": null,
                        "remove": null,
                        "timeout": 10,
                        "tls": true,
                        "update": null,
                        "username": "api",
                        "validate_cert_hostname": false,
                        "validate_certs": false
                    }
                },
                "item": {
                    "name": "wg2"
                },
                "msg": [
                    {
                        "address": "fe80::c0eb:fadb:2da0:50f5/64",
                        "interface": "wg2"
                    }
                ]
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "add": null,
                        "ca_path": null,
                        "cmd": null,
                        "encoding": "ASCII",
                        "extended_query": {
                            "attributes": [
                                "address",
                                "interface"
                            ],
                            "where": [
                                {
                                    "attribute": "interface",
                                    "is": "==",
                                    "or": null,
                                    "value": "wg3"
                                }
                            ]
                        },
                        "force_no_cert": false,
                        "hostname": "some_host",
                        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "path": "ipv6 address",
                        "port": null,
                        "query": null,
                        "remove": null,
                        "timeout": 10,
                        "tls": true,
                        "update": null,
                        "username": "api",
                        "validate_cert_hostname": false,
                        "validate_certs": false
                    }
                },
                "item": {
                    "name": "wg3"
                },
                "msg": [
                    {
                        "address": "fe80::28d1:7b9b:92:ed43/64",
                        "interface": "wg3"
                    }
                ]
            }
        ],
        "skipped": false
    }
}

How to set variable wg1.address value fe80::caa4:e31c:dd63:7598/64, variable wg2.address value fe80::c0eb:fadb:2da0:50f5/64 and wg3.address value fe80::28d1:7b9b:92:ed43/64.

Thank you in advance.

Expecting to see variable wg1.address value fe80::caa4:e31c:dd63:7598/64, variable wg2.address value fe80::c0eb:fadb:2da0:50f5/64 and wg3.address value fe80::28d1:7b9b:92:ed43/64.


Solution

  • You can access the mac address for each interface like this:

    - name: Print mac address of iface wg1
      ansible.builtin.debug:
        msg: "{{ extended_queryout.results|json_query('[0].msg[0].address') }}"
                                            # see: -----^ 
    
    - name: Print mac address of iface wg2
      ansible.builtin.debug:
        msg: "{{ extended_queryout.results|json_query('[1].msg[0].address') }}"
                                            # see: -----^
    

    To store the value in a fact, use the set_fact module:

    - name: Store the mac addresses as facts
      ansible.builtin.set_fact:
        wg1:
          address: "{{ extended_queryout.results|json_query('[0].msg[0].address') }}"
        wg2:
          address: "{{ extended_queryout.results|json_query('[1].msg[0].address') }}"
        wg3:
          address: "{{ extended_queryout.results|json_query('[2].msg[0].address') }}"