Ansible Reload Apache on multiple servers

Sometimes you’d like to reload Apache after a config change on several servers. ansible adhoc commands makes this pretty simple.

Step 1. Install Ansible

I like to install ansible in a virtualenv with pip.

Create a VirtualEnv

Install pre-reqs

  • Ubuntu: apt-get update && apt-get install python-apt python-pip build-essential python-dev git python-virtualenv -y
  • RHEL/CentOS: yum install python-pip git python-devel python-virtualenv gcc -y

Setup VirtualEnv

virtualenv /root/ansible
cd /root/ansible; source bin/activate

Install Ansible with Pip:

pip install paramiko PyYAML jinja2 httplib2 ansible

Step 2. Create the Inventory File

Next we setup the inventory file at /root/ansible/hosts.

Example inventory file:

web01  ansible_connection=local
web02 ansible_host=
db01 ansible_host=



Step 3. Test

Here is the basics for running a adhoc command with ansible:

ansible <host-pattern> [-i inventory_file] [-m module_name] [-a module_args]


ansible web -i /root/ansible/hosts -m command -a ping

Note: The ansible ping module is not an ICMP ping. Instead, it tests Ansible’s connection to the hosts; it is an application-level ping.

Step 4. Reload Apache

Check Apache config file syntax

  • Ubuntu: ansible web -i /root/ansible/hosts -m command -a "apachectl -t"
  • RHEL/CentOS: ansible web -i /root/ansible/hosts -m command -a "httpd -t"

Reload Apache

  • Ubuntu: ansible web -i /root/ansible/hosts -m service -a "name=apache2 state=reloaded"
  • RHEL/CentOS: ansible web -i /root/ansible/hosts -m service -a "name=httpd state=reloaded"

jim has written 83 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>