Ansible Installation in Linux and Node Deployment
Topics to be covered
-
What is Ansible.
-
Configuration management.
-
Push Based vs Pull Based Architecture.
-
How to install Ansible.
-
Host Inventory file.
-
Ansible Roles, Modules.
-
Ansible Playbooks.
-
Hands-on Implementation.
Introduction
-
Ansible - It is an opensource IT engine which automates application deployment, intra service orchestration, cloud provisioning and many other IT tools.
-
Playbooks – Lists the automation jobs written in YAML(Yet Another Markup Language).
-
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
-
Establishes and maintains consistency of a product performance.
-
Maintains physical attributes with its requirements and design.
-
Preserves operational information throughout its life.
Features
-
DEPLOYMENT AGENTLESS
No agents have to be deployed on the nodes unlike puppet or cheff.
-
SSH
Establishes connection to nodes using SSH.
-
PYTHON
Written in python.
-
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.
PULL : Agents from client side pull changes from the management node. Ex – Cheff Puppet.
.. image:: images/A3.png
HOW TO INSTALL ANSIBLE
- 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
- 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.
“ansbile-galaxy init” command is used to create the template for creating ansible roles.
HOST INVENTORY ^^^^^^^^^^^^^^
- Contains the host to which ansible pushed the configurations to.
Ansible Modules
- Modules: Reusable standalone code, Module Index —
Ansible Documentation <https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html>
_
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
Handsom Implementation
- Ansible pushed the modules to the node for the execution.
- As per the verification we can see that nginx has been installed by ansible and is up and running.
- 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