basics of task automation using ansible

Basics of automation using Ansible | Automate any task

Last Updated on February 18, 2023 by cscontents

Introduction

If you are a beginner in Ansible and wondering how does Ansible automate a task from very basic, then this post will surely help you. To get a high level understanding of Ansible, please follow the below article.

Introduction to Ansible | High Level Understanding of Ansible

If you want to learn Ansible basics then please check out the below course from KodeKloud which offers one of the best learning material in DevOps world.

Link of Training Course: Ansible for absolute beginners

Note: The above links are affiliate links, if you enroll this course using the above link, then it would help us to get some monetary benefit from KodeKloud. It won’t cost you anything.

Here, we will discuss with sample playbook (a playbook is nothing but a YAML file which contains your tasks).

Prerequisite

Before starting, please make sure you have two Linux box (or one Linux box and one docker container). Consider one of the Linux box as ‘Ansible Controller Node’ and other one as ‘Ansible Controlled Node’ or remote server (target machine where you want to perform the task using Ansible). In the ‘Ansible Controller Node’ we need to install Ansible. And after installation, please confirm the same by running “ansible –version” command. For other detail, please follow this article.

Example Ansible playbook 1

The first question we might come across in our mind that how the ansible tool is able to automate task and here by task I mean doing any complex or simple thing which could be just running a command to Java version or anything else.

Here, we will learn how to run only one basic Linux command on the remote machine. Please create one inventory file and one playbook as shown below and run that playbook using the command mentioned below.

inventory.txt file

remote_server ansible_host=<IP of your remote machine> ansible_ssh_user=<ssh user of remote server> ansible_ssh_pass=<ssh password of your rempte machine>

In the above inventory.txt file, we are using ‘remote_server’ as an alias to refer to the remote server, we can use any name here and the same name should be used in the ‘hosts’ section of playbook.

Playbook

-
 name: Test Playbook
 hosts: remote_server
 tasks:
    - name: Test task
      command: docker --version

Command we need to run:

ansible-playbook playbook.yml -i inventory.txt -vv

Here, ‘playbook.yml’ is the name of the playbook we have given, you can give any name. And ‘-i’ is the flag used to pass the inventory file which contain details about the remote server.

Example Ansible playbook 2

Here we will run multiple basic Linux command on remote machine. To run multiple command, we need to use loop. For this, we need to use “with_items”

inventory.txt file

remote_server ansible_host=<IP of your target machine> ansible_ssh_user=<ssh user of remote server> ansible_ssh_pass=<ssh password of your target machine>

Playbook

-
 name: Test Playbook
 hosts: remote_server
 tasks:
    - name: test task
      command: "{{ item }}"
      with_items:
        - docker --version
        - pwd

Command we need to run:

ansible-playbook playbook.yml -i inventory.txt -vv

Here, ‘playbook.yml’ is the name of the playbook we have given, you can give any name. And ‘-i’ is the flag used to pass the inventory file which contain details about the remote server.

In the above case, the command is run in the home directory of the user (user which is used by the Ansible to log into the remote server). Which we can verify by the output of the above playbook (there is a command we are running which is “pwd”). In the next example, we will run commands on the remote server in a particular directory.

Example Ansible playbook 3

inventory.txt file

remote_server ansible_host=<IP of your target machine> ansible_ssh_user=<ssh user of remote server> ansible_ssh_pass=<ssh_password of your target machine>

Playbook

-
 name: Test Playbook
 hosts: remote_server
 tasks:
    - name: test task
      command: "{{ item }} chdir=/home/ubuntu/temp"
      with_items:
        - docker --version
        - pwd

OR we can use the below.

-
 name: Test Playbook
 hosts: remote_server
 tasks:
    - name: test task
      command: chdir=/home/ubuntu/temp {{ item }}
      with_items:
        - docker --version
        - pwd

Command we need to run:

ansible-playbook playbook.yml -i inventory.txt -vv

Here, ‘playbook.yml’ is the name of the playbook we have given, you can give any name. And ‘-i’ is the flag used to pass the inventory file which contain details about the remote server.

From the above 3 example, it is clear that we can perform most of the tasks on Linux using Ansible, which means we can automate them.

For example, you need to install any software in a Linux machine. For this, collect the commands which you would run to install that software manually and then write a playbook by looking the above examples which will do the job for you. No need for manual installation.

Not only that, if you have some python script which you want to run on the remote server, then you can easily call that script from the playbook and that script will be run in the remote machine. Same for PowerShell script, if we have a remote Windows machine, and we have PowerShell script to do a specific task, then we can call the script from the ansible playbook and that script will be run on the remote Windows machine. 

And another great thing is once you develop the playbook, you can reuse it in the future. For this, we can go with Ansible role. Ansible’s roles are made to perform a specific task, and they are easy to use, it will simplify your playbook. Creating Ansible role, we can easily share our playbook with the community, or we can use role developed by others.

 

Thank You.

 

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.