local_action when unreachable.

493 views
Skip to first unread message

William McKenzie

unread,
Aug 3, 2017, 9:02:56 AM8/3/17
to Ansible Project
Seems like this should be pretty simple, but I can't seem to find a way to do it. Is there some way to perform a local_action whenever a target is unreachable? I just want to post to a slack channel if ping fails.

~Bill

Nick Kakouros

unread,
Aug 3, 2017, 3:01:48 PM8/3/17
to Ansible Project
You could probably do this with sth like this:

(in a playbook)

- host: unreachable_host
  gather_facts: false
  pre_tasks:
  - block:
      - name: Gathering facts
        setup:
    rescue:
      - name: local task
        debug:
          msg: .....

William McKenzie

unread,
Aug 4, 2017, 8:13:34 AM8/4/17
to ansible...@googlegroups.com
Don't think so. If a host unreachable, rescue never happens.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/v7sfxHaKbGQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/80167fe3-7a8f-4d4c-aead-11331ea55481%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

William McKenzie

unread,
Aug 4, 2017, 11:45:32 AM8/4/17
to Ansible Project
OK, this works: you store the results, clear errors on hosts and look for problem children.


---
- hosts: all
 gather_facts: no
 tasks:
   - block:
      - setup:        
       register: result    
     
    - name: clearing host errors
     meta: clear_host_errors
   
   - block:
     - local_action:
         module: slack
         token: "redacted"
         msg: "Ping failed on host {{inventory_hostname}}"
     when: result.unreachable is defined


Kai Stian Olstad

unread,
Aug 4, 2017, 12:29:31 PM8/4/17
to ansible...@googlegroups.com
On 04. aug. 2017 17:45, William McKenzie wrote:
> OK, this works: you store the results, clear errors on hosts and look for
> problem children.
>
>
> ---
> - hosts: all
> gather_facts: no
> tasks:
> - block:
> - setup:
> register: result
>
> - name: clearing host errors
> meta: clear_host_errors
>
> - block:
> - local_action:
> module: slack
> token: "redacted"
> msg: "Ping failed on host {{inventory_hostname}}"
> when: result.unreachable is defined
It will only work if at least one host is reachable, but that's probably
good enough.

By the way, you don't need your two block: since they only have one task
each.

--
Kai Stian Olstad

William McKenzie

unread,
Aug 4, 2017, 1:18:03 PM8/4/17
to Ansible Project, ansible-pr...@olstad.com
Thanks. Yes, I had the blocks there because I had been playing with the rescue approach earlier.
Reply all
Reply to author
Forward
0 new messages