RESTARTING OF APACHE WEBSERVER USING ANSIBLE

What Is Ansible?

Why Do We Need Ansible?

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.

  • 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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store