Apache Graceful restart with Ansible

Viewed 2705

What is ideal ansible way to do a apache graceful restart?

   - name: Restart Apache gracefully 
      command: apachectl -k graceful

Ansible systemd module does the same? If not, what is the difference? Thanks !

- name: Restart apache service.
  systemd:
    name: apache2
    daemon_reload: yes
    state: restarted
2 Answers

What you can do with Ansible is to ensure that all established connections to Apache are closed (drained in Ansible lingo).

Use the wait_for module with the condition to wait for drained connections on the particular host and port, with the state set to drained. See below:

- name: wait until apache2 connections are drained.
  wait_for:
   host: 0.0.0.0
   port: 80
   state: drained

Note: You can use this for all your Linux network services, which becomes very handy if you want to shutdown services in a particular order in your Ansible playbook.

The wait_for directive is useful to ensuring that Ansible does not run your playbook until specific steps are completed.

There is not support of graceful state at this moment in service or systemd modules because this is quite specific to certain services, status is limited to started, stopped, restarted reloaded and running. So now you need to use a command module as you wrote in the question to perform a graceful restart, this is the only proper solution.

However there is an issue to support custom status, perhaps someone will implement that soon.

Related