Introduction
This tutorial will guide you through the process of installing Ansible, a powerful open-source IT automation tool, on the Ubuntu operating system. You will learn how to set up Ansible and execute Ansible playbooks to streamline your infrastructure management tasks.
Understanding Ansible
Ansible is an open-source software provisioning, configuration management, and application-deployment tool. It is designed to be simple, powerful, and agentless, allowing users to manage and configure multiple remote systems simultaneously.
What is Ansible?
Ansible is a powerful automation tool that allows you to manage your infrastructure, applications, and services in a declarative and idempotent way. It uses a simple, human-readable language called YAML to define the desired state of your systems, and then executes the necessary tasks to achieve that state.
Key Features of Ansible
- Agentless: Ansible does not require any special software or agents to be installed on the remote systems. It uses SSH (or Windows Remote Management) to communicate with the target systems.
- Declarative Approach: Ansible uses a declarative approach, where you define the desired state of your systems, and Ansible takes care of the necessary steps to achieve that state.
- Idempotency: Ansible's tasks are designed to be idempotent, meaning that they can be run multiple times without causing any unintended changes.
- Modular Design: Ansible has a modular design, with a wide range of built-in modules that can be used to perform various tasks, such as managing files, installing packages, or interacting with cloud services.
- Simple and Readable: Ansible's YAML-based language is designed to be simple and easy to read, making it accessible to both developers and system administrators.
Ansible Use Cases
Ansible can be used for a wide range of tasks, including:
- Infrastructure Provisioning
- Configuration Management
- Application Deployment
- Orchestration
- Security and Compliance
- Continuous Integration/Continuous Deployment (CI/CD)
Ansible Architecture
Ansible uses a client-server architecture, where the Ansible control node (the system where Ansible is installed) communicates with the remote target systems (the systems that you want to manage) over SSH or Windows Remote Management.
graph TD
A[Ansible Control Node] -- SSH/WinRM --> B[Target System 1]
A -- SSH/WinRM --> C[Target System 2]
A -- SSH/WinRM --> D[Target System 3]
The Ansible control node executes the necessary tasks on the target systems, and the target systems communicate the results back to the control node.
Installing Ansible on Ubuntu
Prerequisites
Before installing Ansible, make sure you have the following prerequisites:
- Ubuntu 22.04 LTS system
- Python 3 installed
- sudo or root privileges
Install Ansible
- Update the package index:
sudo apt update
- Install the required packages:
sudo apt install software-properties-common
- Add the Ansible PPA (Personal Package Archive) to your system's sources list:
sudo add-apt-repository --yes --update ppa:ansible/ansible
- Install Ansible:
sudo apt install ansible
- Verify the installation:
ansible --version
You should see the Ansible version information displayed.
Configure Ansible
- Create an Ansible inventory file:
sudo nano /etc/ansible/hosts
Add the target hosts to the inventory file, for example:
[webservers]
192.168.1.100
192.168.1.101
[databases]
192.168.1.200
192.168.1.201
- Create an Ansible configuration file:
sudo nano /etc/ansible/ansible.cfg
Add the desired configuration options, for example:
[defaults]
inventory = /etc/ansible/hosts
remote_user = ubuntu
- Test the Ansible connection:
ansible all -m ping
This command will ping all the hosts in the inventory file and verify the connection.
Now, you have successfully installed Ansible on your Ubuntu 22.04 system and configured it to manage your target hosts.
Executing Ansible Playbooks
What is an Ansible Playbook?
An Ansible Playbook is a YAML-formatted file that defines the desired state of your infrastructure or application. It contains a collection of tasks, which are executed in a specific order to achieve the desired state.
Anatomy of an Ansible Playbook
A basic Ansible Playbook consists of the following elements:
- Hosts: The target hosts or groups that the playbook will be executed on.
- Tasks: The actions that Ansible will perform on the target hosts.
- Variables: Values that can be used throughout the playbook.
- Handlers: Tasks that are triggered by other tasks.
Here's an example Ansible Playbook that installs the Apache web server on a group of Ubuntu hosts:
- hosts: webservers
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache
service:
name: apache2
state: started
enabled: yes
Executing an Ansible Playbook
To execute an Ansible Playbook, follow these steps:
- Save the playbook to a file, for example,
apache.yml. - Run the playbook using the
ansible-playbookcommand:
ansible-playbook apache.yml
This will execute the tasks defined in the playbook on the target hosts.
Playbook Execution Options
You can customize the playbook execution by using various options, such as:
- Limit: Execute the playbook on a specific host or group.
ansible-playbook apache.yml -l webservers - Check: Perform a dry run to see what changes would be made.
ansible-playbook apache.yml --check - Become: Execute the playbook with elevated privileges.
ansible-playbook apache.yml --become
By understanding and using Ansible Playbooks, you can easily automate the deployment and configuration of your infrastructure and applications on Ubuntu systems.
Summary
By the end of this tutorial, you will have a solid understanding of Ansible and its capabilities, as well as the knowledge to install Ansible on your Ubuntu system and start automating your infrastructure management tasks using Ansible playbooks.


