02-05-2021 01:32 PM
Hello all,
Does anyone know how to use variables in the -f parameter of moquery ?
For example, say I have defined a variable nodeId="paths-292" and am trying to use it. I have tried all the following with no luck.
I suspect it is some weirdness as the -f argument has to be in double-quotes, but the nodeId is already defined as a string :
apic1# nodeId="paths-292"
apic1#
apic1#
apic1#
apic1#
apic1# moquery -c fvRsCEpToPathEp -f 'fv.RsCEpToPathEp.dn*"$nodeId"'
No Mos found
apic1#
apic1#
apic1# moquery -c fvRsCEpToPathEp -f 'fv.RsCEpToPathEp.dn*"${nodeId}"'
No Mos found
apic1#
apic1#
apic1# moquery -c fvRsCEpToPathEp -f 'fv.RsCEpToPathEp.dn*$nodeId'
Error: no valid token found at (1, 21) '$nodeId'
apic1#
apic1#
apic1# moquery -c fvRsCEpToPathEp -f 'fv.RsCEpToPathEp.dn*${nodeId}'
Error: no valid token found at (1, 21) '${nodeId}'
apic1#
apic1#
If I use the regular string paths-292 in the -f parameter instead of the variable, I get results so I know there are MO's that fit the query.
If anyone has any ideas how to use variables in the -f parameter of moquery, let me know! Thanks!
Solved! Go to Solution.
02-05-2021 10:13 PM
Hi @vv0bbLeS ,
By now you are hoping someone wh really knows this stuff will answer you, not just me
BUT I really do think there are problems with the-f parameter of moquery but that aside, I thought I'd at least get you wasting some more time trying to figure it out with a short lesson on the intricicies of quoting in bash
The problem you are battling with in the examples about is that a single quoted string is always literal in bash, so the $nodeID variable is not going to get expanded using single quotes.
So the command:
moquery -c fvRsCEpToPathEp -f 'fv.RsCEpToPathEp.dn*"paths-292"'
will work for you because the string:
fv.RsCEpToPathEp.dn*"paths-292"
(including the quotes) gets passed to the -f filter
The problem is that if you replace the paths-292 with $nodeId (or ${nodeId}) it will get passed literally - as
fv.RsCEpToPathEp.dn*"$nodeId"
The good new is that the -f parameter is happy with double quotes - but you still need to keep the expanded variable in quotes.
Enter the escapre character - backslash \
This means you COULD enter the command
moquery -c fvRsCEpToPathEp -f 'fv.RsCEpToPathEp.dn*"paths-292"'
as
moquery -c fvRsCEpToPathEp -f "fv.RsCEpToPathEp.dn*\"paths-292\""
by "escaping" the embedded quotes.
And this will also work with variables - so in your example where you set nodeId to "paths-292", the following will work exactly the same way:
moquery -c fvRsCEpToPathEp -f "fv.RsCEpToPathEp.dn*\"${nodeId}\""
I was tempted to add a variable for "up" and "down" when I replied to your previous post, but thought the escaping syntax made it too hard - but then you went and dived into awk (which is a minefield in itself)
Anyway - I hope you come up with a better answer - make sure you share it if you do
02-05-2021 10:13 PM
Hi @vv0bbLeS ,
By now you are hoping someone wh really knows this stuff will answer you, not just me
BUT I really do think there are problems with the-f parameter of moquery but that aside, I thought I'd at least get you wasting some more time trying to figure it out with a short lesson on the intricicies of quoting in bash
The problem you are battling with in the examples about is that a single quoted string is always literal in bash, so the $nodeID variable is not going to get expanded using single quotes.
So the command:
moquery -c fvRsCEpToPathEp -f 'fv.RsCEpToPathEp.dn*"paths-292"'
will work for you because the string:
fv.RsCEpToPathEp.dn*"paths-292"
(including the quotes) gets passed to the -f filter
The problem is that if you replace the paths-292 with $nodeId (or ${nodeId}) it will get passed literally - as
fv.RsCEpToPathEp.dn*"$nodeId"
The good new is that the -f parameter is happy with double quotes - but you still need to keep the expanded variable in quotes.
Enter the escapre character - backslash \
This means you COULD enter the command
moquery -c fvRsCEpToPathEp -f 'fv.RsCEpToPathEp.dn*"paths-292"'
as
moquery -c fvRsCEpToPathEp -f "fv.RsCEpToPathEp.dn*\"paths-292\""
by "escaping" the embedded quotes.
And this will also work with variables - so in your example where you set nodeId to "paths-292", the following will work exactly the same way:
moquery -c fvRsCEpToPathEp -f "fv.RsCEpToPathEp.dn*\"${nodeId}\""
I was tempted to add a variable for "up" and "down" when I replied to your previous post, but thought the escaping syntax made it too hard - but then you went and dived into awk (which is a minefield in itself)
Anyway - I hope you come up with a better answer - make sure you share it if you do
02-08-2021 02:17 PM
@RedNectar I think you know plenty about ACI! I was figuring by now folks would be hoping I would quit clogging the forum with these trivial questions!
And ah yes, the single quotes, I'm not sure how I missed that. PowerShell is the same way if you've ever coded in it, it treats single quotes as string literals, which of course can be useful but can also be a huge pain. ( Python of course doesn't make that quote-type distinction ).
I think your solution of just using 2 pairs of double quotes and escaping the "inner" double quotes is a good solution. For fun I tried expanding on your idea by having 2 variables and nesting them, e.g. nodeId and nodeIdStr , with nodeId being something simple like "292" and nodeIdStr being the actual string with escaped quotes in it. My plan was that if I needed to change the node ID value I could just edit the easier variable nodeId, but it doesn't look like ACI dynamically changes nested variables (unless I'm missing something, which is entirely possible):
apic1# nodeId="292"
apic1#
apic1#
apic1# nodeIdStr="\"paths-${nodeId}\""
apic1#
apic1#
apic1# print ${nodeIdStr}
"paths-292"
apic1#
apic1#
apic1# nodeId="291"
apic1#
apic1#
apic1# print ${nodeIdStr}
"paths-292"
apic1#
So, from my testing, it would be best to just stick to the one variable, e.g. "paths-292" and, per your solution above, include the single variable with escaped double-quotes in the -f parameter. Thanks again!
02-08-2021 03:10 PM
I think what you are trying to achieve can be done like this:
nodeId="291" moquery -c fvRsCEpToPathEp -f "fv.RsCEpToPathEp.dn*\"paths-${nodeId}\"" nodeId="292" moquery -c fvRsCEpToPathEp -f "fv.RsCEpToPathEp.dn*\"paths-${nodeId}\""
02-09-2021 12:44 PM - edited 02-09-2021 12:46 PM
@RedNectar yes sir, i was just trying to nest variable assignments to see if it would work, but I discovered you would have to reassign each variable anytime you wanted to change them as ACI doesn't dynamically update variables that contain nested variables as part of them. Fun times!
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide