I need to accomplish following shell script. I trying to extract the HOSTNAME after it runs Successful via Ansible Playbook Run.

I have text file which contains Ansible-Playbook run command to be executed and writing the output into the log file: result.log

Here is what's the "result.log" file looks like

PLAY RECAP *********************************************************************
TESTLINUX01                : ok=6   changed=1    unreachable=0    failed=0

If the failed is "0", Unreachable is "0" and Changed is more then 0, then print the HOSTNAME only. In this case, TESTLINUX01

Thank you for your help.

closed as off-topic by mikewhatever, user117103, Zanna, Eric Carvalho, user364819 Jun 7 '17 at 19:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – mikewhatever, guntbert, Zanna, Eric Carvalho, Community
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    There's not really enough information in your question, but I'd guess something like awk '/failed=0/ && /unreachable=0/ && !/changed=0/ {print $1}' result.log would do it – steeldriver Jun 6 '17 at 20:02
  • thanks, I just tried and getting no results. Here is the entired log file – netkool Jun 6 '17 at 21:59
  • Ask about Mac OSX on Unix & Linux or Ask Different - many command line utilities are not the same – Zanna Jun 7 '17 at 4:45

You can use someting like this:



changed=`grep -Po "changed=\K\d+" $file`
unreachable=`grep -Po "unreachable=\K\d+" $file`
failed=`grep -Po "failed=\K\d+" $file`

if [ $changed -ge 1 -a $unreachable -eq 0 -a $failed -eq 0 ]
  cut -s -f1 -d: $file | tr -s ' '

First we extract all necessary values then we compare them with your desired ones, if they where match we print out the host name.

  • grep -Po "changed=\K\d+ returns the number in front of "changed"
  • IF statement:
    • $changed -ge 1 if changed is greater that equal "1"
    • -a and
    • $unreachable -eq 0 unreachable was equal to "0"
    • -a and
    • $failed -eq 0 failed was equal to "0", Then:
  • cut -s -f1 -d: $file | tr -s ' ' prints the hostname
  • You're welcome, you can accept the answer if you think it was helpful to you ;) – Ravexina Jun 6 '17 at 21:12
  • Hello Ravexina, I'm working on MAC and the grep -Po doesn't work. I tried using: 'grep -Eoi "changed=\d+"' $LOGFILE '| grep "[0-9]"' I get an empty result. Is there any way to only get the digit from the "changed" – netkool Jun 6 '17 at 22:06
  • These only work on -P, you can use sed instead grep failed result.log | sed -r "s/.*changed=([0-9]*).*/\1/" – Ravexina Jun 6 '17 at 22:15
  • grep failed result.log | sed "s/.*changed=([0-9]*)*.\/1/" sed: 1: "s/.*changed=([0-9]*)*.\/1/": unterminated substitute in regular expression – netkool Jun 6 '17 at 22:41
  • I sugesst sed -r not sed ;) – Ravexina Jun 7 '17 at 7:56

Thank you all for replying and providing the solution. Following code works out for me:

cat $file
$file >> $LOGFILE

SUCCESS=`grep "unreachable=0    failed=0" $LOGFILE | awk '{printf "%s ", $1;}'`
echo "Success: $SUCCESS"

FAILURE=`grep -E "unreachable=0    failed=[1-9]" $LOGFILE | awk '{printf "%s ", $1;}'`
echo "Failure: $FAILURE"

Unreachable=`grep -E "unreachable=1    failed=0" $LOGFILE | awk '{printf "%s ", $1;}'`
echo "Unreachable: $Unreachable"

Not the answer you're looking for? Browse other questions tagged or ask your own question.