Trying to setup Ansible to restart apache on multiple hosts. But in one set of 50 hosts I’ve apache start script in /app/instances/sites/startapache.sh but in other 50 hosts I’ve /app/instances/startweb.sh. Is it possible to trigger any apache configuration which will re-start the apache irrespective of the path?
2
Answers
Using well the inventory, combined with custom vars, allow you to do this.
An option is thus having 2 different groups, and a variable declared for each group with the full path to the start script. Doing so is trivial and you will have all the necessary elements in https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
As an example, here is a piece of an inventory:
Thus, in you playbook, you will refer to
{{ apache_path }}
in any shell or command task.But if it is possible, you should create an init.d/systemd (depending on you system) service with this value, allowing you to just to an “apache start”.
Another way would be to use stat to know which script is present, but may lead to problem if you have any host with both scripts.
If you are looking for a quick and simple solution, I would suggest using first_found lookup or with_first_found loop. For example:
This approach won’t require any inventory changes or other code enhancements.
Just a small aside note here. I also support the idea of using a standard systemd config instead. In addition to simplifying your code it brings the whole power of systemd (assuming you are using some major Linux flavour released after 2015) into the management of your service.