How to install Apache on ubuntu using Ansible Playbook

Working as a I.T professional, We need to also required to install a single or multiple packages in our multiple server, Here we are about to create Ansible Playbook to install Apache2 web server

 

We believe, You have already installed Ansible if not you need to click on given link to install Ansible on ubuntu server. 


 

Step 1: Configure Ansible 

 

By default ansible host configuration file is /etc/ansible/hosts Here you need to add the Apache web server details like this, Use the following step for the same, In my case i have 1 apache server.

 

Group Name  – WebServer

IP Address     – 52.188.164.18

Username      – example

Password       – XXXXXXXXXX


You need to add your server in ansible host file, Syntax is given below, You need to open the /etc/ansible/hosts file with nano or vim text editor.

sudo vim /etc/ansible/hosts

and add the web server entry like this.

 

[webserver]
52.188.164.18 ansible_connection=ssh ansible_ssh_user=example ansible_ssh_pass=xxxxxxx

 

Change the credentials as per your environment, Save and exit from the vim text editor and verify the host machine connectivity with ansible by using the given command.

ansible all -m ping

You should get output like this.

 

 

Here our host configuration is completed and now we can go for next step to write play book.


Step 2: Creating Apache Playbook

 

You need to create a apache-playbook.yml and you change it according to your requirement.

sudo vim apache-playbook.yml

Paste the given code.

 

- hosts: webserver
  become: yes
  tasks:

    - name: update
      apt: update_cache=yes   
   
    - name: install apache2
      apt: name=apache2 state=latest

    - name: enabled mod_rewrite
      apache2_module: name=rewrite state=present

    - name: enabled proxy
      apache2_module: name=proxy state=present

    - name: enabled proxy_http
      apache2_module: name=proxy_http state=present
    
    - name: enabled ssl
      apache2_module: name=ssl state=present
    
    - name: enabled socache_shmcb
      apache2_module: name=socache_shmcb state=present

    - name: enabled mpm_event
      apache2_module: name=mpm_event state=present


      notify:
        - restart apache2

  handlers:
    - name: restart apache2
      service: name=apache2 state=restarte


Now we are ready to use the Apache’s PlayBook, To execute the PlayBook you need to use the given command.

ansible-playbook apache-playbook.yml

You should get output like this.

 

Verify the apache service on your targeted server by using the given command.

sudo systemctl status apache2


Sample output.


 You can also verify the apache packages by execution of given command.

dpkg --get-selections | grep apache2

Sample output.

Conclusion

We have successfully created Ansible playbook for Apache Web Server in our Ubuntu 18.04 server.


Get connected with our new devops tools based website- https://www.devopstricks.in/

How to install Apache on ubuntu using Ansible Playbook

2 thoughts on “How to install Apache on ubuntu using Ansible Playbook

Leave a Reply

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

Scroll to top