Khemnath chauhan
8 min readJul 25, 2020

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 python installed.
$ python3 -V
# Use the below command to install Ansible
$ pip install ansible
# Check if the Ansible is installed
$ ansible --version
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.cfg: 
Config file defines all custom options
/etc/ansible/ansible.cfg
Default hosts file:
/etc/ansible/hosts

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.

# Create a directory - ansible under /etc
$ mkdir /etc/ansible
# Create file ansible.cfg
$ touch ansible.cfg
So, the config file is ready under below path.
/etc/ansible/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:

- Ansible : This is cli to run ansible command.
- all: To all the inventory (meaning all the IPs/Hosts).
- -m : This is parameter indicates the module used
- package: This is module use to Installs, upgrade and removes packages using the underlying OS package manager.
- -a : This parameters is to add module arguments
- name: This is required parameter used with package module. It signifies the Package name, or package specifier with version.
- state:
This is required parameter used with package module. Whether to install (present), or remove (absent) a package.

— 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.

[DB]
192.1.1.2
[WebServer]
192.1.1.1
[AppServer]
192.1.1.3
192.1.1.4

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

[root@localhost ~]# ansible all --list-hosts
hosts (2)
192.1.1.1
192.1.1.2
192.1.1.3
192.1.1.4
[root@localhost ~]# ansible db --list-hosts hosts (2)
192.1.1.2

— 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.

- hosts: webservers
vars:
http_port: 80
# Example playbook that uses the var.- hosts: all
vars:
- svc_name: httpd
- my_name: "Khemnath"
tasks:
- package:
name: "{{ svc_name }}"
state: present
- copy:
#src: "index.html" -- Line is commented
content: "This is another example created by {{ my_name }} "
dest: "/var/www/html/test1.html"
- service:
name: "{{ svc_name }}"
state: started

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.

# passing the variable from command line. Now, in the playbook wherever the svc_name is defined, it will be replaced with nginx.$ ansible-playbook var_usage.yml -e svc_name=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.

# Sample code to show the debug Usage.- hosts: all
vars:
- my_name: Khemnath
- svc_name: "httpd"
tasks:
- package:
name: "{{ svc_name }}"
state: present
- service:
name: "{{ svc_name }}"
status: started
- debug:
msg: "Hello Workd!! {{ my_name }} "

— Command Module:

Ansible allows to run command using the command module.

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

$ ansible all -m command -a date
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

- hosts: all //Or this can be specific hosts group.
tasks:
- package: "name=httpd state=present"
- copy: "src=index.html dest=/var/www/html/"
- service: "name=httpd state=started"
OR, The same code can also be written in below format to make it more readable.
- hosts: all //here you can add specific hosts group, like web,db.
tasks:
- package:
name: httpd
state: started
- copy:
src: index.html
dest: /var/www/html/
- service:
name: httpd
state: started
## Find the below output after running this simple playbook.
## Run the playbook
$ ansible-playbook first.yml
Playbook output.

Khemnath chauhan
Khemnath chauhan

No responses yet