Ansible Playbook to Install and Setup Apache on Ubuntu

Ansible is an open-source configuration management and application deployment tool. It helps to reduce managerial overhead by automating the deployment of the app and managing IT infrastructure.

Using ansible we are going to install apache2 web server in Ubuntu  20.04. For which we need to create a configuration in YAML syntax called Ansible playbooks

Normally, there is a control node and host nodes. Ansible is installed in the control node and will execute the playbook to deploy in host nodes. In this lab, we are going to install and use it in a single node.  

How To Install WordPress On MAMP Se...
How To Install WordPress On MAMP Server | Create Locally WordPress Website | PROWEBTIPS

Installing ansible on Ubuntu

First, update your system. The following command fetches the latest package:

$ sudo apt update

Ansible is available in the base repository of Ubuntu 20.04. To install ansible run the following command:

$ sudo apt install ansible -y

Then, check the ansible version using the command,

$ ansible --version
Ansible version
Ansible version

The host configuration file is stored in /etc/ansible/hosts file. Add your localhost server IP to the file with the help of the following command. Make sure you have a sudo user. In our case, 'ubuntu' has sudo privilege. Here,  I have given my name as 'myserver'  you can give your own. Also, remember the user ubuntu is sudo user in the system.   

$ echo "myserver ansible_host= ansible_user=ubuntu" >> /etc/ansible/host

Just check it by using the command,

$ ansible myserver -m ping 

You should get a pong response similar to the screenshot below.

Ansible ping pong
Ansible ping pong response

Creating a Playbook

Now, lets' create a playbook named 'apache.yml' to install and run the latest apache web server. 

$ vi apache.yml

Add the following content to the file

- hosts: myserver
  become: true
         - vars/default.yml
- tasks:
    - name: Install latest version of Apache
      apt: name=apache2 update_cache=yes state=latest

    - name: Create document root for your domain
        path: "/var/www/{{ http_host }}"
        state: directory
        owner: "{{ app_user }}"
        mode: '0755'

    - name: Copy your index page
        src: "files/index.html.j2"
        dest: "/var/www/{{ http_host }}/index.html"

    - name: Set up virtuahHost
        src: "files/apache.conf.j2"
        dest: "/etc/apache2/sites-available/{{ http_conf }}"
      notify: restart-apache

    - name: "UFW firewall allow HTTP on port {{ http_port }}"
        rule: allow
        port: "{{ http_port }}"
        proto: tcp

    - name: restart-apache
        name: apache2
        state: restarted


  • host: is hostname as you defined in /etc/ansible/hosts file
  • become: true is privilege escalation for being sudo
  • vars: for defining the variables
  • tasks: four tasks are defined. Installing apache, copying index page, setting up virtual host, allowing http from UFW firewall if enabled already.
  • handlers: To restart apache if all the other tasks are successfull.

Along with playbook, you should define three other file vars/default.yml, files/apache.conf.j2 and files/index.html.j2 . Let's create them one by one.


The default.yml file contains domain name, configuration file location and http port. Create the file and add the following.

http_host: "mydomain.local"
http_conf: "my_domain.conf"
http_port: "80"


The apache config template contains configuration for virtual host. The variable are defined within {{ }} and is obtained from vars file defined above. Create the file apache.conf.js and add the following content.

<VirtualHost *:{{ http_port }}>
   ServerAdmin webmaster@localhost
   ServerName {{ http_host }}
   ServerAlias www.{{ http_host }}
   DocumentRoot /var/www/{{ http_host }}
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined


Another template is your index file. Create the file and add the following content. You can modify as per your requirement.

       <title>{{ http_host }}</title>
       <h1>Welcome to {{ http_host }}</h1>

Now you can execute ansible-playbook with the following command.

$ ansible-playbook apache.yml --ask-become-pass

You will be prompt to enter a password for the sudo user. The status should be ok as shown below.

Playbook output
Playbook output

This will also run apache on default port 80,  you can verify browsing localhost in your favorite browser.

index page


Ansible is a helpful tool as it is agentless and writing configuration is easy. You can browse more configurations from ansible official documentation.  Please do comment if you have any issues while writing the playbook on ansible.

Leave a Comment