[英]Ansible: Handling of Forward Slash Fails in Dict Obj. Lookup
- name: Testing PyATS
tasks:
- name: Read in parse_genie role
include_role:
name: clay584.parse_genie
- name: show interfaces
ios_command:
commands:
- show interfaces
register: interfaces
- name: Set Fact Genie Filter
set_fact:
pyats_interfaces: "{{ interfaces['stdout'][0] | parse_genie(command='show interfaces', os='iosxe') }}"
- name: Debug
debug:
var: pyats_interfaces.GigabitEthernet0/0
"ansible_facts": {
"pyats_interfaces": {
"GigabitEthernet0/0": {
"arp_timeout": "04:00:00",
"arp_type": "arpa",
"auto_negotiate": true,
"bandwidth": 1000000,
"counters": {
"in_broadcast_pkts": 0,
"in_crc_errors": 0,
"in_errors": 0,
"in_frame": 0,
"in_giants": 0, etc.
"pyats_interfaces.GigabitEthernet0/0": "VARIABLE IS NOT DEFINED:: 'dict object' has no attribute 'GigabitEthernet0'"
It appears the / is being treated as an escape character and causing the variable to show up as undefined when searching the dict.似乎 / 被视为转义字符并导致变量在搜索字典时显示为未定义。 for G0/0.
对于 G0/0。
You have 2 equivalent syntax to address vars inside a dictionaries in ansible:您有 2 个等效语法来处理 ansible 中的字典内的变量:
some.var.to.address
some['var']['to']['address']
Both notations can be mixed in the way you want eg some['var']['to'].address
两种表示法都可以按照您想要的方式混合,例如
some['var']['to'].address
Although they are equivalent, some cases force you to use the bracket notation.尽管它们是等价的,但在某些情况下会强制您使用括号表示法。
my_var[item]
my_var[item]
my_var['field with spaces']
my_var['field with spaces']
You are in this latest case.你在这个最新的案例中。 So replacing your debug line with the following should fix your problem:
因此,用以下内容替换您的调试行应该可以解决您的问题:
- name: Debug
debug:
var: pyats_interfaces['GigabitEthernet0/0']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.