Ansible: A Complete Guide with Examples
Ansible is an open-source automation tool designed to simplify IT tasks such as configuration management, application deployment, and orchestration. It uses simple YAML syntax to define tasks and playbooks, making it both easy to learn and efficient to use. Ansible works over SSH, so there's no need for additional software or agents on target machines, making it lightweight and highly scalable.
Key Features of Ansible
Ansible Architecture
Getting Started with Ansible
To get started with Ansible, you need to install it on the control node. Installation is simple and supported on most Linux distributions as well as macOS.
Installing Ansible on Ubuntu
bash
sudo apt update sudo apt install ansible -y
Installing Ansible on CentOS
bash
sudo yum install epel-release -y sudo yum install ansible -y
Verifying Installation
bash
ansible --version
You should see the version of Ansible and Python dependencies printed in the output.
Ansible Inventory
Ansible uses an inventory file to define which machines to manage. The inventory file is typically located at /etc/ansible/hosts. You can also specify custom inventory files.
Sample Inventory
[webservers]
192.168.1.10
192.168.1.11
[dbservers]
192.168.1.20
In the above example, we have two groups: webservers and dbservers, with their respective IP addresses.
Writing Your First Ansible Playbook
Playbooks are a collection of plays, and plays consist of tasks. Let's start with a simple playbook that installs Nginx on a group of web servers.
领英推荐
Example Playbook: Install Nginx
---
- hosts: webservers
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
Running the Playbook
ansible-playbook install-nginx.yml
This playbook installs Nginx on all machines in the webservers group. The become: yes directive allows Ansible to run commands with elevated privileges (i.e., using sudo).
Example 2: Creating a User on Multiple Servers
This playbook creates a new user called deploy on all webservers with a specific home directory and shell.
---
- hosts: webservers
become: yes
tasks:
- name: Create a new user
user:
name: deploy
shell: /bin/bash
home: /home/deploy
state: present
Example 3: Copying Files to Multiple Servers
You can use Ansible to copy files or directories to remote nodes. Here's an example of copying an index.html file to web servers.
---
- hosts: webservers
tasks:
- name: Copy index.html to webservers
copy:
src: /path/to/local/index.html
dest: /var/www/html/index.html
Example 4: Restarting Services
This playbook restarts the Nginx service on all web servers.
---
- hosts: webservers
become: yes
tasks:
- name: Restart Nginx
service:
name: nginx
state: restarted
Using Roles for Better Organization
As your playbooks grow, you may want to organize them better. Ansible roles provide a way to group related tasks, variables, and files. Roles are stored in the roles/ directory of your project and are referenced in playbooks.
Example Role Directory Structure
roles/
webserver/
tasks/
main.yml
files/
index.html
templates/
nginx.conf.j2
vars/
main.yml
You can invoke a role in a playbook like this:
---
- hosts: webservers
roles:
- webserver
Conclusion
Ansible is a powerful tool for automating IT tasks. Its simplicity, agentless architecture, and wide range of modules make it suitable for small projects as well as large-scale enterprise environments. With playbooks, inventories, and roles, Ansible allows you to automate everything from server setup to application deployment.
With examples like installing packages, creating users, copying files, and restarting services, you can now start automating your infrastructure and applications with Ansible. Happy automating!