~:Ansible Tutorial:~

Santosh ChauhanSantosh Chauhan
4 min read

Ansible Workflow:

Ansible Workflow

In the above image, the Management Node is the controlling node that controls the entire execution of the playbook. The inventory file provides the list of hosts where the Ansible modules need to be run. The Management Node makes an SSH connection and executes the small modules on the host's machine and install the software.

Terms used in Ansible:

TermsExplanation
Ansible ServerIt is a machine where Ansible is installed and from which all tasks and playbooks will be executed.
ModulesThe module is a command or set of similar commands which is executed on the client-side.
TaskA task is a section which consists of a single procedure to be completed.
RoleIt is a way of organizing tasks and related files to be later called in a playbook.
FactThe information fetched from the client system from the global variables with the gather facts operation.
InventoryA file containing the data regarding the Ansible client-server.
PlayIt is the execution of the playbook.
HandlerThe task is called only if a notifier is present.
NotifierThe section attributed to a task which calls a handler if the output is changed.
TagIt is a name set to a task that can be used later on to issue just that specific task or group of jobs.

Create Ansible Master Node:

Select ubuntu machine.

Security parameter: SSH+HTTP+HTTPS & launch your instance.

Installation of Ansible on Ubuntu:

google "digitalocean" for installing ansible on ubuntu.

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-ubuntu-20-04

sudo apt-add-repository ppa:ansible/ansible

sudo apt update

sudo apt install ansible

Creation of Ansible worker node:

Create 3 ansible worker nodes where automation will be applied through Ansible Master: Ubuntu machine & having the same key as master node.

Inventory file creation:

Mention the servers here where automation needs to be reflected:

sudo nano /etc/ansible/hosts

Ping all the worker nodes from master:

ansible servers -m ping

Copy .pem key from Local PC to Ansible Master:

PS C:\Users\Haha CORPORATION\Downloads> scp -i "ansible-all-access-key.pem" ansible-all-access-key.pem ubuntu@ec2-44-198-183-103.compute-1.amazonaws.com:/home/ubuntu/.ssh

Check this copied key @ Ansible Master

If we want to apply same variable on all the ansible worker node: we need to define "vars"

[server:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansible-all-access-key.pem

Permission granted to this key:

chmod 600 ansible-all-access-key.pem

Now ping all the Ansible worker node from Ansible Master

ansible servers -m ping

All the nodes are pinging from the master node.

Automation process:

Check the disk details of all the ansible worker node:

ansible servers -a "df -h"

-a : used for writing a command

-m: is used for predefined module.

Check the uptime of ansible worker nodes:

ansible servers -a "uptime"

Inventory file creation:

Create Inventory "prod-inv":

[servers]
prod_1 ansible_host=54.157.162.2

[servers:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansiblall-access-key.pem

Ping this inventory:

ansible -i prod_inv servers -m ping

Create Inventory "dev-inv" & ping :

[servers]
dev_1 ansible_host=54.160.94.190

[servers:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansiblall-access-key.pem

Playbook Creation:

Create install_nginx.yml paybook file

-
  name: this playbook install nginx
  hosts: servers
  become: yes
  tasks:
    - name: install nginx
      apt:
        name: nginx
        state: latest
    - name: start nginx
      service:
        name: nginx
        state: started
        enabled: yes

Run the playbook:

ansible-playbook install_nginx.yml

Verify whether ansible is installed on the worker node:

ssh any ansible worker node.

When condition in playbook

create install_condition.yml

-
  name: this will install based on os
  hosts: servers
  become: yes
  tasks:
    - name: install Docker
      apt:
        name: docker
        state: latest
      when: ansible_distribution == "CentOS" or ansible_distribution == "Red Hat Enterprise Linux"

Run "install_condition.yml"

ansible-playbook install_condition.yml

#Installation of aws CLI impacted on all the Ubuntu servers.

Check docker installed or not on asible worker1:

ssh & run command docker --version

Thank you for reading!Happy Learning!!

Santosh Chauhan

0
Subscribe to my newsletter

Read articles from Santosh Chauhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Santosh Chauhan
Santosh Chauhan

Hello, I'm Santosh Chauhan, a DevOps enthusiast, who enjoys automation, continuous integration, and deployment. With extensive Hands-on experience in DevOps and Cloud Computing, I am proficient in various tools and technologies related to infrastructure automation, containerization, cloud platforms, monitoring and logging, and CI/CD. My ultimate objective is to assist organizations in achieving quicker, more effective software delivery while maintaining high levels of quality and dependability.