The Talent500 Blog
Ansible for Beginners 1

Ansible for Beginners

Ansible for Beginners

What is Ansible?

Ansible automates software provisioning, configuration management, and deployment. Ansible runs and configures Unix-like and Windows systems when offering infrastructure as code. Managing and customizing the system uses a declarative programming language.

Ansible’s ease of installation, client connection, absence of agent, and large skill set make it a favorite. Since it communicates with clients over SSH, no specialized agent is required on the client side. Instead, Ansible delivers modules to clients, which are then performed locally and given back to the Ansible server.

The fact that it is based on SSH enables it to instantly connect to clients by utilizing SSH-Keys, which speeds up the process. Inventory files contain client hostnames as well as IP addresses and SSH port numbers. It is possible for Ansible to use an inventory file that has already been constructed and populated.

Ansible is a platform for IT automation that has been built to be exceptionally user-friendly and to ease the process of deploying apps and systems. These goals were achieved by designing Ansible with these design goals in mind. When releasing new versions of your apps or making updates, you should make every effort to avoid generating any scripts or specialized code. Instead, automate the procedure in a language that is comparable to plain English by utilizing SSH. There is no requirement for any agents to be installed on remote machines when using this method. Anyone who works in the field of information technology automation or as a DevOps engineer should make it a priority to educate themselves on the principles of the Ansible automation tool.

In this blog, we will cover the basis of Ansible.

 

Why use Ansible?

  • A huge number of people use this free and open-source community project.
  • IT experts have used it for many years and know it works well.
  • It’s easy to start and use right away, and you don’t need to know how to code.
  • The workflow for deployment is simple, and there are no extra agents.
  • Includes some advanced features like modularity and reusability that help users as they get better.
  • There is a lot of official documentation, and a lot of online material made by the community to go along with it.

 

Ansible Installation

Use pip in your selected Python environment to install the Ansible package of your choice for the current user:

$ python3 -m pip install –user Ansible


Alternately, you can install a specific version of Ansible-core in this Python environment:

$ python3 -m pip install –user Ansible-core==2.12.3


For other ways to install Ansible, you can visit, https://docs.Ansible.com/Ansible/latest/installation_guide/intro_installation.html

Key phrases that are utilized throughout Ansible

Ansible server:

The computer on which Ansible has been installed and which will be used to execute all of the tasks and playbooks

Ansible Module

A module is, in its most fundamental form, a command or group of instructions that are very similar to one another and are designed to be run on the client side.

Task:
A part that consists of a single activity that has to be carried out is referred to as a task.

Role

Function: A method of arranging jobs and related files so that they may be referred to later in a playbook

Fact

The gather-facts procedure retrieves information about the client system’s global variables from the client system itself.

Inventory

A file that contains information about the Ansible client servers is called an inventory. The term “hosts file” will be used in subsequent examples.

Play

Play entails the acting out of a playbook.

Handler
A task that is only called if there is a notifier present in the system.

What is YAML? 

Ansible playbooks are written in YAML. Here we will look at the basics of yaml.

Configurations may be written using the Yaml file format. YAML is a language that was established so that people might use it. It is a tight superset of JSON, which is another language for serializing data. However, because of the fact that it is a rigorous superset, it is capable of doing all that JSON is capable of and even more. In JSON, brackets and braces are used, but in YAML, newlines and indentation have specific meanings. This is one of the most significant differences.

 

Writing YAML

The basic structure of a YAML file is a map. You might call this a dictionary, hash or object, depending on your programming language or mood. Very generally, it’s keys and values all the way down:

key: value

Ansible for Beginners 2

Writing a list in yaml

Name:

  • List 1
  • List 2
  • List 3

Ansible for Beginners 3

Writing a map/dict in YAML

Name:

  •  Key1:value1
  •  key2:value2

Ansible for Beginners 4

Ansible Inventory

Ansible is able to establish access to several servers in order to execute its job. In order to facilitate this connectivity, Ansible keeps an inventory file that contains all of the information regarding the servers to which it needs to connect. On the Ansible server, Ansible is installed, and when it communicates with hosts running Linux or Windows, it uses ssh for Linux and powershell for Windows.

When writing a playbook, an inventory file is written that defines the hostname, ssh key for the host.

Ansible for Beginners 5

Sample Inventory file:

Inventory files have .ini extension.

Ansible for Beginners 6

Inventory files can also be grouped. To group one, you need to declare a name/alias at the beginning of the list.

Ansible for Beginners 7

Inventory files can also be written as a child and parent. For example, one of your playbooks wants to perform some tasks on all the hosts, but sometimes, you might need to perform tasks on only web servers. 

Ansible for Beginners 8

Ansible Inventory Configuration

Ansible for Beginners 9

Ansible Playbooks

Automation tasks, or “playbooks,” are detailed plans for carrying out complicated IT operations with minimal to no intervention from a human operator. Ansible® is a popular tool for creating and managing playbooks. Playbooks written in Ansible are run on hosts that belong to a certain Ansible inventory.

Ansible for Beginners 10

Ansible for Beginners 11

A sample playbook

Ansible for Beginners 12

Ansible Modules

Ansible modules are discrete chunks of code that may be used to direct the behavior of system resources or carry out specific system tasks. Ansible comes with a module library that you may use to run commands on remote hosts directly or through playbooks. Additionally, you have the ability to develop your own modules.

Plugins are another type of add-on for Ansible that are similar to modules in that they are bits of code that enhance the capabilities of the core software. Ansible’s design is based on plugins, which enables it to have a robust feature set that is both flexible and extendable. Ansible is included with a number of plugins, and it also makes it easy to use your own plugins.

Some of the most common modules are as follows.

Package Manager Modules yum & apt

The apt module, which is included in Ansible-core, handles Debian and Ubuntu package management. Here’s an instance of keeping the repository cache current and installing the most recent version of the Nginx package:

 

– name: Update the repository cache and update package “nginx” to latest version

  Ansible.builtin.apt:

    name: nginx

    state: latest

    update_cache: yes

The yum module, which is included in Ansible-core, is used to administer RHEL/Centos/Fedora Linux packages using the yum package manager. Continuing with the yum module from the last example, 

– name: Update the repository cache and update package “nginx” to latest version

   Ansible.builtin.yum:

     name: nginx

     state: latest

     update_cache: yes

Service Module

The service module manages remote host services and, depending on the configuration, uses whatever init system is currently active. A decent abstraction layer is provided by this module for the underlying service management components. Here’s how one may restart the docker service:

 

– name: Restart docker service

   Ansible.builtin.service:

     name: docker

     state: restarted

 

File Module

All actions involving files, symlinks, and directories are handled by the file module. This module may be used to generate permissions for a new directory, as seen here:

 

– name: Create the directory “/tmp/test” if it doesnt exist and set permissions

  Ansible.builtin.file:

    path: /tmp/test

    state: directory

    mode: ‘0750’

Copy Module

The copy component handles file transfers and local file relocations on a distant system, as well as copying files to that system. Here’s how to adjust permissions while transferring a file to the remote machine:

 

– name: Copy file with owner and permissions

  Ansible.builtin.copy:

    src: /example_directory/test

    dest: /target_directory/test

    owner: joe

    group: admins

    mode: ‘0755’

Cron Module

The cron module manages crontab entries and environment variables entries on remote hosts.

 

– name: Run daily DB backup script at 00:00

  Ansible.builtin.cron:

    name: “Run daily DB backup script at 00:00”

    minute: “0”

    hour: “0”

    job: “/usr/local/bin/db_backup_script.sh > /var/log/db_backup_script.sh.log 2>&1”

Ansible Variables

Variables are used by Ansible to control for platform-specific configurations. One command in Ansible may carry out the actions specified by a set of playbooks across a number of distinct computers. You may use variables written in regular YAML syntax, such as lists and dictionaries, to express the differences between the various systems. Playbooks, inventories, reusable files, and roles, and the command line are all great places to specify variables. When a playbook is executed, new variables can be created by assigning the result of a task to it.

In order to use a variable in a module’s argument, a conditional “when” statement, a template, or a loop, you must first create it, either by declaring it in a file, supplying it at the command line, or registering the return value or values of a job as a new variable. Many examples of setting and accessing variables in Ansible may be found in the Ansible-examples repository on github.

Using a single value to define a variable is the most basic application of variables in YAML. This pattern has numerous potential applications, but for the sake of clarity, we’ll use a playbook 

 

– name: Example Simple Variable

  hosts: all

  become: yes

  vars:

    username: bob

 

  tasks:

  – name: Add the user {{ username }}

    Ansible.builtin.user:

      name: “{{ username }}”

      state: present

 

Conclusion

In this article, we will cover the principles of Ansible, the reasons why it is so vital right now, as well as the potential benefits it may give to your company. In the following section, we will construct a sham application so that we may advance our work with Ansible even further. Install Ansible on each of your devices while you wait, and start familiarizing yourself with the documentation it provides. Sign up today to receive email notifications whenever a new post is published that is connected to Ansible.

1+
Shubham Singh

Shubham Singh

He is a SDE-2 DevOps Engineer, with demonstrated history in working with scaling startup. A problem solver by mettle, who loves watching anime when he is not working. He is a learner and educator by heart.

Add comment