MY ANSIBLE LEARNING JOURNEY:-

Ansible.

Automation always fascinates me. Automation is good way to make life easy and improve the efficiency of our day to day work.

What is Ansible?

Ansible is a open-source software or tool that is used for configuration management, software provisioning and application deployment.

The primary advantage of ansible tool is you don’t need to understand all OS commands. We just declare what we want and ansible takes care of how it needs to be done. That is the beauty of this tool. It automatically reads the underlying OS and use the best command to complete the task defined.

Ansible is the not the one that does the actual task in manage node. It just knows which command to call in Manage node.

Key Attributes of Ansible.

  • Declarative language.

We need to just declare what we want. It’s doesn’t require to tell how to do.

  • Agentless.

Unlike other configuration management tools (chef,puppet), Ansible doesn’t require to install agents on nodes systems. Ansible is only install on controller node and everything is control from controller node.

  • Uses SSH Protocol to connect.

The controller system uses ssh protocol to connect to all other nodes for any task/management.

  • Idempotency:

An operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly without any intervening actions.

The modules will not install software if already installed or will not start service if already started.

INSTALLATION STEPS:-

Ansible is written using python, so python needs to be installed in order to run the Ansible. I will be using RedHat OS to do all practical. So, the commands i will be using are based on RHEL8.

  • Check if the Ansible is available in your system. Use Linux package manager rpm.
Checking if software Installed
  • Use yum to install the software. However here we will use pip (python installer) which is similar like yum in OS world. Using pip is appropriate way to install ansible.
Check Ansible Version

ANSIBLE Architecture:-

Ansible Architecture.

Ansible have simple architecture. The ansible tool is install on controller node and all other nodes are manage from this host. The controller node uses SSH protocol to connect to managed hosts.

Detail Architecture.

Important Terminology used in Ansible:-
Control node: This system has ansible installed.
Managed Nodes: The servers that will be managed with Ansible
Inventory: The file defines the hosts and groups of hosts, commands, modules, and tasks in a playbook
Modules: The code that Ansible executes (python files)
Tasks: Specific actions to perform within a Playbook
Variables: Something different about a specific host
Playbooks: it’s Collection tasks written in YAML.

ANSIBLE COMMANDS:-

Check the ansible version. I have installed the stable version of ansible 2.9

When install with pip the config file is not created as shown below it’s none.

The ansible config file needs to be created on this below path. So, create a new directory — /etc/ansible and create file ansible.cfg.

Now, if we check the ansible version the config file path will be shown.

ansible config file.

Currently, there are no host in inventory file. Basically, the inventory file have all IP of the manage nodes. The inventory file needs to be created and path of inventory file needs to be updated in /etc/ansible/ansible.cfg file.

The below command is to check all the hosts in inventory, however the cfg file is not updated yet and hence there is no output for this command.

Now, after adding the inventory path in ansible.cfg file below is the output.

Ansible always look for this inventory to get the hosts details.

Initially ssh connection may failed, this is because the remote connection will be rejected. In order to solve this issue we need to install sshpass software. The parameter of ssh user & password will be passed from inventory file.

Install sshpass software

Running Simple ping command to test:

Simple Ansible command

Another example to install Firefox software on manage node.

Breaking down the above commands:

— Host Group:

The manage nodes, IP can be group base on their underlying system type. Like all DB nodes can be group together, all web servers can be group together and all App Servers can be group together. With this host group managing node become easier and any task can be created to take effect on specific host group.

Now instead of using all, we can use specific host group to perform task. In the below command, web & test are host group name.

— Variable usage in Ansible.

variable can be defined in ansible playbook. Before we start using variables, it’s important to know what are valid variable names.

Variable names should be letters, numbers, and underscores. Variables should always start with a letter.

my_name is a great variable. foo5 is fine too.

foo-port, foo port, foo.port and 12 are not valid variable names.

Defining a variable in playbook:-

We can define variables directly in a playbook. Below is sample code with variable usage.

Ansible also supports to pass the variable during the execution in command line. Suppose in above code in the place of httpd we want to use nginx.

— Register:

When we run a playbook the output shown for task’s module is not detail. If we run the command line the module execution message is shown in detail. However, similar detail message is not shown when running through playbook.

So, we have a concept call register; we can store the output of module and display through debug module.

E.g; when running command line:

E.g; when the same code is run through playbook.

E.g; Now lets use register to capture the output for the module and show after execution.

Register usage:

— Debug:

Debug module is used to print message during execution in ansible.

To learn more about this module click here.

— Command Module:

Ansible allows to run command using the command module.

Suppose i want to execute date command on all manage servers.

command module example.

— Ignore_error:

Ansible by default will skip subsequent task if encounter any error in any of the module. There is a way to skip such error/Ignore and proceed with subsequent task/module.

— PLAYBOOK:

Collection of tasks, saved so you can run those tasks in that order repeatedly. Playbooks can include variables as well as tasks. Playbooks are written in YAML and are easy to read, write, share and understand.

Sample Playbook

It’s always good practice to create a separate directory to keep all codes/playbooks. This is also called as workspace.

Creating workspace.

Simple Playbook:

First let me create a playbook name first.yml

Playbook output.

Stay hungry; Stay Foolish!!