20220202-ansible_reachable_hosts.md (1588B)
1 I use AWX for Ansible in work quite a bit. We have a number of workflows that will run on multiple hosts. One issue we had was that some systems may be offline when the templates in the workflow run, and this would result in a template (and ultimately a workflow) failure even though all the other systems were successful. 2 3 Stackoverflow to the rescue! Thanks to Alex Cohen for [this solution](https://stackoverflow.com/a/55190002). 4 5 To combat the offline hosts the playbook can be modified to perform a check on the inventory first, loading any online systems into a "reachable" group. The rest of the playbook would only be run against the online systems. 6 7 ``` 8 --- 9 - hosts: all 10 gather_facts: false 11 tasks: 12 - block: 13 - wait_for_connection: 14 timeout: 5 15 - group_by: 16 key: "reachable" 17 tags: always 18 check_mode: false 19 rescue: 20 - debug: 21 msg: "unable to connect to {{ inventory_hostname }}" 22 23 - hosts: reachable 24 tasks: 25 - name: normal playbook tasks from here 26 ... 27 ``` 28 29 As you can see this is achieved using the `block` feature in Ansible. The `key` parameter in the `group_by` module specifies the name of our ad-hoc inventory group, in this case "reachable". 30 31 Using the `debug` module message in `rescue` allows us to mark the offline systems as rescued so it doesn't fail the playbook. Then the rest of the playbook is run against all systems in the "reachable" group. 32 33 This now doesn't cause my workflows to fail and I don't have to explain why I'm not concerned when there is red on the dashboard (¬_¬). 34