ntc-ansible "response" 和 "module_args" (如何访问?)
ntc-ansible "response" and "module_args" (how access?)
我在 Ansible 中使用 networktocode ntc-ansible 模块来控制 Cisco IOS 设备(目前是交换机)。我可以成功地使用 ntc_show_Command 获取 'show version' 和 'show ip int brief' 并将结果放入本地文件。但是当我在 ansible-playbook 命令末尾使用 -vvv 时,我在终端中看到了结构化的 JSON 输出。如何从 ntc_show_command 访问 "module_args" 和 "response",即。如果我使用 "show ip int brief" 并且我想知道 int gi1/0/5 的状态。我怎样才能访问它?然后...我使用什么 playbook 命令 would/could 来获取我正在寻找的特定数据?
这是我使用-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'
UPDATE 我根据这样的建议更新了代码:
我取得了进步,但我似乎仍然缺少一些重要的东西。我的第一个调试语句(如下)正在输出完整的输出(此处未显示),这是有效的。我可以set_fact成功到'interfaces'。我可以遍历 interfaces 变量并检测使用 when 语句设置的条件,我将其更改为查找所有 up/up 接口。但是...我的输出(如下所示未显示您显示的详细信息。我的输出显示(item=None)所有符合条件的项目,但不是您的输出显示的详细信息(您的原始答案中的第二个代码块)。
已更新 - 我将调试变量修改为 'item.intf',现在我得到了 intf 名称,但仍然不是所有细节。
**问题:如何获取仅显示符合条件的接口的输出?这个结构化数据可以保存到文件中吗?
更新 2:::
我能够使用复制模块将 {{ interfaces }} 作为结构化数据复制到文件中,但是当我尝试只显示接口而不显示其余数据时,而是使用 {{ interfaces.intf }我得到一个错误。 (见下文)
问题:如何获得仅显示接口名称的输出/文件?
问题:如何获得仅显示 up/up 接口名称的输出/文件?
我的第二个调试语句(下方)正在输出这个,它标识了 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"
您需要注册输出,然后开始使用调试和 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
我在 Ansible 中使用 networktocode ntc-ansible 模块来控制 Cisco IOS 设备(目前是交换机)。我可以成功地使用 ntc_show_Command 获取 'show version' 和 'show ip int brief' 并将结果放入本地文件。但是当我在 ansible-playbook 命令末尾使用 -vvv 时,我在终端中看到了结构化的 JSON 输出。如何从 ntc_show_command 访问 "module_args" 和 "response",即。如果我使用 "show ip int brief" 并且我想知道 int gi1/0/5 的状态。我怎样才能访问它?然后...我使用什么 playbook 命令 would/could 来获取我正在寻找的特定数据?
这是我使用-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'
UPDATE 我根据这样的建议更新了代码:
我取得了进步,但我似乎仍然缺少一些重要的东西。我的第一个调试语句(如下)正在输出完整的输出(此处未显示),这是有效的。我可以set_fact成功到'interfaces'。我可以遍历 interfaces 变量并检测使用 when 语句设置的条件,我将其更改为查找所有 up/up 接口。但是...我的输出(如下所示未显示您显示的详细信息。我的输出显示(item=None)所有符合条件的项目,但不是您的输出显示的详细信息(您的原始答案中的第二个代码块)。
已更新 - 我将调试变量修改为 'item.intf',现在我得到了 intf 名称,但仍然不是所有细节。
**问题:如何获取仅显示符合条件的接口的输出?这个结构化数据可以保存到文件中吗?
更新 2::: 我能够使用复制模块将 {{ interfaces }} 作为结构化数据复制到文件中,但是当我尝试只显示接口而不显示其余数据时,而是使用 {{ interfaces.intf }我得到一个错误。 (见下文)
问题:如何获得仅显示接口名称的输出/文件?
问题:如何获得仅显示 up/up 接口名称的输出/文件?
我的第二个调试语句(下方)正在输出这个,它标识了 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"
您需要注册输出,然后开始使用调试和 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