Ansible architecture and setup :
Tags: devops, automation, ansible, sysadmin
Published: 2020-11-22 16:56:21

# __Agenda :__
* __What is Ansible :__
* __Configuration Management :__
* __Pull based vs Push based :__
* __How to install Ansible :__
* __Host Inventory :__
* __Ansible Modules :__
* __Understanding YAML :__
* __Ansible Playbook :__
* __Hands-on :__

## __What is Ansible :__
Ansible is an IT automation tool which can deploy software , configure systems and orchestrate more advance tasks . 
Ansible is completely agentless means it connects through ssh and after connection ansible pushes codes called as modules and it runs on your nodes once finished it automatically got removes .

## __Configuration Management :__
* Establishes and maintains consistency of a product's performance .
* maintains physical attribute with it's requirement and design .
* Prevents operational information through it's life .
* such info includes exact versions and updates and the network address for all the hardware devices .
so if you want to install webserver in all the nodes it is not feasible for you to go to each server and install it manually .

## __How Ansible Works :__
![Ansible architecture](https://dev-to-uploads.s3.amazonaws.com/i/33ympesmiwld52do90ji.png)

By connecting the node and pushing programs called modules .
Management node (running the installation) does a ssh .
Then it look for the host inventory and then remove the code which is already executed .

## __Features of Ansible :__
* Ansible is a agent less .
* Ansible usages ssh for secure connection .
* written in python 
* push based configuration management sysytem (It provides full control)

## __Push based vs pull based :__
What this agent does is that it keeps on pulling the central server periodically for any kind of confirmation information and it's pulls those changes and gets them affected on their node machines .

So in case of ansible you can make changes directly and push the configs as you got the full control .

## __Disadvantages with the push based architecture :__
You can not achieve full automation with this because it is not usually possible to boot a server and have it configured without some client server protocol and also it gives you some lag when you pass it's limit's(like configuring 1000 servers) unless you use threading and multiprocessing .

## __Install Ansible :__
* Control machine and remote machine
```$sudo apt update```
```$sudo apt install software-properties-common```
```$sudo apt-add-repository ppa:ansible/ansible```
```$sudo apt update```
```$sudo apt install ansible```

## __Host Inventory :__
Inventory defines a group of hosts . we can group our web servers in one group and application servers in another . group can have multiple or single servers .

* Location where we store the info about the host servers 
```$cd /etc/ansible/hosts ```
* Demo
```
[webservers]
server1 ansible_port=4242
ansible_user = admin

[application]
server1
server2

[master]

localhost ansible_connection = local
```

## __Ansible Modules:__
Modules are the executable plugins that get the real job done .
Modules can take the key value pair .

* Ping command to check the status  
```$ansible ping -m ping```

* List out all the files in the webserver host
```$ansible webserver -m command -a "ls"```

* Flush all the iptables rules
```$ansible -i inventory all -m command -a "iptables -F" --become --ask-become-pass```

* Gather facts about the host
```$ansible all -m setup```

* extract the certain facts in the documentation 
```$ansible-doc setup```  

## __Ansible Playbook :__
Coming soon

## __Hands on :__

1. we have to generate a key value pair .
```$ssh-keygen ```

* create a host inventory file in this directory .
```$sudo nano /etc/ansible/hosts ```

```
[test-servers]
knode
```

* Mention the ssh generated key in the knode .
```$ssh-copy-id -i knode ```

* If it is a __remote server__ then use __scp__ to copy the key .
```scp -i 'location of your pem file' 'file that you want to copy' ubuntu@'ip':/home/ubuntu ```

```$ansible -m ping test-servers ```

* Crating a playbook
```$sudo nano demo.yaml ```

```
- hosts : test-servers
  become : true
  vars :
    ansible_become_pass : (your password)
  tasks: 
  - name: install nginx
    package: pkg=nginx state=installed

    notify:
    - start nginx

  handlers :
  - name: start nginx
    service: name= nginx state=started
```

* Run The playbook
```$ansible-playbook demo.yml```

* switch to other node for checking if nginx is installed or not 
```$ps aux | grep nginx```