How Ansible works: Ansible for Beginners

Ansible for Beginners

Ansible is one of the most popular tools for IT automation. It simplifies server management and deployment without the need for installing agents. In this guide, we’ll explain how Ansible works and demonstrate some basic ad-hoc commands.

What is Ansible?

Ansible is an open-source tool that automates configuration management and application deployment. It’s agentless, meaning you don’t need to install any software on the machines you’re managing. This makes Ansible easy to use.

How Ansible Works

Ansible works on push mode means it will push the configuration to the managed node to perform operations.

  1. Control Node: This node has Ansible installed, and users interact with it to configure or deploy on the managed/target nodes.
  2. Managed Nodes: This is the node that is our target machine on which we need to perform the actual operation. We don’t need to install any package on this machine, we just need python to be installed on this node. That is why we call Ansible an agentless tool.
  3. Inventory: This file contains information about our target/managed nodes.

Ansible Architecture Diagram

Workflow of Ansible

Ansible Workflow

  1. The user interacts with the control node using either Ansible playbooks or ad-hoc commands (for quick one-time tasks).
  2. The control node communicates with managed nodes using SSH.
  3. Once a connection is established, Ansible sends the required modules to the managed nodes to perform tasks like configuration, installation, or updates.

This process keeps Ansible simple, flexible, and powerful for handling automation across a wide range of environments.

Password-Based vs. Passwordless Authentication in Ansible

When setting up Ansible, you need to establish communication between the control node and managed nodes. Ansible uses SSH for this, and there are two common ways to authenticate: password-based authentication and passwordless authentication.

  1. Password-Based Authentication: In this setup, each time the control node connects to a managed node, it prompts for a password.
  2. Passwordless Authentication (Preferred Method): For a smoother experience with Ansible, it’s better to set up passwordless authentication using SSH key pairs between the control node and the managed nodes. This eliminates the need for manual password input during every connection, making automation more efficient. Here’s how you can set it up:

Generate an SSH key pair on the control node:

ssh-keygen

Copy the public key to the managed nodes using:

ssh-copy-id user@managed-node-ip

Now, you can run Ansible commands without prompting for a password:

ansible all -m ping

Passwordless authentication is more secure and efficient, especially in large-scale environments. It’s the recommended method for automating tasks with Ansible.

Why Python is Important

Both the control and managed nodes need Python installed. Ansible’s modules are written in Python, making it necessary for executing tasks. Managed nodes, especially those based on Linux, may already have Python installed.

Running Ansible Ad-hoc Commands

Let’s jump into some basic ad-hoc commands. These are quick, one-time tasks that don’t require playbooks.

  • Ping All Nodes:
ansible all -m ping

This command checks the connection to all managed nodes. If everything is set up correctly, you should see successful responses.

  • Check Uptime on Web Servers:
ansible all -m command -a "uptime"

This command checks the uptime of all the servers.

  • Install Nginx:
ansible webservers -m package -a "name=nginx state=present"

Use this to install Nginx on your web servers.

Conclusion

Ansible makes automation accessible by simplifying complex tasks like server management and deployment. Its agentless nature and the use of ad-hoc commands offer a quick way to manage servers with minimal setup. Whether you’re just getting started or looking to automate larger deployments, understanding Ansible’s architecture and mastering basic commands is a solid foundation.

As you become more comfortable, you can explore writing playbooks for repeatable and complex tasks, making Ansible a powerful tool in your DevOps arsenal.

For those who prefer visual learning, you can also check out my video on this topic.

Thank you for reading, and I’d love to hear your thoughts or questions!


If you’re new to Ansible or looking to dive deeper, check out my Ansible Playlist on YouTube for step-by-step tutorials.

2 thoughts on “How Ansible works: Ansible for Beginners”

  1. Pingback: Mastering the Ansible Configuration File: A Beginner’s Guide - Techi Nik

  2. Pingback: Ansible Inventory Management: HAProxy and Web Servers — Step-by-Step Guide - Techi Nik

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top