请告知如何将一个任务的注册数据用作同一剧本中另一个任务的变量。
剧本:
---
- 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 正在创建 3 个(通过循环)Wireguard 接口,我需要使用这些接口的本地链接地址作为变量,如wg1.address、wg2.address、wg3.address。现在我只能看到使用以下命令生成的大量输出:
- name: Dump "Extended query example" output
ansible.builtin.debug:
msg: '{{ extended_queryout }}'
像这样:
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
}
}
如何设置变量 wg1.address 值 fe80::caa4:e31c:dd63:7598/64、变量 wg2.address 值 fe80::c0eb:fadb:2da0:50f5/64 和 wg3.address 值 fe80::28d1:7b9b:92:ed43/64。
先感谢您。
期望看到变量 wg1.address 值 fe80::caa4:e31c:dd63:7598/64、变量 wg2.address 值 fe80::c0eb:fadb:2da0:50f5/64 和 wg3.address 值 fe80::28d1:7b9b:92:ed43/64。