RESTARTING OF APACHE WEBSERVER USING ANSIBLE

Arnab Saha
4 min readMar 21, 2021

What Is Ansible?

Ansible is an open source IT Configuration Management, Deployment & Orchestration tool. It aims to provide large productivity gains to a wide variety of automation challenges. This tool is very simple to use yet powerful enough to automate complex multi-tier IT application environments.

Why Do We Need Ansible?

Let us take a little flashback to the beginning of networked computing when deploying and managing servers reliably and efficiently has been a challenge. Previously, system administrators managed servers by hand, installing software, changing configurations, and administering services on individual servers.
As data centers grew, and hosted applications became more complex, administrators realized they couldn’t scale their manual systems management as fast as the applications they were enabling. It also hampered the velocity of the work of the developers since the development team was agile and releasing software frequently, but IT operations were spending more time configuring the systems. That’s why server provisioning and configuration management tools came to flourish.

Consider the tedious routine of administering a server fleet. We always need to keep updating, pushing changes, copying files on them etc. These tasks make things very complicated and time consuming.

But let me tell you that there is a solution to the above stated problem. The solution is — Ansible.

Ansible Terms:

  • Controller Machine: The machine where Ansible is installed, responsible for running the provisioning on the servers you are managing.
  • Inventory: An initialization file that contains information about the servers you are managing.
  • Playbook: The entry point for Ansible provisioning, where the automation is defined through tasks using YAML format.
  • Task: A block that defines a single procedure to be executed, e.g. Install a package.
  • Module: A module typically abstracts a system task, like dealing with packages or creating and changing files. Ansible has a multitude of built-in modules, but you can also create custom ones.
  • Role: A pre-defined way for organizing playbooks and other files in order to facilitate sharing and reusing portions of a provisioning.
  • Play: A provisioning executed from start to finish is called a play. In simple words, execution of a playbook is called a play.
  • Facts: Global variables containing information about the system, like network interfaces or operating system.
  • Handlers: Used to trigger service status changes, like restarting or stopping a service

Task Description📝

Restarting HTTPD Service is not idempotence in nature and also consume more resources.. Suggest a way to rectify this challenge in Ansible Playbook.

👉The following Ansible Playbook accomplishes the following tasks:

  • Mounts CDROM on a directory in the Target Node
  • Updates YUM repo list
  • Installs HTTPD server
  • Configures the service (including the changing of default Document Root)
  • Copies webpage from Controller Node to the Target Node
  • Exposes the port on which HTTPD server is running
  • Starts the service

So Let’s write a playbook now for doing all this jobs

- hosts: all

tasks:

- name: “Creating CDROM’s mount point.”

file:

path: “/dvd”

state: directory

- name: “Mounting CDROM”

mount:

src: “/dev/cdrom”

path: “dvd”

state: mounted

fstype: “iso9660”

- name: “Creating yum repo 1”

yum_repository:

baseurl: “/dvd/AppStream”

name: “DVDrepo1”

description: “CDROM Repo 1”

gpgcheck: no

- name: “Creating yum repo 2”

yum_repository:

baseurl: “/dvd/BaseOS”

name: “DVDrepo2”

description: “CDROM Repo 2”

gpgcheck: no

- name: “Installing HTTPD Server”

package:

name: “httpd”

state: present

- name: “Creating new directory for root to configure HTTPD”

file:

state: directory

path: “/var/www/myconf”

- name: “Copying new conf file for HTTPD”

copy:

src: “newconf.conf”

dest: “/etc/httpd/conf.d/newconf.conf”

- name: “Copying content to Document root”

copy:

dest: “/var/www/myconf/myweb.html”

content: “This is my webpage”

- name: “Exposing the port”

firewalld:

port: “8080/tcp”

state: enabled

permanent: yes

immediate: yes

- name: “Restarting HTTPD Service”

shell: “/usr/sbin/httpd”

register: startoutput

changed_when: “‘already running’ not in startoutput.stdout”

📌Let’s now run the playbook

ansible-playbook http_playbook.yml

So we can see the HTTPD service is restarting

Congrats. The work is done

If you find it interesting please upvote it.

Connect with me on LinkedIn | GitHub

--

--