myol
myol

Reputation: 9828

Ansible use different hostname if first fails

I have a number of raspberry pis that I swap out (only one running at a time) and run ansible against. Most pis respond to ping raspberrypi but I have one that responds to ping raspberrypi.local

Rather than remembering to manually ping the correct hostname before executing the playbook, is there a way in ansible to run a playbook against a different hostname if the first fails?

Currently my playbook is

---
- hosts: raspberrypi

and /etc/ansible/hosts

[raspberrypi]
raspberrypi
#raspberrypi.local

If I uncomment out the second hostname and the first fails, then the playbook will fail and not run on the .local hostname

Upvotes: 0

Views: 226

Answers (2)

Nick
Nick

Reputation: 2034

You could run the play against both host groups.

- hosts: raspberrypi:raspberrypi.local

Upvotes: 0

error404
error404

Reputation: 2823

I am not sure if this is directly possible in ansible.

But a hack I can think of is to create a list of hosts store them in a variable do a ping using the localhost. If ping is successful create a custom hosts group and execute the task you want to do.

Also are you executing your playbook with serial: 1 ?

Hope so this helps.

Upvotes: 1

Related Questions