ntc-ansible“响应"和“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"和响应",即.如果我使用show ip int brief"并且我想知道 int gi1/0/5 的状态.我怎样才能访问它?然后......我将/可以使用哪些剧本命令来获取我正在寻找的特定数据?
I am using the networktocode ntc-ansible module in Ansible to control Cisco IOS devices (switches currently). I successfully can use ntc_show_Command to get 'show version' and 'show ip int brief' and put the result into a local file. But when I use the -vvv at the end of the ansible-playbook command, I see structured JSON output in the terminal. How do I get a access the "module_args" and the "response" from the ntc_show_command, ie. if i use "show ip int brief" and i want to know the status of int gi1/0/5. How can I access that? And then... what playbook commands would/could I use to grab the specific data I am looking for?
这是我运行剧本时可以看到的输出,使用-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 名称,但仍然不是所有细节.
UPDATE I updated code based on suggestions as such:
I made progress, but I seem to still be missing something important. My first debug statement (below) is outputting the full output (not shown here), which works. I can set_fact successfully to 'interfaces'. I can loop through the interfaces variable and detect a condition set with the when statement, which I changed to look for all up/up interfaces. BUT... my output (shown below doesn't show the details yours showed. Mine shows (item=None) for all and an OK on items which match the criteria, but not the details your output shows (your second code block in original answer).
UPDATED - I modified the debug var to 'item.intf' and now I get intf name, but still not all details.
**问题:如何获取仅显示符合条件的接口的输出?这种结构化数据可以保存到文件中吗?
**QUESTION: How can I grab an output that shows only the interfaces that meet the criteria? Can this structured data be saved to a file?
更新 2:::我能够使用复制模块将 {{ interfaces }} 作为结构化数据复制到文件中,但是当我尝试只显示接口而不是其余数据时,而是使用 {{ interfaces.intf }} 我得到一个错误.(见下文)
UPDATE 2::: I was able to use the copy module to copy {{ interfaces }} to a file as structured data, but when I try to just show the interfaces, and not the rest of the data, but using {{ interfaces.intf }} I get an error. (See below)
问题:如何获得只显示接口名称的输出/文件?
问题:我怎样才能得到一个只显示 up/up 的接口名称的输出/文件?
Question: How can I get a output / file that shows only the interface names?
Question: How can I get an output / file that shows only the interfaces names that are 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"
推荐答案
您需要注册输出,然后开始使用 debug 和 set_fact 来检索要检索的数据结构元素:
You need to register the output and then start using debug and set_fact to retrieve the data structure elements that you want to retrieve:
在这里我注册输出,然后只检索FastEthernet4"接口.
Here I register the output and then only retrieve the 'FastEthernet4' interface.
- 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").
Once again you could do another set_fact and save only this FastEthernet4 interface and then look at the 'proto' and 'status' fields (i.e. 'up' and '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 的更多信息(即,这是保存仅对应于该接口的变量的方法).
Adding more info on the set_fact (i.e. here is how you could save a variable corresponding to only that one interface).
- set_fact:
intf_fa4: "{{ item }}"
when: '"FastEthernet4" in item.intf'
loop: "{{ interfaces }}"
- debug:
var: intf_fa4
这篇关于ntc-ansible“响应"和“module_args"(如何访问?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!