简体   繁体   English

Ansible:在字典对象中处理正斜杠失败。 抬头

[英]Ansible: Handling of Forward Slash Fails in Dict Obj. Lookup

Code:代码:

- 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

Expect:预计:

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

Getting:得到:

"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 中的字典内的变量:

  • The dot notation:点符号:
some.var.to.address
  • The bracket notation:括号符号:
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.尽管它们是等价的,但在某些情况下会强制您使用括号表示法。

  • when the name of the field comes from a variable eg my_var[item]当字段的名称来自变量时,例如my_var[item]
  • when the name of the field contains special characters eg 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM