Last Updated: February 25, 2016
·
9.216K
· eosrei

Check Apache HTTP config before reloading

If making any changes to the Apache HTTP config, be sure to test the config before reloading. Force failure when there is a problem.

- name: Apache | Enable the new site
  command: a2ensite site-name creates=/etc/apache2/sites-enabled/site-name

- name: Apache | Check the apache configuration
  command: apache2ctl configtest
  register: apache_result
  ignore_errors: yes

- name: Apache | Reverting - Disable new site
  action: command a2dissite site-name
  when_failed: $apache_result

- name: Apache | Reverting changes - Ending playbook
  action: fail msg="Apache configuration is invalid. Please check before re-running the playbook."
  when_failed: $apache_result

- name: Apache | Reload Apache
  service: name=apache2 state=reloaded