Ansible — 101

Arunvel Arunachalam
5 min readApr 2, 2021

Ansible is an agent-less IT automation tool.

Q) What is so great about Ansible?

A)There are several configuration management tools out there [Chef/Puppet].

Ansible’s USP

  • Easy-to-Read Syntax [Plain English]
  • Zero Installation on Remote Hosts [No agent/software to be installed on Remote Hosts]
  • Push Based [Control is in your hands/Your destiny in your own hands] — Ansible connects to remote servers & executes code, which may or may not change remote server state.
  • Built-in Modules [We as System Admins use modules to perform tasks such as installing softwares, copying files, so on & so forth (Sky's the limit)]

Installing Ansible

I am using Ubuntu. To install Ansible on Ubuntu use the following command

  • sudo apt-get install ansible

For other OS do visit https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

  1. Ansible version

Ideal scenario

Control Machine is any machine with Ansible installed. We can use any computer that has a Python installation.

Target Servers are servers where,tasks are executed.

In my case, target server is my local machine, where I have installed Ansible(Reason being I have single Machine). Hence my Ansible control machine & target server are both same.

The following steps are for passwordless ssh between control machine & target server

The Ansible inventory file defines the remote hosts.

The host file is used to store connections for Ansible playbooks. In our case we have a group called server & host <localhost>

Ansible ad-hoc command uses the /usr/bin/ansible CLI tool to automate a single task. Adhoc-commands are quick & easy, but not reusable.

We are using ansible CLI tool, our target remote hosts is mentioned in server group.

ansible server -m ping

  • m = module
  • ping = Tries to connect to host, verify a usable python & return pong on success

Q) What are Ansible modules?

A) Ansible modules are reusable, standalone scripts that can be used by the Ansible API or by the ansible or ansible-playbook programs.

ansible server -m command -a uptime

  • m = module
  • command = Executes commands on target servers
  • a = pass an argument to the module (the argument is the command to run).

In our case, the command is uptime

The command module is used commonly,hence its the default module.

Enough of ad hoc commands, these are not at all used in production.

In production we use Ansible Playbook[Playbooks are used with another great topic called Ansible Roles].

Q) What is Ansible Playbook?

A) An Ansible playbook is a blueprint of automation tasks, which are complex IT actions executed with limited or no human involvement. With Ansible playbook we can execute lot of tasks.

Let’s see a simple playbook

hosts: server [ In hosts file, we have created a group called server ]

sudo: yes [ Used to run task as privileged user ]

tasks [ executes a module in remote server ]

name [ name of task ]

apt: [ apt ansible module is used to manage packages with apt package manager ]

update_cache [ update the apt cache ]

state=present [ It will ensure desired package is installed ]

http://localhost

Below we are using command module. Executing date command on remote server & saving output in variable date_var

debug module prints statements during execution

Below we are using gather_facts module.

This module is automatically called by playbooks to gather useful variables about remote hosts that can be used in playbook.

Below ansible has gathered remote host distro

Below we are using lineinfile module.

This module ensures a particular line is in a file. In our case, it will add contents in file.

Note: File should already be present in remote host.

We are also using with_items.

Ansible with_items is a lookup type plugin which is used to return list items passed into it.

In our case list items are Hello, Hi & Bye

Folks that’s it for Ansible -101.

Please visit https://docs.ansible.com/ for more detailed information.

--

--