run a task when a play gets skipped by all hosts

19 views
Skip to first unread message

John Simmons

unread,
Apr 26, 2019, 8:09:01 AM4/26/19
to Ansible Project
Hi all

I have a playbook that checks 2 servers for log entrys, below is what is expected when it runs correctly: (part of the playbook below)

########################################

    - name: check logs for  Completion
      shell: "docker logs --since 13h ***_***_1 | grep '********' | cut -d' ' -f2,16-30"
      register: command_output


    - name: sucess notification message via Slack
      slack:
        token: "*******************************"
        msg: "some message"
        username: 'Ansible'
        link_names: 0
        parse: 'none'
        color: 'good' #normal, good, warning, danger
      delegate_to: localhost
      when: command_output.stdout | length > 0
      ignore_errors: yes

########################################

and this is the output form the command line:

TASK [success notification message via Slack] *****************************************************************
skipping: [10.*.0.138]
ok: [10.*.0.137 -> localhost]

########################################

As the task is looking in the logs on both of these hosts will only find the entry on 1 of the hosts, and therefore only send the message from the server that could actually find the log and not from both servers so that the slack channel is not getting bombarded with blank messages from the host that didn't have then log entry.



This is all fine until I want to run another play as an alert if both hosts skipped the task due to the log not existing on either host. So for example this:

TASK [success notification message via Slack] *****************************************************************
skipping: [10.*.0.137]
skipping: [10.*.0.138]

########################################

How could I get around this? and is it even possible?

If there was a way to check the status of a play and if that play had skipped all hosts then run another play that would be great.

Hope I have made this understandable, if not please let me know where i confused you :)

Kai Stian Olstad

unread,
May 8, 2019, 2:49:22 PM5/8/19
to ansible...@googlegroups.com
On 26.04.2019 14:09, John Simmons wrote:
> - name: sucess notification message via Slack
> slack:
> token: "*******************************"
> msg: "some message"
> username: 'Ansible'
> link_names: 0
> parse: 'none'
> color: 'good' #normal, good, warning, danger
> delegate_to: localhost
> when: *command_output.stdout | length > 0*
> ignore_errors: yes
>
> ########################################
>
> and this is the output form the command line:
>
> TASK [success notification message via Slack]
> *****************************************************************
> skipping: [10.*.0.138]
> *ok: [10.*.0.137 -> localhost]*
>
> ########################################
>
> As the task is looking in the logs on both of these hosts will only find
> the entry on 1 of the hosts, and therefore only send the message from the
> server that could actually find the log and not from both servers so that
> the slack channel is not getting bombarded with blank messages from the
> host that didn't have then log entry.
>
>
>
> This is all fine until I want to run another play as an alert if both hosts
> skipped the task due to the log not existing on either host. So for example
> this:
>
> TASK [success notification message via Slack]
> *****************************************************************
> *skipping: [10.*.0.137]*
>
> *skipping: [10.*.0.138]*
>
> ########################################
>
> How could I get around this? and is it even possible?


You would need to check them both with something like(list in when is the same as logical AND)

when:
- hostvars['10.*.0.137'].command_output.stdout | length > 0
- hostvars['10.*.0.138'].command_output.stdout | length > 0


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages