Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using Ansible - Comprehensive Tutorial

Introduction

Ansible is an open-source automation tool that simplifies tasks such as configuration management, application deployment, and task automation. It uses a simple language (YAML) and does not require any agent software on the client machines. This tutorial will guide you through the basics of using Ansible, from installation to writing your first playbook.

Installing Ansible

The first step in using Ansible is to install it on your control node (the machine from which you will manage your infrastructure).

For Debian/Ubuntu:

sudo apt update
sudo apt install ansible

For CentOS/RHEL:

sudo yum install epel-release
sudo yum install ansible

Verify the installation:

ansible --version

Setting Up Inventory

Ansible uses an inventory file to keep track of all the systems it manages. The default inventory file is located at /etc/ansible/hosts, but you can specify a different file if needed.

Example inventory file:

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

Testing Connectivity

Before you start managing your servers, you should test the connectivity to ensure Ansible can reach them. You can use the ping module for this purpose.

ansible all -m ping
web1.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
web2.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
db1.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
db2.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Writing Your First Playbook

A playbook is a YAML file that defines a series of tasks to be executed on your managed nodes. Below is an example of a simple playbook that installs Nginx on your webservers.

---
- name: Install Nginx on webservers
  hosts: webservers
  become: yes
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

Save this content to a file called install_nginx.yml, and then run it using the following command:

ansible-playbook install_nginx.yml

Managing Configuration Files

Ansible can also be used to manage configuration files on your servers. You can use the copy module to copy files from your control node to the managed nodes.

---
- name: Deploy Nginx configuration
  hosts: webservers
  become: yes
  tasks:
    - name: Copy Nginx configuration file
      copy:
        src: ./nginx.conf
        dest: /etc/nginx/nginx.conf
      notify:
        - Restart Nginx

  handlers:
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted

Run the playbook with the following command:

ansible-playbook deploy_nginx_config.yml

Using Roles for Reusability

Roles allow you to organize your playbooks into reusable components. Each role is a collection of tasks, templates, files, and variables that can be reused across multiple playbooks.

Create a role directory structure:

ansible-galaxy init myrole

This will create a directory structure like this:

myrole/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

You can now define tasks, handlers, and variables within their respective directories and include the role in your playbooks.

Conclusion

You've now learned the basics of using Ansible, from installation to writing playbooks and using roles. Ansible is a powerful tool that can greatly simplify your IT automation tasks. As you gain more experience, you'll find even more ways to leverage its capabilities to streamline your work.