DEV Community

Cover image for Ansible architecture and setup :
Rahul Kumar
Rahul Kumar

Posted on

Ansible architecture and setup :

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Run The playbook

$ansible-playbook demo.yml

  • switch to other node for checking if nginx is installed or not

$ps aux | grep nginx

Discussion (0)