Install Apache Using Ansible Playbooks

Spread the love

Automating Web Server Deployment: A Guide to Installing Apache with Ansible

Automation is at the heart of modern IT, and Ansible stands out as a powerful tool in this environment. This popular automation tool allows for remote installation, configuration, and management of various systems and services, reducing the need for manual intervention on each server. In this guide, we will look at how to use Ansible to install and configure Apache2 on multiple servers at once using YAML Playbooks.

Setting Up the Environment

Before diving into the automation process, ensure you have Ansible installed on the master server. Additionally, make sure that SSH communication is established between the master server and the target hosts (slaves).

Ansible Playbook: Installing Apache2

Ansible Playbooks are written in YAML, utilizing a module-based format for efficient automation. Let’s create a playbook (apache2.yml) to install Apache2 on our target servers:

- hosts: servers
  sudo: yes
    - name: Install Apache2
      apt: name=apache2 update_cache=yes state=latest

    - name: Enable mod_rewrite
      apache2_module: name=rewrite state=present
        - restart apache2

    - name: Restart Apache2
      service: name=apache2 state=restarted


  • hosts: servers: Specifies the server group name.
  • sudo: yes: Grants sudo privileges for the tasks.
  • tasks: Defines a series of tasks to be executed.
  • handlers: Specifies handlers for notifications.

Save the playbook and run it using the following command:

ansible-playbook apache2.yml

This playbook installs Apache2 on all specified servers and ensures mod_rewrite is enabled. The handlers section restarts Apache2 if changes are detected.

PLAY [servers] ********************************************************************************

TASK [Gathering Facts] ************************************************************************
ok: [slave2]
ok: [slave1]

TASK [install apache2] ************************************************************************
changed: [slave1]
changed: [slave2]

TASK [enabled mod_rewrite] ********************************************************************
changed: [slave1]
changed: [slave2]

RUNNING HANDLER [restart apache2] *************************************************************
changed: [slave2]
changed: [slave1]

PLAY RECAP ************************************************************************************
slave1                     : ok=4    changed=3    unreachable=0    failed=0
slave2                     : ok=4    changed=3    unreachable=0    failed=0

Verifying the Installation

After the playbook execution completes successfully, Apache2 should be installed on the specified servers. Verify this by accessing the Apache2 welcome page through the web browser using the IP addresses of the slave servers.


Congratulations! You’ve successfully automated the installation of Apache2 on multiple servers using Ansible. This approach not only saves time but also ensures consistency across your infrastructure. In the next article, we’ll delve into deploying a static website with images using Ansible Playbooks. Keep reading and sharing our posts for more automation insights. Happy automating!

5 thoughts on “Install Apache Using Ansible Playbooks”

Leave a Comment