Skip to main content

Command Palette

Search for a command to run...

Ansible: A Simple Guide to Installation and Your First Playbook

Updated
3 min read
A

Associate DevOps Engineer Kubernetes | Docker | GCP/ AWS / Azure | Linux Web Development (HTML5, CSS3, Bootstrap, JS) Video Editing (DaVinci Resolve) Currently working on Terraform

Introduction

In the realm of automation, Ansible shines as a user-friendly tool that simplifies complex tasks. This blog post is your passport to Ansible, covering everything from installation to executing your first playbook. Let's dive in!

Installing Ansible

Getting started with Ansible is a breeze. Here's a simple guide to install Ansible on your system:

1. On Linux (Ubuntu):

sudo apt update
sudo apt install ansible

2. On MacOS:

brew install ansible

3. On Windows:

  1. Install Windows Subsystem for Linux (WSL).

  2. Open WSL and follow the Linux installation steps.

Once installed, you're ready to harness the power of Ansible.

Setting Up in the Master Node

Ansible follows a simple master-node architecture. Your machine, where Ansible is installed, becomes the control node, while the target machines are the managed nodes. Let's set up the master node:

1. Inventory File

Create a file named inventory.ini to define your managed nodes:

# Inventory File
# This file defines the managed nodes for Ansible.

# You can define single user
# Example
192.168.1.0
# Define a group of hosts named 'targets'
[targets]
# Specify the IP address of the managed node
[ip_address] ansible_ssh_user=your_username

# Specify additional managed nodes
# You can add more nodes with similar configurations
192.168.1.102 ansible_ssh_user=your_username
192.168.1.103 ansible_ssh_user=your_username

# Define a group of database servers
# Specify the IP address and SSH user for the database server
[database_servers]
10.0.0.1 ansible_ssh_user=db_admin

# Define a group of web servers
# Specify the IP address and SSH user for the web server
[web_servers]
10.0.0.2 ansible_ssh_user=web_admin

2. Configuring Ansible

Create a configuration file, ansible.cfg, to set up basic configurations:

[defaults]
inventory = ./inventory.ini

Your master node is ready to orchestrate tasks on the managed nodes.

Your First Ansible Playbook: Hello World Ping

Now, let's create a simple playbook to ensure everything is set up correctly. Create a file named hello_world_ping.yml:

---
# Hello World Playbook
# This playbook demonstrates pinging all machines and displaying a message.

# Define the playbook
- name: Hello World Playbook
  # Specify the group of hosts from the inventory file
  hosts: all

  # List of tasks to be executed
  tasks:
    # Task 1: Ping all machines
    - name: Ping All Machines
      # Ansible 'ping' module to check connectivity
      ping:

    # Task 2: Display "hello from team" message on the terminal
    - name: Display Hello Message
      # Ansible 'command' module to run a command on the control node
      command: echo "Hello from DevToDevOps team"

Here's a breakdown of what this playbook does:

  • name: A human-readable description for the playbook.

  • hosts: Specifies the group of hosts from the inventory file.

  • tasks: Defines the list of tasks to be executed.

  • ping: An Ansible module to check connectivity to the managed nodes.

  • command: An Ansible module that allows the execution of arbitrary commands on the control node.

Executing Your Playbook

Run your playbook using the following command:

ansible-playbook hello_world_ping.yml

If everything is set up correctly, you should see a successful ping response from the managed node and our message "Hello from DevToDevOps team" on the terminal.

Congratulations! You've just executed your first Ansible playbook. This is just the beginning – Ansible's simplicity and power await your exploration. Stay tuned for more Ansible adventures!

More from this blog

D

DevToDevops

8 posts

At DevToDevOps, we are on a mission to empower individuals and teams in the ever-evolving landscape of DevOps. Our goal is to provide a platform where enthusiasts, beginners, and seasoned professional