How To Write First Ansible Playbook as a Beginner: Writing an Ansible Playbook is very easy, and you’ll see in the article how we can create one with a few simple steps without any coding experience. let’s dig in!
What is Ansible?
Ansible is a configuration management tool created by Michael DeHaan. He founded Ansible, Inc. (originally AnsibleWorks, Inc.) in 2013 with Timothy Gerla and Saïd Ziouani to provide commercial support and sponsorship for the project. Also, it is agent-less. We can access and configure an unlimited number of servers with just the private key provided by the cloud platform or VM.
Why Ansible?
There are a lot of configuration management tools out there in the market, including Chef, Puppet, Salt, and more. However, configuring these tools is a very lengthy process, and sometimes it takes a lot of time to troubleshoot. Unlike Ansible, these tools require setting up an agent before starting to configure servers and VMs. So, Ansible has a plus point because of its agentless server management via private key.
How to install Ansible?
Installing Ansible is a lot easier than you think. Generally, Ansible is used in Red Hat and Debian OS. To install Ansible, we just need to type one command, and Ansible is ready to use. First, let’s see how to install Ansible in the Red Hat Operating System.
- python3 -m pip install –user ansible
- python3 -m pip install –user ansible-core
- python3 -m pip install –user ansible-core==2.12.3
- python3 -m pip install –upgrade –user ansible
Python is the initial for Ansible because Ansible uses Python’s environment. Once you have installed Python, let’s move to the next step.
- sudo yum-add-repository ppa:ansible/ansible
- sudo yum update
- sudo yum install ansible -y
Congratulations! You have successfully installed Ansible on the Red Hat Operating System. Let’s see how we can install it on Debian.
- python3 -m pip install –user ansible
- python3 -m pip install –user ansible-core
- python3 -m pip install –user ansible-core==2.12.3
- python3 -m pip install –upgrade –user ansible
Python is essential for the installation.
- sudo apt-add-repository ppa:ansible/ansible
- sudo apt update
- sudo apt install ansible -y
How easy it is to install Ansible on both OS. Furthermore, If you want to install it on Windows, then you need to install WSL after that, you will be able to use Ubuntu, which is Debian, and follow the second command and install Ansible on Windows.
How To Write First Ansible Playbook
After installation, it is quite easy to write first Ansible playbook. Before writing first Ansible playbook, let’s understand some basic concepts, including inventory, Modules, Tasks, and Roles.
Inventory: In the OS, we are using Ansible as a master; we need to add servers, which we will configure using Ansible. the Syntax is same for Debian and Red Hat:
[Group Name]
- server1 ansible_host=11.074.33.123 ansible_user=ubuntu –private-key=~/.ssh/ansible_key
- server2 ansible_host=23.470.33.143 ansible_user=ubuntu –private-key=~/.ssh/ansible_key
So, firstly, you have to define the group name braces and then list all server names and host IP with username and key. I have given the sample above, which represents the private key, in ~/.ssh, and I created a file myself ~/.ssh/ansible_key that contains the private key.
Modules: The modules are discrete units of code that serve as the fundamental building blocks for automation tasks within Ansible. An example here is the list of some commonly used modules:
System & User Management
-
ping -
user -
group -
hostname
Package Management
-
apt -
yum -
dnf -
package
File & Directory Management
-
file -
copy -
fetch -
template -
unarchive
Service & Process Management
-
service -
systemd -
shell -
command
Networking
-
uri -
get_url -
wait_for
We can simply run these modules like:- ansible localhost -m ping
Tasks: Automation or Configuration we do via a YAML script and use tasks to tell the master Ansible what to perform on other servers. The syntax is in an Ansible playbook.
Roles: Ansible Roles provide a structured and reusable way to organize Ansible automation content. They serve as self-contained units for grouping related tasks, variables, handlers, files, templates, and other assets within a predefined directory structure.
Write first Ansible Playbook: first of all, do sudo vim /etc/ansible/test.yml, it’s your choice whether you use nano, vi, or vim. Below is the written Ansible playbook:
- —
– name: Ping Playbook
hosts: rashid1
tasks:
– name: Ping the host
ping:
After writing the script, do:wq! for saving it.
Congratulations, you have successfully write first Ansible playbook. Now, let’s see how to execute it.
Simply run the command given below;
sudo ansible-playbook test.yml –private-key=~/.ssh/ansible_key
Finally, you have successfully written and executed the Ansible playbook, which will show the ping of as a output.
Conclusion
In a nutshell, you have to install Ansible and have minimal knowledge of inventory, tasks, modules, and roles. Once you have the knowledge of these, you are good to go and the YML language, which is quite easier compared to other programming languages. That’s all in this article. I hope you got the idea How To Write First Ansible Playbook as a Beginner.
FAQs
Q1: What is Ansible?
A1: Ansible is an agentless configuration management tool that automates server configuration using private keys.
Q2: Why should we use Ansible instead of other tools like Puppet or Chef?
A2: Unlike other tools, Ansible doesn’t require agents, making it simpler and faster to configure servers.
Q3: How do you install Ansible on Red Hat or Debian OS?
A3: Install Python first, then run sudo yum install ansible -y on Red Hat or sudo apt install ansible -y on Debian.
Q4: What are Ansible modules?
A4: Modules are small units of code used to perform automation tasks like managing users, packages, files, and services.
Q5: How do you execute your first Ansible playbook?
A5: Save the playbook as test.yml and run it with sudo ansible-playbook test.yml --private-key=~/.ssh/ansible_key.
𝗥𝗲𝗮𝗹𝗮𝗻𝗰𝗲𝗿 – 𝗙𝗿𝗲𝗲 𝗙𝗿𝗲𝗲𝗹𝗮𝗻𝗰𝗶𝗻𝗴, 𝗔𝗹𝘄𝗮𝘆𝘀!
💸 Apply to unlimited projects & tasks without paying connects or fees.
🧑💻 Work part-time, weekends, remote, or onsite — your schedule, your rules.
🌍 Get hired when you’re free and connect with clients worldwide.
🚀 Build your freelance career without barriers or hidden costs.
👉 Join the waitlist now: https://app.realancer.net
Read more blogs: Alitech Blog










Leave a Reply