JMESPath 响应过滤
JMESPath filtering on response
当 lldp-remote-system-name
包含 slc1
.
时,我尝试过滤以获得 lldp-remote-system-name
但我收到错误:
Error in jmespath.search in json_query filter plugin:\n'in ' requires string as left operand, not NoneType
任务:
- name: get system information
juniper_junos_rpc:
rpc: get-lldp-neighbors-information
register: response
- name: Get remote system name
set_fact:
lldp_interface: "{{ response.parsed_output | to_json | from_json | json_query(interface) }}"
vars:
interface: '"lldp-neighbors-information"."lldp-neighbor-information"[?contains("lldp-remote-system-name","slc1")]."lldp-remote-system-name"'
- name: Print response
debug:
msg:
- "{{ lldp_interface }}"
回应
{
"lldp-neighbors-information": {
"lldp-neighbor-information": [
{
"lldp-local-parent-interface-name": "ae1",
"lldp-local-port-id": "et-0/0/50",
"lldp-remote-chassis-id": "22:22:22:22:22:22",
"lldp-remote-chassis-id-subtype": "Mac address",
"lldp-remote-port-description": "las1-router-1:et-0/0/50",
"lldp-remote-system-name": "las1-router-1"
},
{
"lldp-local-parent-interface-name": "ae0",
"lldp-local-port-id": "xe-0/0/1",
"lldp-remote-chassis-id": "11:11:11:11:11:11",
"lldp-remote-chassis-id-subtype": "Mac address",
"lldp-remote-port-description": "slc1-router-1-xe-0/0/1",
"lldp-remote-system-name": "slc1-router-1"
}
]
}
}
无需使用jmepath:
- name: testplaybook jinja2
hosts: localhost
gather_facts: no
vars:
response:
lldp-neighbors-information:
lldp-neighbor-information:
- lldp-local-parent-interface-name: ae1
lldp-local-port-id: et-0/0/50
lldp-remote-chassis-id: 22:22:22:22:22:22
lldp-remote-chassis-id-subtype: Mac address
lldp-remote-port-description: las1-router-1:et-0/0/50
lldp-remote-system-name: las1-router-1
- lldp-local-parent-interface-name: ae0
lldp-local-port-id: xe-0/0/1
lldp-remote-chassis-id: 11:11:11:11:11:11
lldp-remote-chassis-id-subtype: Mac address
lldp-remote-port-description: slc1-router-1-xe-0/0/1
lldp-remote-system-name: slc1-router-1
tasks:
- name: Disp
debug:
msg: "{{ item['lldp-remote-system-name'] }}"
loop: "{{ response['lldp-neighbors-information']['lldp-neighbor-information'] }}"
loop_control:
label: "interface name: {{ item['lldp-local-parent-interface-name'] }}"
when: "'slc1' in item['lldp-remote-system-name']"
结果:
skipping: [localhost] => (item=interface name: ae1)
ok: [localhost] => (item=interface name: ae0) => {
"msg": "slc1-router-1"
}
在 JMESPath 中,双引号不是字符串分隔符,它们有一个特定的用途:它们分隔具有特殊字符的标识符:
An identifier can also be quoted. This is necessary when an identifier has characters not specified in the unquoted-string grammar
rule. In this situation, an identifier is specified with a double quote, followed by any number of unescaped-char
or escaped-char
characters, followed by a double quote.
来源:https://jmespath.org/specification.html#identifiers
如果您想要 raw string littoral,请改用反引号:`
.
因此,您的 JMESPath 查询应该是 — 分成多行以使其更具可读性:
interface: >-
"lldp-neighbors-information"
."lldp-neighbor-information"[?
contains("lldp-remote-system-name",`slc1`)
]
."lldp-remote-system-name"
给定任务:
- debug:
msg: "{{ json | json_query(interface) }}"
vars:
interface: >-
"lldp-neighbors-information"
."lldp-neighbor-information"[?
contains("lldp-remote-system-name",`slc1`)
]
."lldp-remote-system-name"
json:
lldp-neighbors-information:
lldp-neighbor-information:
- lldp-local-parent-interface-name: ae1
lldp-local-port-id: et-0/0/50
lldp-remote-chassis-id: 22:22:22:22:22:22
lldp-remote-chassis-id-subtype: Mac address
lldp-remote-port-description: las1-router-1:et-0/0/50
lldp-remote-system-name: las1-router-1
- lldp-local-parent-interface-name: ae0
lldp-local-port-id: xe-0/0/1
lldp-remote-chassis-id: 11:11:11:11:11:11
lldp-remote-chassis-id-subtype: Mac address
lldp-remote-port-description: slc1-router-1-xe-0/0/1
lldp-remote-system-name: slc1-router-1
这产生:
ok: [localhost] =>
msg:
- slc1-router-1
当 lldp-remote-system-name
包含 slc1
.
lldp-remote-system-name
但我收到错误:
Error in jmespath.search in json_query filter plugin:\n'in ' requires string as left operand, not NoneType
任务:
- name: get system information
juniper_junos_rpc:
rpc: get-lldp-neighbors-information
register: response
- name: Get remote system name
set_fact:
lldp_interface: "{{ response.parsed_output | to_json | from_json | json_query(interface) }}"
vars:
interface: '"lldp-neighbors-information"."lldp-neighbor-information"[?contains("lldp-remote-system-name","slc1")]."lldp-remote-system-name"'
- name: Print response
debug:
msg:
- "{{ lldp_interface }}"
回应
{
"lldp-neighbors-information": {
"lldp-neighbor-information": [
{
"lldp-local-parent-interface-name": "ae1",
"lldp-local-port-id": "et-0/0/50",
"lldp-remote-chassis-id": "22:22:22:22:22:22",
"lldp-remote-chassis-id-subtype": "Mac address",
"lldp-remote-port-description": "las1-router-1:et-0/0/50",
"lldp-remote-system-name": "las1-router-1"
},
{
"lldp-local-parent-interface-name": "ae0",
"lldp-local-port-id": "xe-0/0/1",
"lldp-remote-chassis-id": "11:11:11:11:11:11",
"lldp-remote-chassis-id-subtype": "Mac address",
"lldp-remote-port-description": "slc1-router-1-xe-0/0/1",
"lldp-remote-system-name": "slc1-router-1"
}
]
}
}
无需使用jmepath:
- name: testplaybook jinja2
hosts: localhost
gather_facts: no
vars:
response:
lldp-neighbors-information:
lldp-neighbor-information:
- lldp-local-parent-interface-name: ae1
lldp-local-port-id: et-0/0/50
lldp-remote-chassis-id: 22:22:22:22:22:22
lldp-remote-chassis-id-subtype: Mac address
lldp-remote-port-description: las1-router-1:et-0/0/50
lldp-remote-system-name: las1-router-1
- lldp-local-parent-interface-name: ae0
lldp-local-port-id: xe-0/0/1
lldp-remote-chassis-id: 11:11:11:11:11:11
lldp-remote-chassis-id-subtype: Mac address
lldp-remote-port-description: slc1-router-1-xe-0/0/1
lldp-remote-system-name: slc1-router-1
tasks:
- name: Disp
debug:
msg: "{{ item['lldp-remote-system-name'] }}"
loop: "{{ response['lldp-neighbors-information']['lldp-neighbor-information'] }}"
loop_control:
label: "interface name: {{ item['lldp-local-parent-interface-name'] }}"
when: "'slc1' in item['lldp-remote-system-name']"
结果:
skipping: [localhost] => (item=interface name: ae1)
ok: [localhost] => (item=interface name: ae0) => {
"msg": "slc1-router-1"
}
在 JMESPath 中,双引号不是字符串分隔符,它们有一个特定的用途:它们分隔具有特殊字符的标识符:
An identifier can also be quoted. This is necessary when an identifier has characters not specified in the
unquoted-string grammar
rule. In this situation, an identifier is specified with a double quote, followed by any number ofunescaped-char
orescaped-char
characters, followed by a double quote.
来源:https://jmespath.org/specification.html#identifiers
如果您想要 raw string littoral,请改用反引号:`
.
因此,您的 JMESPath 查询应该是 — 分成多行以使其更具可读性:
interface: >-
"lldp-neighbors-information"
."lldp-neighbor-information"[?
contains("lldp-remote-system-name",`slc1`)
]
."lldp-remote-system-name"
给定任务:
- debug:
msg: "{{ json | json_query(interface) }}"
vars:
interface: >-
"lldp-neighbors-information"
."lldp-neighbor-information"[?
contains("lldp-remote-system-name",`slc1`)
]
."lldp-remote-system-name"
json:
lldp-neighbors-information:
lldp-neighbor-information:
- lldp-local-parent-interface-name: ae1
lldp-local-port-id: et-0/0/50
lldp-remote-chassis-id: 22:22:22:22:22:22
lldp-remote-chassis-id-subtype: Mac address
lldp-remote-port-description: las1-router-1:et-0/0/50
lldp-remote-system-name: las1-router-1
- lldp-local-parent-interface-name: ae0
lldp-local-port-id: xe-0/0/1
lldp-remote-chassis-id: 11:11:11:11:11:11
lldp-remote-chassis-id-subtype: Mac address
lldp-remote-port-description: slc1-router-1-xe-0/0/1
lldp-remote-system-name: slc1-router-1
这产生:
ok: [localhost] =>
msg:
- slc1-router-1