awkansibleadhoc

Ansible command to get yum history


Hello Ansible newbie here...I'm looking for an ansible way to get the last yum update date and write that out to a text file (later to be part of a web page). I know the raw command: hostname && yum history | awk 'NR==5 { print $7, $8 }' and this works fine running from a regular bash shell (RHEL version 7 and 8), however, when I try to put this in an ansible ad-hoc command I get errors:

localhost | FAILED | rc=1 >>
server: cmd. line:1: NR==5 { print ,  }
awk: cmd. line:1:               ^ syntax error
awk: cmd. line:1: NR==5 { print ,  }
awk: cmd. line:1:                  ^ syntax error
awk: cmd. line:1: NR==5 { print ,  }
awk: cmd. line:1:                   ^ unexpected newline or end of stringnon-zero return code 

Any suggestions? I am guessing escaping may help but at this point I'm not sure what to escape?


Solution

  • If you want to use an Ansible ad hoc command to gather all information from all the hosts and consolidate it in a local file you have to escape the $ in the awk command by prefixing them by a \.

    ansible all -m ansible.builtin.shell -a \
           "hostname && yum history | awk 'NR==5 { print \$7, \$8 }'" \
           >> result.txt