In this tutorial, you will learn how to use Ansible, an open-source IT automation tool. Ansible allows you to automate software provisioning, configuration management, and application deployment on multiple machines.
1. What is Ansible?
Ansible is an IT automation tool that helps you manage servers, deploy applications, and perform system configurations.
It uses simple YAML files to define tasks and workflows, and it works by connecting to remote machines over SSH (or WinRM for Windows) without requiring agents to be installed on the remote hosts.
Key Features of Ansible:
- Agentless: No agents need to be installed on the target machines.
- Declarative: You define what you want the system to look like, and Ansible ensures the system matches that state.
- Idempotent: Ansible ensures that running the same task multiple times does not change the outcome unless necessary.
2. Ansible Installation
Before you can start using Ansible, you need to install it on your local machine (control machine).
Ansible is typically installed on Linux or macOS systems, and it can be installed via pip (Python package manager) or using the package manager for your distribution.
Installing Ansible with Pip
Install Python (if not already installed).
Use pip to install Ansible:
pip install ansible
Installing Ansible on Ubuntu (Linux)
sudo apt update
sudo apt install ansible
Verify Installation
After installation, verify it by checking the version of Ansible:
ansible --version
3. Ansible Basics
Ansible works by interacting with inventory (hosts or machines) and using playbooks to define automation tasks.
Ansible Inventory
The inventory is a list of all the machines you want to manage. It can be a simple file or an external source like a cloud provider.
You can define the inventory in a simple text file, typically located at /etc/ansible/hosts.
Example of an Inventory File:
[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com
db2.example.com
Alternatively, you can specify the inventory file using the -i flag:
ansible -i inventory_file all -m ping
Ansible Playbooks
Playbooks are YAML files where you define the tasks that need to be executed on the managed machines.
A playbook can have multiple plays, each targeting a group of hosts and a set of tasks.
Here’s an example of a basic playbook that installs Nginx on the web servers:
---
- name: Install Nginx on webservers
hosts: webservers
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
Explanation:
name: Describes the task in human-readable terms.
hosts: Specifies which group of machines (from the inventory) to target.
become: Indicates whether to run the task with elevated privileges (e.g., using sudo).
tasks: A list of tasks to run.
4. Ansible Commands
You can interact with Ansible through the command line using various commands. Here are a few common ones:
Running a Single Command on Multiple Hosts
You can use ansible to run a module on a host or group of hosts.
Example (ping all hosts in the inventory):
ansible all -m ping
This will attempt to ping all the hosts in your inventory.
Running a Playbook
To run a playbook, use the ansible-playbook command.
Example:
ansible-playbook install_nginx.yml
This command will run the install_nginx.yml playbook, which installs Nginx on the machines listed in the webservers group.
5. Creating and Running an Ansible Playbook
Let's create a simple playbook to install Apache on a group of web servers.
Example Playbook: Install Apache
---
- name: Install Apache Web Server
hosts: webservers
become: yes
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache service
service:
name: apache2
state: started
enabled: yes
Running the Playbook
Now, you can run the playbook with the following command:
ansible-playbook -i inventory_file install_apache.yml
This will:
Update the apt cache.
Install the Apache package.
Ensure the Apache service is started and enabled on boot.
6. Ansible Modules
Ansible modules are units of work that Ansible executes. They perform tasks like installing software, copying files, or managing services. Some common modules include:
apt: For managing packages on Debian-based systems.
yum: For managing packages on Red Hat-based systems.
service: For managing services (e.g., starting, stopping, enabling).
copy: For copying files to remote servers.
Example using the copy module to copy a file to a remote server:
- name: Copy a file to the webserver
hosts: webservers
tasks:
- name: Copy index.html
copy:
src: ./index.html
dest: /var/www/html/index.html
7. Ansible Roles
Roles are a way to organize your playbooks and make them reusable. Roles can contain tasks, templates, variables, and files needed to configure a specific part of your infrastructure.
Creating a Role
To create a role, you can use the ansible-galaxy command:
ansible-galaxy init apache
This will generate a directory structure like:
apache/
├── defaults/
├── files/
├── handlers/
├── meta/
├── tasks/
│ └── main.yml
├── templates/
└── vars/
Now, you can define tasks in the tasks/main.yml file and include the role in your playbook:
---
- name: Install Apache Web Server
hosts: webservers
become: yes
roles:
- apache
8. Best Practices
Use Variables: Use variables to make playbooks more flexible and reusable.
vars:
apache_port: 80
tasks:
- name: Ensure Apache is listening on port 80
service:
name: apache2
state: started
enabled: yes
notify:
- restart apache
Use Handlers: Handlers are special tasks that are only triggered when notified (e.g., restart a service after a configuration change).
Use Tags: Tags allow you to run specific parts of your playbook. For example, if you want to run only the "install" part of your playbook:
ansible-playbook install_apache.yml --tags install
Group Your Hosts: Group your hosts logically in the inventory file (e.g., webservers, dbservers).
Use Ansible Vault: Ansible Vault allows you to encrypt sensitive data (e.g., passwords, API keys) in your playbooks.
9. Conclusion
In this tutorial, we've covered the basics of Ansible, including:
- Installation and setup
- Ansible inventory and playbooks
- Running Ansible commands and playbooks
- Using Ansible modules and roles
- Best practices for writing maintainable playbooks
Ansible is a powerful tool for automating server management, deployment, and configuration tasks. By learning how to structure your playbooks and use Ansible effectively, you can automate repetitive tasks and make your infrastructure more efficient and scalable.
As you get more comfortable with Ansible, you can explore advanced topics like Ansible Galaxy (to reuse existing roles), Ansible Tower (for a web UI), and more complex workflows.
Happy automating!