0

When I execute the following command to find index IDs by using an Ansible ad-hoc command:

$ ansible oamvip -i /storage/inventory/None/${myvnf}hosts --become -m shell -a "/usr/sbin/aladmin list | grep "PartitionUsage" | awk '{ print \$2 }'"
xxx.xx.xxx.xxx | CHANGED | rc=0 >>
4818b4715eb6542838023
e520bec15eb6542838023 

However, when I try to execute a shell command (sh) on the results from the awk output, Ansible shows me the help menu:

$ ansible oamvip -i /storage/inventory/None/${myvnf}hosts --become -m shell -a "/usr/sbin/aladmin list | grep "PartitionUsage" | awk '{ print "aladmin show " \$2 | "sh"}'"
Usage: ansible <host-pattern> [options] 

I've tried single and double quotes, backslashes, to no avail. Please advise.

3
  • 2
    Looks like your awk command is malformed. If I have to guess you need to escape the double quotes in the awk command. Also, I think you need a comma after the "aladmin show" $ ansible oamvip -i /storage/inventory/None/${myvnf}hosts --become -m shell -a "/usr/sbin/aladmin list | grep "PartitionUsage" | awk '{ print \"aladmin show \", \$2 | \"sh\"}'"
    – ventsyv
    Nov 19, 2021 at 19:49
  • @ventsyv Without the comma awk just combines the arguments to print. With the comma they get separated by OFS (space). Nov 19, 2021 at 20:00
  • @HaukeLaging My awk is very rusty...
    – ventsyv
    Nov 19, 2021 at 21:12

1 Answer 1

1
"/usr/sbin/aladmin list | grep "PartitionUsage" | awk '{ print \$2 }'"

is just one string (word) from the perspective of the shell which parses the whole command line. I assume it is passed to sh -c. Independent of your problem, it should be

"/usr/sbin/aladmin list | grep \"PartitionUsage\" | awk '{ print \$2 }'"

or

"/usr/sbin/aladmin list | grep PartitionUsage | awk '{ print \$2 }'"

instead.

set -x
: "/usr/sbin/aladmin list | grep "PartitionUsage" | awk '{ print "aladmin show " \$2 | "sh"}'"
+ : '/usr/sbin/aladmin list | grep PartitionUsage | awk '\''{ print aladmin' show ' $2 | sh}'\'''

Those are three words for the shell and thus for ansible which does not understand these arguments.

It's again the ".

awk '{ print "aladmin show " \$2 | "sh"}'

must become

awk '{ print \"aladmin show \" \$2 | \"sh\"}'

BTW

There is (nearly in general and especially in this case) no reason for a

... | grep ... | awk ...

pipeline as (with few exceptions like PCRE) awk can do everything that grep can.

grep PartitionUsage | awk '{ print \$2 }'

=>

awk '/PartitionUsage/ { print \$2 }'
3
  • Awesome Hauke, your solution worked perfectly! Also got rid of 'grep'.
    – Bjoern
    Nov 19, 2021 at 20:13
  • @Bjoern Then you should accept my answer so that your question does not look unanswered any more. Nov 19, 2021 at 20:23
  • Sorry, had to change PCs, for some reason this page won't reload on my other PC.
    – Bjoern
    Nov 19, 2021 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .