skip to Main Content

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


  1. 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:

    [group_startapache:children]
    host_1
    host_2
    ...
    [group_startweb:children]
    host_a
    host_b
    ...
    [group_startapache:vars]
    apache_path=/app/instances/sites/startapache.sh
    [group_startweb:vars]
    apache_path=/app/instances/startweb.sh
    

    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.

    Login or Signup to reply.
  2. If you are looking for a quick and simple solution, I would suggest using first_found lookup or with_first_found loop. For example:

    - name: Start apache using first found script
      command: "{{ item }}"
      with_first_found:
        - /app/instances/sites/startapache.sh
        - /app/instances/startweb.sh
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search