Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Ansible Configuration Tutorial

Introduction

Ansible is an open-source automation tool used for IT tasks such as configuration management, application deployment, and task automation. This tutorial will guide you through the process of configuring Ansible from start to finish.

Installing Ansible

To start with Ansible, you need to install it on your control node. You can install Ansible using package managers like apt for Ubuntu or yum for CentOS.

For Ubuntu:

sudo apt update
sudo apt install ansible

For CentOS:

sudo yum install epel-release
sudo yum install ansible

Configuring Ansible

Once Ansible is installed, the next step is its configuration. The primary configuration file is ansible.cfg.

Create a directory for your Ansible project and create the ansible.cfg file inside it:

mkdir my_ansible_project
cd my_ansible_project
touch ansible.cfg

Edit the ansible.cfg file to set up your configuration:

[defaults]
inventory = ./inventory
remote_user = your_user
private_key_file = /path/to/your/private/key
host_key_checking = False

Defining the Inventory

The inventory file lists the hosts and groups of hosts on which Ansible commands, modules, and tasks will operate. Create an inventory file in your project directory:

touch inventory

Edit the inventory file to add your hosts:

[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11

[dbservers]
db1 ansible_host=192.168.1.20
db2 ansible_host=192.168.1.21

Testing the Configuration

After configuring Ansible, it's a good idea to test the connection to your hosts. Use the ping module to ensure Ansible can communicate with your nodes:

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

Creating Playbooks

Playbooks are Ansible's configuration, deployment, and orchestration language. They describe the desired state of your systems in simple, human-readable YAML syntax. Create a playbook file, for example, site.yml:

touch site.yml

Edit the site.yml file to define your tasks:

---
- name: Ensure web servers are running
hosts: webservers
tasks:
- name: install apache
apt:
name: apache2
state: present
become: yes

- name: Ensure database servers are running
hosts: dbservers
tasks:
- name: install mysql
apt:
name: mysql-server
state: present
become: yes

Running Playbooks

To apply the configurations defined in your playbook, run the following command:

ansible-playbook site.yml
PLAY [Ensure web servers are running] ****************************

TASK [Gathering Facts] ******************************************
ok: [web1]
ok: [web2]

TASK [install apache] ******************************************
changed: [web1]
changed: [web2]

PLAY [Ensure database servers are running] **********************

TASK [Gathering Facts] ******************************************
ok: [db1]
ok: [db2]

TASK [install mysql] ******************************************
changed: [db1]
changed: [db2]

PLAY RECAP ******************************************************
web1 : ok=2 changed=1 unreachable=0 failed=0
web2 : ok=2 changed=1 unreachable=0 failed=0
db1 : ok=2 changed=1 unreachable=0 failed=0
db2 : ok=2 changed=1 unreachable=0 failed=0

Conclusion

Congratulations! You have successfully configured Ansible and run your first playbook. This tutorial covered the basics, including installation, configuration, defining the inventory, creating playbooks, and running them. Ansible is a powerful tool, and this is just the beginning. Explore further to automate complex IT tasks with ease.