简体   繁体   中英

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 }' 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?

I'm looking for an ansible way to get the last yum update date and write that out to a text file

 - shell: "yum history | awk 'NR==5 { print $7, $8 }' >> /tmp/yum_history.txt"
   args:
     warn: false

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM