ANSIBLE — PLAYBOOK

Michael Mensah
4 min readMay 6, 2023

--

What is Ansible playbook — An Ansible playbook is a file that contains code written in YAML format for executing tasks using Ansible.

To get started on this project.

First — Launch your servers

Second — Login to the Ansible control Engine and download ansible.

#Switch to root user for the download
sudo su -

#I want to turn on access to the Extra Packages for Enterprise Linux (EPEL) repository in an Amazon.
sudo amazon-linux-extras install epel -y

#Download ansible on Amazon Linux 2 AMI
Amazon-Linux-extras install ansible2 -y

#Check the anisble version
ansible --verion

Third —The IP addresses for all six servers (both Test and Prod) have been included in the Ansible host inventory file.

#To add your servers to the hosts file
cd /etc/ansible/
vi hosts (Add your servers)

#To exit the vi editor
Press the ESC button shift and : on your keyboard and then select wq to exit

Four — Generate a key pair and distribute the public key to the relevant servers. Once completed, use SSH to access all servers from the Ansible control engine.

#Create a key
ssh-keygen -t rsa

#copy key
cat ~/.ssh/id_rsa.pub

#ssh into all the server to see if you they can accessed from the control engine
ssh IP Addres

Five — It is time to commence building our Ansible playbook on the servers.

#Go to the ansible control enginer and create a yaml file to run the play
vi Testserver.yaml

# Simple playbook for Test server (Httpd webserver)
---
- hosts: Test
tasks:
- name: install web server
yum:
name: httpd
state: latest
- name: starting the webserver
services:
name: httpd
state: started

#Run the playbook
ansible-playbook Testserver.yaml

I encountered an error while running the playbook. I reviewed the Testserver.yaml file to verify if there were any missing indentation issues.

The installation of Httpd using the Testserver.yaml playbook was successful.

ansible-playbook Testserver.yaml

The installation of Nginx using the Prodserver.yaml playbook was successful.

#Go to the ansible control enginer and create a yaml file to run the play
vi Prodserver.yaml

# Simple playbook for Test server (Nginx webserver)
---
- hosts: Prod
tasks:
- name: install web server
yum:
name: nginx
state: latest
- name: starting the webserver
services:
name: nginx
state: started


#Run the playbook
ansible-playbook Proserver.yaml

Terminate instance by the project to avoid any chargers

To conclude, this is a straightforward and informative method to execute an Ansible playbook on multiple servers. Thank you for your time and attention. It is much appreciated.

--

--