-1

我正在使用 Ansible 中的 networktocode ntc-ansible 模块来控制 Cisco IOS 设备(目前是交换机)。我可以成功地使用 ntc_show_Command 来获取“show version”和“show ip int brief”并将结果放入本地文件中。但是当我在 ansible-playbook 命令末尾使用 -vvv 时,我会在终端中看到结构化的 JSON 输出。如何从 ntc_show_command 访问“module_args”和“响应”,即。如果我使用“show ip int brief”并且我想知道 int gi1/0/5 的状态。我怎样才能访问它?然后......我会/可以使用什么剧本命令来获取我正在寻找的特定数据?

这是我在使用 -vvv 运行剧本时可以看到的输出,但我不知道如何访问结构化数据

ok: [VTgroup_SW] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "command": "show ip interface brief",
            "connection": "ssh",
            "connection_args": {},
            "delay": "1",
            "file": null,
            "global_delay_factor": "1",
            "host": "x.x.x.x",
            "index_file": "index",
            "key_file": null,
            "local_file": "verification/192.168.2.101.cfg",
            "optional_args": {},
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "platform": "cisco_ios",
            "port": null,
            "provider": null,
            "secret": null,
            "template_dir": "/home/melshman/.ansible/plugins/modules/ntc- 
ansible/ntc-templates/templates",
            "trigger_device_list": null,
            "use_keys": false,
            "use_templates": true,
            "username": "admin"
        }
    },
    "response": [
        {
            "intf": "Vlan1",
            "ipaddr": "y.y.y.y",
            "proto": "down",
            "status": "administratively down"
        },
        {
            "intf": "Vlan2",
            "ipaddr": "x.x.x.x",
            "proto": "up",
            "status": "up"
        },
TRUNCATED...

这是本地文件中 Show ip int brief 的输出。

Interface              IP-Address      OK? Method Status                
Protocol
Vlan1                  172.16.x.xxx    YES NVRAM  administratively down down
Vlan2                  192.168.x.xxx   YES NVRAM  up                    up
Vlan10                 10.10.yy.xxx      YES NVRAM  administratively down down

这是我的戏...

    - name: VERIFY INTERFACES
      ntc_show_command:
        connection: ssh
        platform: "cisco_ios"
        command: 'show ip interface brief'
        delay: 1 # delay before performing actions / commands in seconds
        global_delay_factor: 1 # delay between commands
        local_file: verification/{{ ansible_host }}.cfg
        host: "{{ ansible_host }}"
        username: "{{ ansible_user }}"
        password: "{{ ansible_ssh_pass }}"
        use_templates: True
        template_dir: '/home/melshman/.ansible/plugins/modules/ntc- 
  ansible/ntc-templates/templates'

更新我根据以下建议更新了代码:

我取得了进步,但我似乎仍然错过了一些重要的事情。我的第一个调试语句(如下)正在输出完整的输出(此处未显示),它有效。我可以成功地将 set_fact 设置为“接口”。我可以遍历 interfaces 变量并使用 when 语句检测条件集,我将其更改为查找所有 up/up 接口。但是...我的输出(如下所示未显示您显示的详细信息。我的显示 (item=None) 对所有符合条件的项目显示为 OK,但不是您的输出显示的详细信息(您的第二个代码块在原始答案)。
更新- 我将调试变量修改为“item.intf”,现在我得到了 intf 名称,但仍然不是所有详细信息。

**问题:如何获取仅显示符合条件的接口的输出?可以将这些结构化数据保存到文件中吗?

更新 2::: 我能够使用复制模块将 {{ interfaces }} 作为结构化数据复制到文件中,但是当我尝试只显示接口而不是其余数据时,而是使用 {{ interfaces .intf }} 我得到一个错误。(见下文)

问题:如何获得仅显示接口名称的输出/文件?
问题:如何获得仅显示启动/启动的接口名称的输出/文件?

我的第二个调试语句(如下)正在输出这个,它标识了 up/up 接口: UPDATED

skipping: [VTgroup_SW] => (item=None)
ok: [VTgroup_SW] => (item=None) => {
    "item.intf": "Vlan2"
}
skipping: [VTgroup_SW] => (item=None)
skipping: [VTgroup_SW] => (item=None)
skipping: [VTgroup_SW] => (item=None)
ok: [VTgroup_SW] => (item=None) => {
    "item.intf": "GigabitEthernet1/0/27"

剧本更新 2的补充

  - debug:
      var: output

  - set_fact:
      interfaces: "{{ output.response }}"

# shows all the intf that are up/up
  - debug:
      var: item.intf
    when:
      - '"up" in item.proto'
      - '"up" in item.status'
    loop: "{{ interfaces }}"


  - name:  Structured Data - INTERFACES
    copy:
      content:  "{{ interfaces }}"
      dest: "verification/interfaces_{{ansible_host}}.txt"

# This causes error (see traceback)
  - name:  Structured Data - INTERFACES with .intf
    copy:
      content:  "{{ interfaces.intf }}"
      dest: "verification/interfaces_{{ansible_host}}.txt"

游戏输出;- 名称:结构化数据 - 接口(截断)

[{"status": "administratively down", "intf": "Vlan1", "ipaddr": "172.16.0.253", "proto": "down"}, {"status": "up", "intf": "Vlan2", "ipaddr": "192.168.2.101", "proto": "up"}, {"status": "administratively down", "intf": "Vlan10", "ipaddr": "10.10.10.1", "proto": "down"}, {"status": "administratively down", "intf": "Vlan20", "ipaddr": "10.10.20.1", "proto": "down"}, {"status": "administratively down", "intf": "Vlan51", "ipaddr": "192.168.1.1", "proto": "down"}, ---- TRUNCATED

回溯上次播放;- 名称:结构化数据 - 与 .intf 的接口

TASK [Structured Data - INTERFACES] ************************************************************************************
task path: /home/melshman/projects/ansible/from-kbyer-ios-pb.yml:117
fatal: [VTgroup_SW]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'intf'\n\nThe error appears to have been in '/home/melshman/projects/ansible/from-kbyer-ios-pb.yml': line 117, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name:  Structured Data - INTERFACES\n    ^ here\n"
4

1 回答 1

0

您需要注册输出,然后开始使用 debug 和 set_fact 来检索您想要检索的数据结构元素:

在这里我注册输出,然后只检索“FastEthernet4”接口。

- name: Cisco IOS Automation
  hosts: pynet-rtr1
  connection: local
  gather_facts: no

  tasks:
    - name: VERIFY INTERFACES
      ntc_show_command:
        connection: ssh
        platform: "cisco_ios"
        command: 'show ip interface brief'
        host: "{{ ansible_host }}"
        username: "{{ ansible_user }}"
        password: "{{ ansible_ssh_pass }}"
        use_templates: True
        template_dir: '/home/kbyers/ntc-templates/templates'
      register: output

    - debug:
        var: output

    - set_fact:
        interfaces: "{{ output.response }}"

    - debug:
        var: item
      when: '"FastEthernet4" in item.intf'
      loop: "{{ interfaces }}"

您可以再次执行另一个 set_fact 并仅保存此 FastEthernet4 接口,然后查看“proto”和“status”字段(即“up”和“up”)。

这是执行的最后的样子:

TASK [debug] *****************************************************************************************************************************************************************************
skipping: [pynet-rtr1] => (item={u'status': u'down', u'intf': u'FastEthernet0', u'ipaddr': u'unassigned', u'proto': u'down'}) 
skipping: [pynet-rtr1] => (item={u'status': u'down', u'intf': u'FastEthernet1', u'ipaddr': u'unassigned', u'proto': u'down'}) 
skipping: [pynet-rtr1] => (item={u'status': u'down', u'intf': u'FastEthernet2', u'ipaddr': u'unassigned', u'proto': u'down'}) 
skipping: [pynet-rtr1] => (item={u'status': u'down', u'intf': u'FastEthernet3', u'ipaddr': u'unassigned', u'proto': u'down'}) 
ok: [pynet-rtr1] => (item={u'status': u'up', u'intf': u'FastEthernet4', u'ipaddr': u'10.220.88.20', u'proto': u'up'}) => {
    "item": {
        "intf": "FastEthernet4", 
        "ipaddr": "10.220.88.20", 
        "proto": "up", 
        "status": "up"
    }
}
skipping: [pynet-rtr1] => (item={u'status': u'down', u'intf': u'Vlan1', u'ipaddr': u'unassigned', u'proto': u'down'}) 

PLAY RECAP *******************************************************************************************************************************************************************************
pynet-rtr1                 : ok=4    changed=0    unreachable=0    failed=0   

在 set_fact 上添加更多信息(即,这里是您如何保存仅对应于该一个接口的变量)。

- set_fact:
    intf_fa4: "{{ item }}"
  when: '"FastEthernet4" in item.intf'
  loop: "{{ interfaces }}"

- debug:
    var: intf_fa4
于 2018-07-24T02:32:41.980 回答