Ansible Fundamentals Simplified

Ansible Fundamentals Simplified

ยท

6 min read

Ansible is like a digital assistant for your computer systems. It's a tool that helps you automate and manage tasks on your servers, like installing software, configuring settings, and even updating applications. Instead of doing these tasks manually, you can tell Ansible what to do, and it takes care of them for you, making your work easier and more efficient.

Ansible Architecture

Ansible's setup is quite straightforward. There's one main computer called the 'Management Node,' and all the other computers where we want to apply our configuration changes connect to it using a secure connection called SSH. It's like having one boss computer that gives orders to the others through a secret connection.

How Ansible Works?

Ansible has two main parts: the 'control node,' which is any computer with Ansible installed, and the 'managed nodes,' which are the computers we want to control. The control node talks to the managed nodes and tells them what to do.

Ansible uses small bits of code called 'modules' to do tasks on the managed nodes. These modules are like tools in a toolbox. You put these tools into a 'task' list, and that list is called a 'playbook.' So, a playbook is like a set of instructions for Ansible to follow.

We write these playbooks in a simple language called YAML. It's easy to understand, even if you're just starting. The best part is that Ansible doesn't need any extra software on the managed nodes. You only need a computer with Ansible, a terminal to give commands, and a text editor to write your instructions.

Ansible Installation

To get going with Ansible, you need to put it on your main computer, which we'll call the 'control node.' This could be your laptop, for instance. This control node talks to and manages other computers, telling them what to do.

You'll follow some steps to install Ansible, and the exact way to do it depends on your computer and what system it uses. But a common way is to use a tool called 'pip.'

  1. You need to add the Ansible repository to your system. This repository holds all the important files and stuff that Ansible needs.

     sudo apt-add-repository ppa:ansible/ansible
    
  2. Update the system.

     sudo apt update
    
  3. Finally, go ahead and install Ansible.

     sudo apt install ansible
    

    Remember, you only have to run these commands on the master (control) node.

Basic Concepts & Terms

  • Host: A remote machine managed by Ansible.

  • Group: In Ansible, a group is like a folder for organizing computers in your inventory. It helps you group similar computers together, so you can give them all the same instructions at once. Think of it as sorting your toys into different boxes based on what they are.

  • Inventory: The inventory in Ansible is like a list of all the computers Ansible can control. It's where Ansible looks to find out which computers it can talk to and manage. Think of it as a guest list for a party, showing who's invited and ready to participate.

    Here's a basic example of an Ansible inventory file:

      [web_servers]
      server1 ansible_host=192.168.1.101
      server2 ansible_host=192.168.1.102
    
      [web_servers:vars]
      ansible_python_interpreter=/usr/bin/python3
      ansible_user=ubuntu
      ansible_ssh_private_key_file=/home/ubuntu/.ssh/ssh-key.pem
    

    You can see the default inventory of Ansible by using this command ansible-inventory --list -y. You can find the default inventory of Ansible in this location: "/etc/ansible/hosts."

  • Modules: Modules in Ansible are like small, specialized tools that help Ansible perform specific tasks on remote computers. They are like individual tools in a toolbox. Ansible uses these modules to do things like installing software, managing files, or configuring settings on the managed computers. Think of them as the different functions or actions Ansible can perform.

  • Ad hoc commands: Ad hoc commands in Ansible are quick, one-time tasks you can give to Ansible without writing a whole playbook. You just tell Ansible what to do on the command line, and it does it right away on the remote computers. It's like asking Ansible to do a small job without writing a detailed plan. For eg:

      ubuntu@172.90.183.12:$~ ansible web_servers -a "sudo apt update"
    
  • Tasks: In Ansible, tasks are like recipe steps, specifying actions for Ansible to perform on remote computers, such as installing software or copying files. They are part of playbooks, which are instructions for Ansible to follow when managing computers.

  • Playbooks: In Ansible, playbooks are like detailed cooking recipes. They contain a series of tasks that tell Ansible what to do on remote computers, step by step. Playbooks are written in a structured format and allow you to define how Ansible should manage and configure those computers. Think of playbooks as the complete set of instructions for Ansible to follow.

    Here's a basic example of an Ansible Playbook to create a file:

      -
          name: File Creation Playbook
          hosts: web_servers
          tasks:
              - name: Creating file
                command: touch file.txt
    
  • Roles: In Ansible, roles are like pre-packaged sets of instructions that simplify common tasks. They help organize playbooks, acting as ready-made templates for managing systems consistently.

  • YAML: In Ansible, YAML is a plain and easy-to-read format for writing instructions that Ansible can understand. It's like a simplified way of giving Ansible directions in a text file.

Conditions in Playbooks

Conditions in Ansible playbooks are like decision points that allow you to control the flow of tasks based on certain criteria. They help you make choices during automation.

Here's how they work:

  1. Define a Condition: You specify a condition, like "if a file exists" or "if a service is running."

  2. Set Actions: You decide what should happen if the condition is true or false. For example, you can say, "If the file exists, copy another file; otherwise, create a new one."

  3. Benefits: Conditions make your automation smarter. They enable your playbooks to adapt to different situations, making your tasks more flexible and responsive to changes in your system.

In Ansible playbooks, you can add conditions using the when keyword. Here's a simple example of how to add a condition to a task:

-
    name: File Creation Playbook
    hosts: web_servers
    tasks:
        - name: Creating file
          command: touch file.txt

          when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

The playbook above makes a file in the "web_servers" group, but only if the operating system on those servers is either Debian or Ubuntu.

Installing Nginx & Docker with Playbooks

We're going to install Docker and Nginx on servers in the 'web_servers' group, depending on their operating system. If it's Ubuntu or Debian, we'll install Docker; if it's Red Hat or CentOS, we'll install Nginx. We'll use Ansible playbooks to do this and run it on remote servers connected through SSH.

Here is the YAML file that does what we talked about earlier.

-
  name: Ansible-Playbook
  hosts: web_servers
  become: yes
  tasks:
    - name: Installing Docker
      apt:
        name: docker.io
        state: latest

    - name: Starting docker
      service:
        name: docker.io
        state: started
        enabled: yes

      when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

    - name: Installing Nginx
      apt:
        name: nginx
        state: latest

    - name: Starting nginx
      service:
        name: nginx
        state: started
        enabled: yes

      when: ansible_distribution == 'Red Hat Enterprise Linux' or ansible_distribution == 'CentOS'

In conclusion, Ansible is a powerful tool for automating tasks and managing infrastructure. By simplifying complex operations and streamlining repetitive tasks, Ansible empowers you to work more efficiently and effectively. So, start exploring Ansible's capabilities, and you'll discover how it can transform your automation journey. Happy automating!

ย