Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alalinaci opened this issue Apr 18, 2021 · 1 comment

Comments

@alalinaci
Copy link

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
Copy link
Author

alalinaci commented Apr 18, 2021

I used jmespath simulator and came up with contains(*.["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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant