Setup Ansible

How to setup Ansible to facilitate server management tasks


Ansible is a tool for server management. It uses SSH to connect to the servers and execute scripts. Scripts can be organized as "playbooks" to automate tasks such as server configuration, failover and load balancing, etc.

Installation Using APT

apt install ansible

Inventory Configuration

Edit the file /etc/ansible/hosts and create your inventory like this:

# define a group of machines and specify port for ssh if not 22
vm1 ansible_port=22 ansible_host=
vm2 ansible_port=22 ansible_host=
vm3 ansible_port=22 ansible_host=

# another group
vm0 ansible_port=22 ansible_host=

# group of groups


Variables for hosts and groups can be set in individual files:


SSH Configuration

Make sure that you can login using ssh on all servers

Test Connection

ansible all -m ping


ansible vms -m ping

Create Master Playbook

Define a playbook for the complete infrastructure in site.yml:

- import_playbook: common.yml
- import_playbook: webservers.yml

Create Tasks in Individual Playbooks

Configure apt and install packages:

- hosts: all
   - name: Install pv
     apt: pkg=pv state=installed update_cache=true


Execute Playbooks

ansible-playbook site.yml --limit vms