What is Ansible?
Ansible is a free tool that runs the same tasks on many servers at once. Imagine you must update ten servers. Doing it by hand takes ages. With Ansible you write the task once and it does the work on all ten. It connects through SSH. SSH is a safe way to control a faraway server by typing commands.
The machine where you install Ansible is the control node. The servers it manages are called hosts.
Install Ansible on Ubuntu
- Log in to your control node with SSH.
- Update the package list:
sudo apt update - Install Ansible:
This command downloads Ansible and what it needs.sudo apt install ansible - Check it:
You should see a version number.ansible --version
On CentOS or AlmaLinux, install the EPEL repository first, then run sudo yum install ansible. A repository is an online store of software.
Tell Ansible about your servers
- Open the inventory file. It is a list of your hosts:
sudo nano /etc/ansible/hosts - Add a group name and your server addresses. Use your own details:
The address here is only an example. Your real IP address is in your welcome email.[webservers] server1 ansible_host=203.0.113.10 ansible_user=root - Save with Ctrl+O, Enter, then exit with Ctrl+X.
Set up SSH keys
- Create a key pair:
Press Enter to accept the defaults.ssh-keygen - Copy the key to your server:
This lets Ansible log in without a password.ssh-copy-id root@203.0.113.10
Run your first command
ansible webservers -m ping
This command asks every host in the webservers group to answer. A reply of pong means it works.
Next step: a playbook
A playbook is a file of tasks written in YAML, a simple text format. You run it with ansible-playbook file.yml.
Quick recap
- Ansible runs tasks on many servers.
- Install it with
sudo apt install ansible. - List your servers in
/etc/ansible/hosts. - Use SSH keys for login.
- Test with
ansible webservers -m ping.