Last Updated on July 20, 2025 by cscontents
Introduction
If you are a beginner in Ansible and wondering how Ansible automates a task from the very basics, then this post will surely help you. To get a high-level understanding of Ansible, please follow the article below.
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 a sample playbook (a playbook is nothing but a YAML file that contains your tasks).
Prerequisite
Before starting, please make sure you have two Linux boxes (or one Linux box and one Docker container). Consider one of the Linux boxes as ‘Ansible Controller Node’ and the 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 the “ansible –version” command. For other details, 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 tasks, and by task I mean doing any complex or simple thing, which could be just running a simple command. Example – running a command to check Java version.
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 the 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 contains details about the remote server.
Example Ansible playbook 2
Here we will run multiple basic Linux commands on a remote machine. To run multiple commands, we need to use a 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 contains details about the remote server.
In the above case, the command is run in the home directory of the user (the user that is used by 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 contains details about the remote server.
From the above 3 examples, 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 on a Linux machine. For this, collect the commands that you would run to install that software manually, and then write a playbook by looking at the above examples, which will do the job for you. No need for manual installation.
Not only that, if you have some Python script that you want to run on the remote server, then you can easily call that script from the playbook, and that script will be run on the remote machine. Same for PowerShell script, if we have a remote Windows machine, and we have a 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 that once you develop the playbook, you can reuse it in the future. For this, we can go with the Ansible role. Ansible’s roles are made to perform a specific task, and they are easy to use; it will simplify your playbook. Creating an Ansible role, we can easily share our playbook with the community, or we can use a role developed by others.
Thank You.
If you are interested in learning DevOps, please have a look at the articles below, which will help you greatly.
- Kubernetes Series: Part 1 – Introduction to Kubernetes | Background of Kubernetes
- Kubernetes Series: Part 2 – Components of Kubernetes cluster | Kubernetes cluster in detail
- Kubernetes Series: Part 3 – What is Minikube and How to create a Kubernetes cluster (on Linux) using Minikube?
- Introduction to Ansible | High Level Understanding of Ansible
- 10 frequently used ansible modules with example
- Jenkins Pipeline as code – High level information
- What is End-to-End Monitoring of any web application and Why do we need it?
- What is “Monitoring” in DevOps? Why do we need to Monitor App/DB servers, Transactions etc.?
- DevOps Engineer or Software Developer Engineer which is better for you?- Let’s discuss
- How To Be A Good DevOps Engineer?
- Network Connectivity Troubleshooting Guide for Beginners
- How to do git push, git pull, git add, git commit etc. with Bitbucket