jmespath / jmespath.site

The repo for the jmespath.org website.
http://jmespath.org/
Apache License 2.0
56 stars 51 forks source link

Check multiple conditions for all keys, in the given nested dictionary using jmespath #86

Open alalinaci opened 3 years ago

alalinaci commented 3 years ago

Hi

I am completely new to jmespath. I've been trying to check conditions if for ALL nodes, ('Config state '=='NSHUT' and ('State '=='OPERATIONAL' or 'State '=='IOS XR RUN')). I am expecting a single Boolean value after the check. Following is the nested dictionary I'm trying this on:

{ "0/0/1": { "Config state ": "", "Node ": "0/0/1", "State ": "OK", "Type ": "NC55-MPA-12T-S" }, "0/0/CPU0": { "Config state ": "NSHUT", "Node ": "0/0/CPU0", "State ": "IOS XR RUN", "Type ": "NC55-MOD-A-S" }, "0/0/NPU0": { "Config state ": "", "Node ": "0/0/NPU0", "State ": "UP", "Type ": "Slice" }, "0/FC0": { "Config state ": "NSHUT", "Node ": "0/FC0", "State ": "OPERATIONAL", "Type ": "NC55-5508-FC" }, "0/FC1": { "Config state ": "NSHUT", "Node ": "0/FC1", "State ": "OPERATIONAL", "Type ": "NC55-5508-FC" }, "0/FC2": { "Config state ": "NSHUT", "Node ": "0/FC2", "State ": "OPERATIONAL", "Type ": "NC55-5508-FC" }, "0/FC3": { "Config state ": "NSHUT", "Node ": "0/FC3", "State ": "OPERATIONAL", "Type ": "NC55-5508-FC" }, "0/FC4": { "Config state ": "NSHUT", "Node ": "0/FC4", "State ": "OPERATIONAL", "Type ": "NC55-5508-FC" }, "0/FC5": { "Config state ": "NSHUT", "Node ": "0/FC5", "State ": "OPERATIONAL", "Type ": "NC55-5508-FC" }, "0/FT0": { "Config state ": "NSHUT", "Node ": "0/FT0", "State ": "OPERATIONAL", "Type ": "NC55-5508-FAN" }, "0/FT1": { "Config state ": "NSHUT", "Node ": "0/FT1", "State ": "OPERATIONAL", "Type ": "NC55-5508-FAN" }, "0/FT2": { "Config state ": "NSHUT", "Node ": "0/FT2", "State ": "OPERATIONAL", "Type ": "NC55-5508-FAN" }, "0/PM0": { "Config state ": "NSHUT", "Node ": "0/PM0", "State ": "OPERATIONAL", "Type ": "NC55-PWR-3KW-DC" }, "0/PM1": { "Config state ": "NSHUT", "Node ": "0/PM1", "State ": "OPERATIONAL", "Type ": "NC55-PWR-3KW-DC" }, "0/PM2": { "Config state ": "NSHUT", "Node ": "0/PM2", "State ": "OPERATIONAL", "Type ": "NC55-PWR-3KW-DC" }, "0/PM3": { "Config state ": "NSHUT", "Node ": "0/PM3", "State ": "FAILED", "Type ": "NC55-PWR-3KW-DC" }, "0/RP0/CPU0": { "Config state ": "NSHUT", "Node ": "0/RP0/CPU0", "State ": "IOS XR RUN", "Type ": "NC55-RP-E(Active)" }, "0/RP1": { "Config state ": "NSHUT", "Node ": "0/RP1", "State ": "SW_INACTIVE", "Type ": "NC55-RP-E" }, "0/SC0": { "Config state ": "NSHUT", "Node ": "0/SC0", "State ": "OPERATIONAL", "Type ": "NC55-SC" }, "0/SC1": { "Config state ": "NSHUT", "Node ": "0/SC1", "State ": "OPERATIONAL", "Type ": "NC55-SC" } }

Please help!

alalinaci commented 3 years ago

I used jmespath simulator and came up withcontains(*.["Config state "!='' && ("State "=='OPERATIONAL' || "State "=='IOS XR RUN')][], `false`), this should give me the expected result but while using it in ansible playbook, it produces the leaning toothpick syndrome, and it doesn't produce the expected output. The ansible task I wrote looks like:

  - name: Query to display nodes
    set_fact:
     display_nodes: "{{ parsed_output | json_query(jmesquery) }}"
    vars:
     jmesquery: "contains(*.[\"Config state \"!='' && (\"State \"=='OPERATIONAL' || \"State \"=='IOS XR RUN')][], `false`)"
  - debug:
     msg="{{ display_nodes }}"

Can you suggest a workaround?