• TechOps

    Need clarity?
    Chat with our experts now

    • Web Hosting SupportWeb Hosting Support
    • Helpdesk Support

      Skilled and professional 24/7 helpdesk support

    • Product Support

      Boost your product support with our expertise

    • Managed ServicesManaged Services
    • Server Management

      Don't let server issues slow you down. Let us manage them for you

    • Server Monitoring

      Safeguard your server health with our comprehensive monitoring solutions

    • Staff AugmentationStaff Augmentation
    • Hire an Admin

      Transform your business operations with our expert administrative support

    • Hire a Team

      Augment your workforce with highly skilled professional from our diverse talent pool

  • CloudOps

    Confused?
    Discuss with our sales team now.

    • Author ProfileAWS
      Well Architected Review
    • Author ProfileFinOps As a Service

      FinOps As a Service

    • Migrate

      Upgrade the journey: Migrate & Modernize seamlessly

    • Modernize

      Effortless CloudOps mastery for seamless cloud management

    • Optimize

      Efficient CloudOps: Boosting performance through optimization

    • Manage

      Simplify compliance complexities with our dedicated service

  • DevOps

    How Supportsages
    is creating an
    impact?

    View Casestudies
    • Author Profile24/7 DevOps As a Service

      Round-the-clock DevOps for uninterrupted efficiency

    • Author ProfileCI/CD Pipeline

      Automated CI/CD pipeline for seamless deployments

    • Author ProfileInfrastructure As a Code

      Crafting infrastructure with ingenious code

    • Author ProfileDevSecOps

      Integrated security in continuous DevOps practices

    • Author ProfileHire DevOps Engineers

      Level up your team with DevOps visionaries

    • Author ProfileConsulting Services

      Navigate success with expert DevOps consulting

  • SecOps

    Expert SecOps Services
    for any Scale

    • Author ProfileVAPT

      Vulnerability Assessment and Penetration Testing

    • Author ProfileSource Code Review

      Ensuring source code security and safe practices to reduce risks

    • Author ProfileSecurity Consultation

      On demand services for improving server security

    • Author ProfileSystem Hardening

      Reduced vulnerability and proactive protection

    • Author ProfileManaged SOC

      Monitors and maintains system security. Quick response on incidents

    • Author ProfileCompliance as a Service

      Regulatory compliance, reduced risk

  • Insights

    Explore our latest
    insights and resources

    Blog

    Explore our latest articles and insights

    Case Studies

    Read about our client success stories

  • Contact Us

  • About
  • Certifications
  • Life at Supportsages
  • Events
  • Contact
  • Careers
  • Blog

  • Dedicated Support Team
  • Quasi-dedicated Support Team
  • Hire a DevOps Engineer
  • Hire a Billing Support Staff
  • Per-ticket Support Plan
  • Managed Services

  • Microsoft Azure Expert
  • AWS Cloud Expert
  • Hire a developer
SS

SupportSages

Bites of wisdom @ work


Copyright © 2008 - 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy

How to install apache on ubuntu 20.04 using ansible roles.

TECH

  • 7 min read
How to install apache on ubuntu 20.04 using ansible roles.

Generating audio, please wait...

Roles let you automatically load related vars, files, tasks, handlers, and other Ansible artifacts based on a known file structure. After you group your content into roles, you can easily reuse them and share them with other users.

Prerequisites

In order to execute the automated setup provided by the playbook we’re discussing in this guide, you’ll need:

  • One Ansible control node: an Ubuntu 20.04 machine with Ansible installed and configured to connect to your Ansible hosts using SSH keys. Make sure the control node has a regular user with sudo permissions. To set up Ansible, please follow https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-ubuntu
  • One or more Ansible Hosts: one or more remote Ubuntu 20.04 servers previously set up.

How do we create Ansible Roles?

To create an Ansible role, use the ansible-galaxy command which has the templates to create it. This will create it under the default directory /etc/ansible

/roles and do the modifications else we need to create each directory and file manually. The client ansible-galaxy is included in Ansible. The Galaxy client allows you to download roles from Ansible Galaxy, and also provides an excellent default framework for creating your own roles.

ansadmin@ThinkPad-X240:~$ sudo ansible-galaxy init /etc/ansible/roles/apache
- Role /etc/ansible/roles/apache was created successfully List out the directory created under /etc/ansible/roles.
ansadmin@ThinkPad-X240:/etc/ansible/roles$ tree /etc/ansible/roles/apache
/etc/ansible/roles/apache defaults
main.yml handlers
main.yml meta
main.yml README.md
tasks
main.yml tests
inventory test.yml
vars
main.yml

6 directories, 8 files

Here, ansible-galaxy is the command to create the roles using the templates, init is to initialize the role, and here apache is the name of the role. No, we can add files and modify the role according to our use case. List out the directory created under /etc/ansible/roles/apache.

ansadmin@ThinkPad-X240:~$ tree /etc/ansible/roles/apache
/etc/ansible/roles/apache defaults
main.yml files
apache2.original index.html
handlers
main.yml meta
main.yml README.md
tasks
configure.yml install.yml main.yml service.yml
templates tests
inventory test.yml
vars
main.yml

8 directories, 13 files

We have got a clean directory structure with the ansible-galaxy command. Each directory must contain a main.yml file. By default, Ansible will look in each directory within a role for a main.yml file for the relevant content.

Directory Structure:

  • tasks – contains the main list of tasks to be executed by the role.
  • handlers – contains handlers, which may be used by this role or even anywhere outside this role.
  • defaults – default variables for the role.
  • vars – other variables for the role. Vars have a higher priority than defaults
  • files – contains files required to transfer or deployed to the target machines via this role.
  • templates – contains templates that can be deployed via this role.
  • meta – defines some data/information about this role (author, dependency, versions, examples, etc,.)

 

You can see the following playbook example to install and configure apache2 on ubuntu 20.04.

Let’s convert this playbook code into an Ansible role.

---
-	hosts: all become: yes tasks:
-	name: install apache2 package apt: name=apache2 state=latest
-	name: copy apche configuration file
copy: src=/data/httpd.original dest=/etc/httpd/conf/httpd.conf
-	name: copy index.html file
copy: src=/data/index.html dest=/var/www/html notify:
- restart apache
-	name: start and enable httpd service
service: name=apache2 state=restarted enables=yes handlers:
-	name: restart apache
service: name=apache2 state=restarted

First, move on to the Ansible roles directory and start editing the yml files.
1.Tasks
Edit main.yml available in the tasks folder to define the tasks to be executed.

ansadmin@ThinkPad-X240:/etc/ansible/roles/apache/tasks$ cat main.yml
---
-	name: install apache2 package apt: name=apache2 state=latest
-	name: copy apche configuration file
copy: src=/data/httpd.original dest=/etc/httpd/conf/httpd.conf
-	name: copy index.html file
copy: src=/data/index.html dest=/var/www/html notify:
- restart apache
-	name: start and enable httpd service
service: name=apache2 state=restarted enables=yes

Altogether, you can add all your tasks in this file or just break the codes even more as below using “import_tasks” statements.

ansadmin@ThinkPad-X240:/etc/ansible/roles/apache/tasks$ cat main.yml
---
# tasks file for /etc/ansible/roles/apache

-	import_tasks: install.yml
-	import_tasks: configure.yml
-	import_tasks: service.yml

Let’s create install.yml, confgure.yml, service.yml included in the main.yml with actions in the same directory.
install.yml

ansadmin@ThinkPad-X240:/etc/ansible/roles/apache/tasks$ cat install.yml
---
- name: install apche2 package apt: name=apache2 state=latest

configure.yml

ansadmin@ThinkPad-X240:/etc/ansible/roles/apache/tasks$ cat configure.yml
---
-	name: copy apache configuration file
copy: src=files/apache2.original dest=/etc/apache2/sites-available/thenaturepalette.conf
-	name: copy index.html file
copy: src=files/index.html dest=/var/www/thenaturepalette/
-	name: rename the default apache configuration file to disable it
command: "mv /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default. conf_bak"
-	name: create symlink for new configuration file file:
src: /etc/apache2/sites-available/thenaturepalette.conf dest: /etc/apache2/sites-enabled/thenaturepalette.conf state: link
notify:
- restart apache

service.yml

---
- name: start and enable apache2 service
service: name=apache2 state=restarted enabled=yes

2.Files
Copy the required files (httpd.conf and index.html) to the files directory.

ansadmin@ThinkPad-X240:/etc/ansible/roles/apache/files$ ls -l total 8
-rw-r--r-- 1 root root 284 Feb 13 22:29 apache2.original
-rw-r--r-- 1 root root 46 Feb 14 21:59 index.html
ansadmin@ThinkPad-X240:/etc/ansible/roles/apache/files$ cat apache2.original

ServerAdmin webmaster@localhost ServerName thenaturepalette.xyz ServerAlias www.thenaturepalette.xyz DocumentRoot /var/www/thenaturepalette/ ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

ansadmin@ThinkPad-X240:/etc/ansible/roles/apache/files$ cat index.html This is a HTML page for testing ansible role.

3.Handlers
Edit handler’s main.yml to restart the server when there is a change. Because we have already defined it in the tasks with notify option. Use the same name “restart apache” within the main.yml file as below.

ansadmin@ThinkPad-X240:/etc/ansible/roles/apache/handlers$ cat main.yml
---
# handlers file for /etc/ansible/roles/apache
- name: restart apache
service: name=apache2 state=restarted

4.Meta
Edit meta main.yml to add the information about the roles like author, descriptions, license, platforms supported.

ansadmin@ThinkPad-X240:/etc/ansible/roles/apache/meta$ cat main.yml galaxy_info:
author: Scarlett description: DevOps testing
company: your company (optional)

——skipped

We can list out the files now.

ansadmin@ThinkPad-X240:/etc/ansible/roles/apache$ tree
.
defaults
main.yml files
apache2.original index.html
handlers
main.yml meta
main.yml README.md
tasks
configure.yml install.yml main.yml service.yml
templates tests
inventory test.yml
vars
main.yml

8 directories, 13 files

Let’s apply this role into the ansible-playbook “apache_role_playbook.yml” as below to deploy it on the client nodes.

ansadmin@ThinkPad-X240:/etc/ansible/playbooks$ cat apache_role_playbook.yml
---
- hosts: all become: yes roles:
- apache

We have defined these changes should be run on all nodes, you can also use specific hostnames/IPs if needed. Specify the role name as “apache”, also if you have created multiple roles, you can use the below format to add it.
–apache
–mysql
–ftp

Lets verify for syntax errors:

ansadmin@ThinkPad-X240:/etc/ansible/playbooks$ ansible-playbook apache_role_playbook.yml --syntax-check playbook: apache_role_playbook.yml

No errors were found. Let’s move on to deploy the role.

ansadmin@ThinkPad-X240:/etc/ansible/playbooks$ ansible-playbook apache_role_playbook.yml

PLAY [all]
****************************************************************************************************************
********************

TASK [Gathering Facts]
****************************************************************************************************************
********
ok: [13.235.104.239]

TASK [apache : install apche2 package]
******************************************************************************************************** changed: [13.235.104.239]

TASK [copy apache configuration file]
********************************************************************************************************* changed: [13.235.104.239]

TASK [apache : copy index.html file]
********************************************************************************************************** changed: [13.235.104.239]

TASK [rename the default apache configuration file to disable it]
***************************************************************************** changed: [13.235.104.239]

TASK [apache : create symlink for new configuration file]
************************************************************************************* changed: [13.235.104.239]

TASK [start and enable apache2 service]
******************************************************************************************************* changed: [13.235.104.239]

PLAY RECAP
****************************************************************************************************************
********************
13.235.104.239 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

That’s it, we have successfully deployed the Apache webserver using Ansible Roles to the client node. Login into the client node and verify the following things.

ansadmin@ip-172-31-37-135:~$ apache2 -v Server version: Apache/2.4.41 (Ubuntu)
Server built:	2022-03-16T16:52:53ansadmin@ip-172-31-37-135:~$ systemctl status apache2 apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-03-25 06:30:42 UTC; 20s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 17962 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS) Main PID: 17966 (apache2)
Tasks: 55 (limit: 1147) Memory: 4.7M
CGroup: /system.slice/apache2.service 17966 /usr/sbin/apache2 -k start 17967 /usr/sbin/apache2 -k start 17968 /usr/sbin/apache2 -k start

You can now test by loading the server IP in the browser, you can see the test index.html page loading fine.

Conclusion
In this guide, we used Ansible to automate the process of installing and configuring Apache on Ubuntu 20.04.

  • Apache

Looking for AWS Experts?

We provide top-of-the-line custom AWS setup services tailored to your needs.

Creating Cloudwatch Alarm and pushing the Notifications to Email

Creating Cloudwatch Alarm and pushing the Notifications to Email
  • AWS
logo

How to create multiple S3 buckets using Cloudformation template

How to create multiple S3 buckets using Cloudformation template
  • AWS
logo

How to deploy container image from ECR to EC2 instances using AWS code-deploy agent

How to deploy container image from ECR to EC2 instances using AWS code-deploy agent
  • AWS
logo
How to install apache on ubuntu 20.04 using ansible roles.

Posts by TECH

TECH