How to get and parse the output from target hosts

28 views
Skip to first unread message

Jörg Kastning

unread,
Sep 14, 2016, 1:40:42 PM9/14/16
to Ansible Project
Hello,

I would like to write a playbook to install Red Hat Security Advisory on my hosts. The idea is to put the published information about the advisories with their Numbers (e.g. RHSA-2016:1234) in a variable and run the following playbook on all hosts:
---
- hosts: all

  tasks
:
   
- name: Group by OS
      group_by
: key=os_{{ ansible_distribution }}
      changed_when
: False

- hosts: os_RedHat
  vars
:
    RHSA
: "RHSA-2016:1633, RHSA-2016:1626"
  tasks
:
   
- name: Install Red Hat Security Advisory
      command
: yum update-minimal --advisory {{ RHSA }}
     
register: yumoutput

   
- name: Reboot if packages were updated
      command
: reboot NOW
     
when: yumoutput = ???

As you could see I would like to reboot a host only in case packages were updated. As far as I know the last line in the yum output on the host should be "Complete!". But I have trouble to get only the last line from the output and use it in the conditional when clause.

Could someone help me here, please?

Because english is not my first language I may have difficulties to make my point. Please bare with me and ask for more details or points I have to make more clear.

Kind regards,
Joerg

Alexander H. Laughlin

unread,
Sep 14, 2016, 7:38:15 PM9/14/16
to Ansible Project
I believe something like:

- name: Reboot if packages were updated
  command
:
reboot NOW
 
when: "'Complete!' in yumoutput"


would do the trick.



That is assuming that you don't have the string 'Complete!' turning up anywhere else in the output.

Jörg Kastning

unread,
Sep 15, 2016, 4:53:02 AM9/15/16
to Ansible Project
Hi Alexander,

you put me in the right direction. I'd like to give a short example for my solution:
---
- name: show return value of command yum
  hosts
: e-stage
  tasks
:
   
- name: command
      command
: yum update-minimal --advisory RHSA-2016:1234
     
register: yumlog

   
- command: reboot NOW
     
when: '"Completed!" in "{{ yumlog.stdout_lines[-1] }}"'

With "{{ yumlog.stdout_lines[-1] }}" I got the last item from the list and do not have to think about the case when it occures somewhere else in the output.

Greetz
Joerg
Reply all
Reply to author
Forward
0 new messages