我有这个:
- name: Get Client Info
debug:
msg: "{{ client_list | community.general.json_query(query) }}"
vars:
query: " meraki_response[*].{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 }}"
给出:
TASK [Get Client Info] ***********************
ok: [localhost] => {
"msg": [
{
"ip": "10.0.0.1",
"name": "some-device1"
},
{
"ip": "10.0.0.2",
"name": "some-device2"
}
]
}
TASK [Get Client Info] *****
TASK [debug] ******************
ok: [localhost] => {
"msg": " client_mac = ['00:00:00:00:00:01', '00:00:00:00:00:02']"
}
最终目标是使用“{{ client_mac }}”作为键,“{{ ip }}”作为ip,“{{ name }}”作为名称。
{
“client_mac”: {
“ip”: “ip”,
“name”: “name”
},
{
“client_mac”: {
“ip”: “ip”,
“name”: “name”
}
}
我是 ansible 的新手,需要一些帮助。我已经绞尽脑汁一周了,但我一生都无法完成这件事。 我尝试了很多不同的事情,主要是通过跟踪这个网站。我记不起一切。一个问题是我不知道如何循环 set_fact。我认为如果我可以从 Mac 地址创建密钥获得帮助,那么我就可以找出其他部分。
我想使用 client_mac、ip 和 name 变量。
提前谢谢您
例如,给出测试数据
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)) }}"
给予你(可能)想要的东西
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
用于测试的完整剧本示例
- 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