Getting Started with Ansible: A Beginner's Guide

Vivesh - Oct 28 - - Dev Community

Ansible is a powerful open-source automation tool that simplifies IT tasks like configuration management, application deployment, and orchestration. It allows you to automate repetitive tasks, manage infrastructure as code, and ensure consistency across your environments. If you're new to Ansible, this guide will help you get started with an introduction and a basic example.


What is Ansible?

Ansible is a tool that automates tasks across multiple systems. It uses a simple language called YAML (Yet Another Markup Language) to describe how a system should be configured or what tasks should be run. The key features of Ansible include:

  • Agentless: Unlike other automation tools, Ansible doesn’t require any agents installed on the target machines. It uses SSH to connect to them, which makes it easy to set up.
  • Declarative Language: You describe the desired state of your systems in playbooks, and Ansible takes care of achieving that state.
  • Idempotency: Running a playbook multiple times won’t cause issues; Ansible ensures that the end state is always the same.

Setting Up Ansible

Before we start with examples, let’s set up Ansible on your local machine.

Step 1: Install Ansible

Ansible can be installed using pip (Python package manager) or from your system's package manager.

For Ubuntu/Debian-based systems:

sudo apt update
sudo apt install ansible -y
Enter fullscreen mode Exit fullscreen mode

For CentOS/RHEL-based systems:

sudo yum install epel-release -y
sudo yum install ansible -y
Enter fullscreen mode Exit fullscreen mode

Using pip:

pip install ansible
Enter fullscreen mode Exit fullscreen mode

To confirm the installation, run:

ansible --version
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up SSH Access to Remote Machines

Ansible communicates over SSH. You should be able to SSH into your remote machines without a password prompt using SSH keys. You can set up SSH keys using:

ssh-keygen -t rsa
ssh-copy-id user@remote_host
Enter fullscreen mode Exit fullscreen mode

Replace user and remote_host with your remote machine’s username and IP address.


Ansible Concepts

Before diving into examples, let’s understand some basic terms:

  • Inventory: A file that defines the list of servers Ansible will manage.
  • Playbook: A YAML file that contains tasks for Ansible to execute.
  • Task: A single unit of work, like installing a package or copying a file.
  • Module: Ansible uses modules to execute tasks (e.g., yum, apt, copy, file).

Example 1: Creating a Simple Playbook

Let's create a basic playbook to install the Apache web server on a remote host.

  1. Define the Inventory File

Create a file called hosts.ini and specify the IP addresses or hostnames of the servers you want to manage:

[webservers]
192.168.1.10
Enter fullscreen mode Exit fullscreen mode
  1. Write the Playbook

Create a file named install_apache.yml:

---
- name: Install Apache on web servers
  hosts: webservers
  become: yes
  tasks:
    - name: Ensure Apache is installed
      apt:
        name: apache2
        state: present
      when: ansible_os_family == "Debian"

    - name: Ensure Apache is installed (CentOS/RedHat)
      yum:
        name: httpd
        state: present
      when: ansible_os_family == "RedHat"

    - name: Start and enable Apache service
      service:
        name: "{{ ansible_os_family == 'Debian' | ternary('apache2', 'httpd') }}"
        state: started
        enabled: true
Enter fullscreen mode Exit fullscreen mode

Here’s what this playbook does:

  • name: Describes the playbook action.
  • hosts: Specifies which servers will run the playbook.
  • become: yes: Tells Ansible to use sudo to run tasks.
  • tasks: Contains the actions (installing packages, starting services).
  • when: Conditional statements to handle different operating systems.
  1. Run the Playbook

To run the playbook, execute:

ansible-playbook -i hosts.ini install_apache.yml
Enter fullscreen mode Exit fullscreen mode

Ansible will SSH into the servers listed under [webservers] in hosts.ini, install Apache, and ensure the service is running.


Example 2: Create a Simple File Using Ansible

Suppose you want to create a file on a remote server using Ansible. You can use the file module for that.

  1. Playbook to Create a File

Create a new file create_file.yml:

---
- name: Create a file on remote servers
  hosts: webservers
  become: yes
  tasks:
    - name: Create a file named "info.txt"
      file:
        path: /tmp/info.txt
        state: touch
        owner: root
        mode: '0644'
Enter fullscreen mode Exit fullscreen mode

This will create a new file /tmp/info.txt on the remote machine with the specified permissions.

  1. Run the Playbook

Run it using:

ansible-playbook -i hosts.ini create_file.yml
Enter fullscreen mode Exit fullscreen mode

Tips for Beginners

  1. Start Simple: Begin with basic tasks like installing packages or managing services before moving on to complex playbooks.
  2. Modular Approach: Reuse tasks across multiple playbooks by writing roles.
  3. Learn YAML: Understanding YAML syntax is essential for writing effective playbooks.
  4. Use Dry Runs: Test playbooks without making changes using the --check option:
   ansible-playbook -i hosts.ini install_apache.yml --check
Enter fullscreen mode Exit fullscreen mode

Happy Automating!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .