我试图将我的loopback0
的IP地址存储为变量。我可以调用ipv4
信息,但它不会让我调用地址信息。 [隐私信息]
码:
- name: Configure IPSLA on Americas Router
gather_facts: false
hosts: IP_SLA
connection: local
serial: 1
tasks:
- name: Gather Switch Info
ios_facts:
- name: Debug
debug:
var: ansible_facts["net_interfaces"]["Loopback0"]["ipv4"]
...
输出:
PLAY [Configure IPSLA on Americas Router] ***************************************************************************************************************************************************************************************************
TASK [Gather Switch Info] *******************************************************************************************************************************************************************************************************************
ok: [host] => {"ansible_facts": {"ansible_net_interfaces": {"Loopback0": {"bandwidth": 8000000, "description": null, "duplex": null, "ipv4": [{"address": "10.x.x.x", "subnet": "32"}], "lineprotocol": "up ", "macaddress": null, "mediatype": null, "mtu": 1514, "operstatus": "up", "type": null}
TASK [Debug] ********************************************************************************************************************************************************************************************************************************
ok: [host] => {
"ansible_facts[\"net_interfaces\"][\"Loopback0\"][\"ipv4\"]": [
{
"address": "10.x.x.x",
"subnet": "32"
}
]
}
但是当我尝试拨打地址时:
- name: Debug
debug:
var: ansible_facts["net_interfaces"]["Loopback0"]["ipv4"]["address"]
我最终得到了这个错误:
TASK [Debug] ****************************************************************************************************************************************
ok: [host] => {
"ansible_facts[\"net_interfaces\"][\"Loopback0\"][\"ipv4\"][\"address\"]": "VARIABLE IS NOT DEFINED!: 'list object' has no attribute 'address'"
如何将地址存储为变量,以便在将来的任务中使用它?
方括号:
ok: [host] => {
"ansible_facts[\"net_interfaces\"][\"Loopback0\"][\"ipv4\"]": [
{
"address": "10.x.x.x",
"subnet": "32"
}
]
}
表明这个["net_interfaces"]["Loopback0"]["ipv4"]
是一个包含单个元素的数组。
使用[0]
指示数组的第一个元素,如以下代码所示:
ansible_facts["net_interfaces"]["Loopback0"]["ipv4"][0]["address"]