Por favor, informe como usar dados registrados de uma tarefa como variáveis para outra tarefa no mesmo manual.
Manual de instruções:
---
- 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 }}'
O Playbook está criando 3 (via loop) interfaces wireguard e eu preciso usar esses endereços link-local de interfaces como variáveis como wg1.address, wg2.address, wg3.address . Agora eu só consigo ver muita saída gerada com
- name: Dump "Extended query example" output
ansible.builtin.debug:
msg: '{{ extended_queryout }}'
assim:
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
}
}
Como definir o valor da variável wg1.address fe80::caa4:e31c:dd63:7598/64, o valor da variável wg2.address fe80::c0eb:fadb:2da0:50f5/64 e o valor wg3.address fe80::28d1:7b9b:92:ed43/64.
Agradeço antecipadamente.
Esperando ver a variável wg1.address valor fe80::caa4:e31c:dd63:7598/64, a variável wg2.address valor fe80::c0eb:fadb:2da0:50f5/64 e wg3.address valor fe80::28d1:7b9b:92:ed43/64.
mapear o atributo msg
para obter a lista
Você pode selecionar e mapear o que quiser ou pode converter a lista em um dicionário
dá o dicionário trivial
Exemplo de um manual completo para testes
Você pode acessar o endereço MAC de cada interface assim:
Para armazenar o valor em um fato, use o
set_fact
módulo: