Ansible:尝试解析Loopback0 IP地址

时间:2019-02-25 22:58:46

标签: ansible cisco

我正在尝试将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'"

如何将地址存储为变量,以便在以后的任务中使用它?

1 个答案:

答案 0 :(得分:0)

方括号:

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"]