Ansible Installation in Linux and Node Deployment

Topics to be covered

  1. What is Ansible.

  2. Configuration management.

  3. Push Based vs Pull Based Architecture.

  4. How to install Ansible.

  5. Host Inventory file.

  6. Ansible Roles, Modules.

  7. Ansible Playbooks.

  8. Hands-on Implementation.

INTRODUCTION

  1. Ansible - It is an opensource IT engine which automates application deployment, intra service orchestration, cloud provisioning and many other IT tools.

  2. Playbooks – Lists the automation jobs written in YAML(Yet Another Markup Language).

  3. Ansible connects to the host vis ssh connection and sends programs called modules. Ansible runs the modules on the nodes and removes them when finished.

CONFIGURATION MANAGEMENT

  1. Establishes and maintains consistency of a product performance.

  2. Maintains physical attributes with its requirements and design.

  3. Preserves operational information throughout its life.

../_images/A1.png

Features

  1. DEPLOYMENT AGENTLESS

    No agents have to be deployed on the nodes unlike puppet or cheff.

  2. SSH

    Establishes connection to nodes using SSH.

  3. PYTHON

    Written in python.

  4. PUSH

    Push based architecture.

PUSH BASED vs PULL BASED ARCHITECTURE

PUSH : Configuration changes from management node is pushed to the client node. No agents have to be deployed to the client side. Ex – Ansible, CFEngine.

../_images/A2.png

PULL : Agents from client side pull changes from the management node. Ex – Cheff Puppet.

../_images/A3.png

HOW TO INSTALL ANSIBLE

  1. Please type in the commands one by one to install ansible successfully.

sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
  1. Ansible Roles: Ansible roles consists of many playbooks, Roles are way to group multiple tasks together in one placeholder to do automation in an effective manner with clean directory structures.

Roles can be easily reused by anyone, if the role is suitable to anyone. It can easily modified and reduce syntax errors.

Note

“ansbile-galaxy init” command is used to create the template for creating ansible roles.

HOST INVENTORY

  1. Contains the host to which ansible pushed the configurations to.

../_images/A4.png

ANSIBLE MODULES

Note

A module is a reusable, standalone script that Ansible runs on your behalf, either locally or remotely. Modules interact with your local machine, an API, or a remote system to perform specific tasks.

  • To use ansible modules from command line, type:

ansible all –m ping       ping = ssh
  • If we want to use the ping module to ping all the hosts efined in the inventory, then type:

ansible webservers -m command –a “ls”
  • If you want to flush iptable rules on all the hosts in the inventory, then type:

ansible –i inventory all –m command –a “iptables -F” –become –ask-become-pass
  • To gather facts about the hosts, then type:

ansible all –m setup
  • If we want to extract particular facts in the documentation of the setup module, then type:

ansible-doc setup

SAMPLE ANSIBLE PLAYBOOK

../_images/A5.png

HANDSON IMPLEMENTATION

  • Ansible pushed the modules to the node for the execution.

../_images/A6.png
  • As per the verification we can see that nginx has been installed by ansible and is up and running.

../_images/A7.png
  • SSH Passwordless Authentication:

cat ~/.ssh/id_rsa.pub | ssh user@xxx.xxx.xxx.xxx "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
  • Execute the below command:

ansible-playbook -i hosts task_install.yml
../_images/A8.png