Ansible - The DevOps Tool.

Ansible - The DevOps Tool.

What is DevOps and Ansible and How to write your first a Playbook.

Welcome to my second blog on Ansible. Ansible is DevOps and Configuration management tool. so first we need to understand What DevOps is and what configuration management is.

DevOps - Simply DevOps is the combination of two words, one is Development and the other is IT operations. This allows a single team to handle the entire application lifecycle from development to testing, deployment, and operations, looks interesting! DevOps promotes collaboration between the development and operations teams to deploy code to production faster in an automated and repeatable way.

Why DevOps?

The Operation and development team worked in complete isolation.

Without the use of DevOps, members spend a lot of time designing, testing, and deploying instead of building the project.

Manual code deployment leads to human errors in production.

So let's automate the deployment.

Automate means? Automate means automatic. for Example

Kitchen tools - before modernization, cooking used to be a very hectic job because everything was required to do manually. chopping, peeling, blending and mixing, etc. everything was done by hand or we used different machines for different works. but in the present world, we automate everything. also, we used an automated washing machine. it's cool Right?

Automation reduces time consumption, especially during the testing and deployment phase. The productivity increases and releases are made quicker by automation this will lead to catching bugs quickly so that they can be fixed easily.

Configuration management - it ensures the application interacts with only those resources that are concerned with the environment in which it runs.

Simply, if I make a project using python and c#, and Kafka okay If I give this project to my friend who doesn't have installed python and c#, and Kafka on his device and also doesn't know how to install that stuff. it's pretty difficult for him to run my project. and I am not going to my every friend's home to teach him how to install it, So I will make an automated script that first takes my project clone from the git and stores it on his device. and set the environment and deploy. set environments mean installing python and c# and Kafka. this is called configuration management. for that deployment, we used one of the DevOps tools Ansible.

there are many DevOps tools like chef, puppet, and Gitlab. In this blog, we are going to talk about Ansible.

Ansible

Ansible is an open-source IT engine that automates the IT tools such as intra-service orchestration, application deployment, cloud provisioning, etc

Ansible is easy to deploy because it does not use any agents or custom security infrastructure on the client side and by pushing modules to the clients.

Why ansible?

if we compare ansible with other best DevOps tools such as chef and puppet.

ansible is easy to learn. and in terms of configuration language, Yaml (yet another markup language) but it is not a markup language like HTML is considered to be the easiest one as it is similar to English and human-readable while puppet DSL and ruby DSL languages create setbacks for management. once again ansible portrays its dominance over the others in terms of management as it supports YAML languages creating setbacks for management. once again ansible portrays its dominance over the others in terms of management as it supports YAML.

so the ansible is -

  1. Easy to learn

  2. Easy installation process

  3. Python and Yaml configuration language.

  4. configuration is push and pull.

The scalability of the configuration tool is one of the main factors enterprises consider before choosing the tool

and the scalability of ansible is very high compared to chef and puppet.

Ansible Structure

control node - A system on which ansible is installed. You can run ansible commands such as ansible inventory on a control node.

managed node - a remote system or host that ansible controls.

Inventory - a list of managed nodes that are logically organized you create an inventory on the control node to describe host deployments to ansible.

The installation process for ansible

install the Ansible software with:

sudo apt-add-repository ppa:ansible/ansible

sudo apt update

sudo apt install ansible

Your Ansible control node now has all of the software required to administer your hosts. Next, we will go over how to add your hosts to the control node’s inventory file so that it can control them.

How to write an inventory file?

create an inventory file. in inventory, then in that file write master IP and slave IP. master IP is the local IP where you run ansible and slave IPs are your remote servers where you deploy a code. In slave IP you need an ssh password so you need to mention your ssh password in an inventory file

example - Inventory file

localhost ansible_connection=local

[Master]

localhost

[Slave]

193.234.5.22 ansible_connection=ssh ansible_ssh_user=your_username ansible_ssh_pass=your_password ansible_sudo_pass=your_sudopassword

123.343.3.12 ansible_connection=ssh ansible_ssh_user=your_username ansible_ssh_pass=your_password ansible_sudo_pass=your_sudopassword

For setup sudo and ssh passwords -

First, create a secret file to decrypt the password then create vault.txt and write your password.

How to encrypt and decrypt files using ansible-vault

Why do we need to encrypt files?

A common use case would be: you need to share secret files with your team without the risk of making them public

Install ansible-vault:

$ pip3 install ansible-vault

Create a file with your password

$ opens rand -hex 512 -out secret.pass

Now, we are going to encrypt our crt and key files

Encrypt files

$ ansible-vault encrypt --vault-password-file secret.pass file.crt

$ ansible-vault encrypt --vault-password-file secret.pass file.key

Here, you would see something like:

$ANSIBLE_VAULT;1.1;AES25
36653638646663366461326137303166643361303233306662376262633335616264393836613461
6634333832366436303262653036623439326162363036300a373166643834626436353537336630
39373039636439646637616sd2616666356565393133353233363863323036363032613134616563
6665303637366564330a643898643764666565363734633436353037653935306137383564656239
65643665636666623261653633643136633632346534666664623563323461616363663036346633
65643636386466373430613871323132353131663134313430663535306639343366343066356131
62623063383833633865613936383934323264363931653836663634643233393238626533356136

Decrypt files

$ ansible-vault decrypt --vault-password-file secret.pass file.crt

$ ansible-vault decrypt --vault-password-file secret.pass file.key

Create a Templates folder. In the template folder, we create service, sites, and conf folders and add services and sites files for updating files on the remote server.

after taking the git clone. we get repos folder. which contains the gateway, server, protocol folder, etc.

create ansible.cfg file

in that file write

[defaults]

INVENTORY=inventory

stdout_callback = debug
[ssh_connection]pipelining = True

check connection

ansible all -m ping -u root

you get this output after a successful connection.

Output

server1|success => {"changed": false, "ping": "pong"}

Write your first playbook -

Then create a Playbook folder -

The playbook is a collection of tasks

In this folder, we create a simple tasks file that installs python and some python modules like NumPy and pandas - simple.yml

Indentation is very important in an Ansible YAML file

In this simple.yml we write

---

- name: install packages on a remote server

hosts: deploy

become: true
vars_files: - secret

tasks: -

- name: install Python

apt:

- Python

# we install python packages like NumPy and pandas and jinja2

- name: Install Python

pip:

name:

- Numpy

- pandas

- jinja2

looks easy? cooool!

then run this playbook -

ansible-playbook /home/user_name/folder_name/playbook/simple.yml --vault-password-file=playbook/vault.txt

*add your user_name of pc and folder_name

check your remote server or IPs. Are python and the module successfully installed?

congratulations! You write your first ansible playbook and successfully automate the environment.

In Ansible blog - 2

I will show you how to install Kafka redis and how to take a git clone and how to deploy a code.

Abhishek Chavan Thank you!

#WeMakeDevs #opensource