Ansible Playground

Ansible Playground

Introduction

Ansible is an open-source automation tool that allows you to automate IT tasks, such as configuration management, application deployment, and infrastructure orchestration. It uses a simple and human-readable language called YAML (Yet Another Markup Language) for defining automation tasks.

Setting up Ansible Local Environment using VirtualBox

To set up Ansible locally, we need a virtual environment.

Here's a step-by-step guide using VirtualBox:

  1. Install VirtualBox on your host machine from the official website (https://www.virtualbox.org).

  2. Download an Ubuntu Server ISO image from the official Ubuntu website (https://ubuntu.com/download/server).

  3. Open VirtualBox and click on "New" to create a new virtual machine.

  4. Provide a name for the virtual machine and select the appropriate operating system and version.

  5. Allocate memory, storage, and other settings for the virtual machine.

  6. Start the virtual machine and follow the Ubuntu installation process.

  7. Once Ubuntu is installed, open a terminal within the virtual machine.

Installing Ansible

Now that we have our virtual environment ready with several virtual machines, let's install Ansible. In the terminal of one of your Ubuntu virtual machines, run the following commands:

Update the system packages:

sudo apt update

sudo apt upgrade

Install Ansible using the package manager:

sudo apt install ansible

Verify the installation by checking the Ansible version:

ansible --version

If everything is installed correctly, you should see the version of Ansible installed on your system.

The machine with Ansible installed that manages and orchestrates other machines is called the control node. The control node is where you run Ansible commands, create and execute playbooks, and manage the automation tasks for the target machines.

The control node serves as the central point of control and coordination for Ansible. It interacts with the target machines over SSH (Secure Shell) or other remote connection methods to execute tasks and configurations.

The control node requires Ansible to be installed and configured properly. It should have access to the Ansible inventory file, which contains the details of the target machines, and any required playbooks, roles, or other resources.

Ansible Concepts

Ansible Inventory

Ansible inventory is a file that contains a list of hosts or groups of hosts on which Ansible executes tasks. It serves as a source of truth for Ansible to know which machines to target and perform automation tasks on. The inventory file can be written in INI-like format or YAML format. Here's an example of an inventory file in INI format:

[web]

webserver1.example.com

webserver2.example.com


[database]

dbserver.example.com

Breaking down the inventory file:

Ansible Playbooks

Ansible playbooks are written in YAML and contain a set of tasks that should be executed on remote hosts. Playbooks define the desired state of the system. Here's an example of a simple playbook that installs a package using the apt module:

---

- name: Install Nginx

  hosts: web

  become: true

  tasks:

    - name: Install Nginx package

      apt:

        name: nginx

        state: present

To run the playbook, use the following command:

ansible-playbook playbook.yml

Breaking down the playbook step by step:

  • name: Install Nginx: This line describes the playbook. It is a human-readable identifier that helps in identifying the purpose of the playbook.

  • hosts: web: This line specifies the target hosts on which the tasks in the playbook will be executed. In this case, it is the web group from the inventory file. You can define multiple groups or individual hosts here.

  • become: true: This line enables privilege escalation, allowing the playbook to execute tasks with elevated privileges, such as using sudo. This is often necessary for system-level configurations or package installations.

  • tasks: This section defines the tasks that Ansible will execute on the target hosts.

  • - name: Install Nginx package: This line describes the task. It helps in identifying the specific task being executed.

  • apt: This line specifies the module to be used for this task. In this case, it is the apt module, which is responsible for managing packages on Debian-based systems.

  • name: nginx: This line specifies the name of the package (nginx) that the apt module will manage.

  • state: present: This line indicates that the desired state of the package is "present," meaning Ansible will ensure that the Nginx package is installed on the target hosts. If the package is already installed, Ansible will do nothing. If it is not installed, Ansible will install it.

When you run this playbook using the ansible-playbook command, Ansible will connect to the specified target hosts, check if Nginx is installed, and if not, it will install the package using the apt module.

Ansible Modules

Ansible modules are reusable units of code that perform specific tasks on remote hosts. There are various types of modules available, such as command modules, script modules, service modules, and more.

Command Module:

Executes a command on the remote host.

Here's an example that runs the ls command:

- name: Run ls comman
  command: ls

Script Module:

Copies and executes a script on the remote host.

Here's an example that copies and runs a script:

- name: Run ls comman
  command: ls

Service Module:

Manages services on the remote host.

Here's an example that starts the Nginx service:

  - name: Start Nginx service

    service:

      name: nginx

      state: started

Lineinfile Module:

Modifies a specific line in a file on the remote host.

Here's an example that adds a line to the /etc/hosts file:

  - name: Add an entry to /etc/hosts

    lineinfile:

      path: /etc/hosts

      line: "192.168.1.10  host.example.com"

Ansible Variables

Ansible allows you to use variables in playbooks to make them more dynamic and reusable. Variables can be defined in various places, such as inventory files, playbooks, or external variable files.

Here's an example of using variables in a playbook:

---

- name: Install Apache

  hosts: web

  become: true

  vars:

    http_port: 80

    app_name: myapp

  tasks:

    - name: Install Apache package

      apt:

        name: apache2

        state: present


    - name: Configure Apache virtual host

      template:

        src: templates/virtualhost.conf.j2

        dest: /etc/apache2/sites-available/{{ app_name }}.conf

In this example, we defined two variables http_port and app_name in the playbook:

  • http_port: This variable is set to the value 80. It represents the port number on which the Apache service will listen. By using a variable, the playbook becomes more flexible, allowing you to easily change the port number if needed.

  • app_name: This variable is set to the value myapp. It represents the name of the application or website that will be configured in the Apache virtual host. By using a variable, you can easily customize the application name without modifying the playbook itself.

Conditionals, Loops, and Roles

Ansible Conditionals

Ansible provides several ways to use conditionals in playbooks, such as using when statements and failed_when statements.

Here's an example of using a when statement:

---

- name: Install package if OS is Ubuntu

  hosts: web

  become: true

  tasks:

    - name: Install package

      apt:

        name: mypackage

        state: present

      when: ansible_distribution == "Ubuntu"

In this example, the apt module will only be executed if the distribution of the remote host is Ubuntu.

Ansible Loops

Loops in Ansible allow you to iterate over a list of items or dictionary entries and perform tasks repeatedly. There are different ways to use loops in Ansible, such as using the loop keyword or with_items keyword. Here's an example of using the loop keyword:

---

- name: Create multiple users

  hosts: web

  become: true

  tasks:

    - name: Create user accounts

      user:

        name: "{{ item }}"

        state: present

        password: "{{ item | password_hash('sha512') }}"

      loop:

        - user1

        - user2

        - user3

In this example, the user module will be executed multiple times, creating three user accounts specified in the loop statement.

Ansible Roles

Roles in Ansible allow you to package a playbook and its associated files into a reusable component. A role typically contains tasks, handlers, templates, variables, and other files organized in a specific directory structure.

Here's an example of a simple role structure:

myrole/
  ├── tasks/
  │   └── main.yml
  ├── handlers/
  │   └── main.yml
  ├── templates/
  ├── vars/
  ├── defaults/
  └── meta/

To use a role in a playbook, you can include it using the roles keyword:

---

- name: Use myrole

  hosts: web

  become: true

  roles:

  - myrole

In this example, the role myrole will be applied to the hosts specified in the playbook.

To run the playbook with the role, use the following command:

ansible-playbook playbook.yml

Key Points

  • Ansible is a powerful automation tool that simplifies the management and configuration of systems at scale. It uses a declarative approach, allowing you to describe the desired state of your infrastructure and automate tasks accordingly.

  • To get started with Ansible, you can set up a local environment using tools like VirtualBox to create multiple virtual machines. Installing Ansible involves configuring the control node, which is the machine where Ansible is installed and used to manage other machines.

  • The Ansible inventory file plays a crucial role in connecting Ansible with target machines. It defines the hosts and groups of hosts that Ansible can manage. By configuring the inventory file, you specify which machines Ansible should interact with and perform automation tasks on.

  • Ansible playbooks provide a way to define automation workflows. Playbooks are written in YAML and consist of a series of tasks that Ansible executes on the target machines. Tasks can utilize various modules, such as the command, script, service, or lineinfile module, to perform specific actions on the hosts.

  • Variables in Ansible playbooks enable flexibility and customization. By using variables, you can define reusable values that can be easily changed or passed externally. Variables can be used to define parameters for tasks, customize configurations, or adapt playbooks to different environments.

  • Conditionals and loops in Ansible allow you to introduce logic and control flow into your playbooks. Conditionals enable you to perform tasks based on certain conditions, while loops allow you to repeat tasks for multiple iterations or a specific set of items.

  • Ansible roles help in packaging playbooks into reusable units. Roles encapsulate related tasks, variables, templates, and other resources into a structured directory layout. This makes it easier to share and reuse automation code across different projects and teams.

In summary, Ansible provides a flexible and scalable framework for automating infrastructure management. By understanding Ansible's concepts, such as the inventory, playbooks, variables, conditionals, loops, and roles, you can efficiently manage and configure systems, saving time and effort in repetitive tasks and ensuring consistency across your infrastructure.