If you need to install tools like Nginx or MySQL on a server, it’s manageable. However, as the number of servers increases, repeating the same tasks becomes tedious and error-prone. This is where Ansible comes into play, automating processes to enhance efficiency.
Ansible is a tool that offers:
With Ansible, you write the code for installation once and deploy it across multiple servers, allowing you to focus on more productive tasks instead of repetitive ones.
To use Ansible, you’ll need:
Playbooks are configuration files containing instructions in YAML format. Here’s an example:
---
- name: step1
hosts: webserver1
tasks:
- name: "apt-get update"
apt:
update_cache: yes
cache_valid_time: 3600
- name: "install nginx"
apt:
name: ['nginx']
state: latest
- name: step2
hosts: dbserver1
tasks:
- name: "apt-get update"
apt:
update_cache: yes
cache_valid_time: 3600
- name: "install mysql"
apt:
name: ['mysql-server']
state: latest
Key points about playbooks:
---
.hosts
field specifies the target server(s) for the play and can be:
"all"
for all servers in the specified inventory."localhost"
for the server running the Ansible playbook."webserver1"
for a single target in the specified inventory."webservers"
for a list of targets in the specified inventory.The Inventory file classifies servers into groups. For example:
[appservers]
appserver1
appserver2
[webservers]
webserver1
webserver2
webserver3
[dbservers]
dbserver1
dbserver2
Ansible needs to be installed only on the local machine because it operates agentlessly. The playbook and inventory are specified on the local machine. Ansible executes plays on remote servers using the SSH protocol. If you can SSH into a server, you can run an Ansible playbook on that server.
For more detailed information, refer to the official Ansible documentation.
I hope this guide helps you understand the basics of Ansible and how it can streamline your IT automation tasks.