Setup Ansible
How to setup Ansible to facilitate server management tasks
Introduction
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 [vms] vm1 ansible_port=22 ansible_host=10.20.1.75 vm2 ansible_port=22 ansible_host=10.20.1.54 vm3 ansible_port=22 ansible_host=10.20.1.88 # another group [admin] vm0 ansible_port=22 ansible_host=10.20.1.78 # group of groups [mymachines:children] vms admin
Variables
Variables for hosts and groups can be set in individual files:
/etc/ansible/group_vars/vms/db_settings /etc/ansible/group_vars/vms/vpn_settings /etc/ansible/host_vars/vm0/network_settings
SSH Configuration
Make sure that you can login using ssh
on all servers
Test Connection
ansible all -m ping
or
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 tasks: - name: Install pv apt: pkg=pv state=installed update_cache=true
Execute Playbooks
ansible-playbook site.yml --limit vms