--- title: Ansible in linux --- # 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. ![A1](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. ![A2](images/A2.png) **PULL** : Agents from client side pull changes from the management node. Ex – Cheff Puppet. .. image:: images/A3.png ### HOW TO INSTALL ANSIBLE 1. Please type in the commands one by one to install ansible successfully. ```bash 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 ``` 2. 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. :::info 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. ![A4](images/A4.png) ### Ansible Modules * Modules: Reusable standalone code, Module Index — `Ansible Documentation `_ :::info 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: ```bash 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: ```bash ansible webservers -m command –a “ls” ``` * If you want to flush iptable rules on all the hosts in the inventory, then type: ```bash ansible –i inventory all –m command –a “iptables -F” –become –ask-become-pass ``` * To gather facts about the hosts, then type: ```bash ansible all –m setup ``` * If we want to extract particular facts in the documentation of the setup module, then type: ```bash ansible-doc setup ``` ## SAMPLE ANSIBLE PLAYBOOK ![A5](images/A5.png) ### Hands-on Implementation * Ansible pushed the modules to the node for the execution. ![A6](images/A6.png) * As per the verification we can see that nginx has been installed by ansible and is up and running. ![A7](images/A7.png) * SSH Passwordless Authentication: ```bash 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: ```bash ansible-playbook -i hosts task_install.yml ``` ![A8](images/A8.png) ---