Ansible

Introduction
Ansible is an open-source agentless IT automation, infrastructure management, configuration management, software provisioning, and application-deployment tool.

The only prerequisites to use it are Python 2.7 or Python 3.5+ on both control node and managed nodes and SSH access from the control node to the managed nodes.

Installation
Installing Ansible is very simple since it is available in the official distribution repositories: sudo apt install ansible ansible-lint

sudo yum install ansible ansible-lint

It is also possible to install it with pip: sudo python get-pip.py sudo pip install ansible

To install Ansible command shell completion: sudo [apt|yum|pip] install python-argcomplete sudo activate-global-python-argcomplete

Directories and files: mkdir -p /etc/ansible/group_vars mkdir -p /etc/ansible/host_vars mkdir -p /etc/ansible/playbooks mkdir -p /etc/ansible/roles mkdir -p /etc/ansible/template touch /etc/ansible/hosts

Inventory
The inventory is a file, usually in INI or YAML format, that lists and groups all managed nodes:

Its default location is:
 * /etc/ansible/hosts

This is an example in INI format:

ad-hoc commands
An ad-hoc command can be used to automate a single task on one or more managed nodes: ansible [--become] [-u ] [--check] [-m ] [-a ""] 

Some examples: ansible -a "hostname -f" localhost

ping module
Useful to verify the ability to login and that a usable Python is configured: ansible -m ping all

shell module
Execute commands through a shell (/bin/sh): ansible -m shell -a "free -m | grep Swap" db

copy module
Copy files from the local machine to a location on the remote machine: ansible -m copy -a "src=/repo/httpd.conf dest=/tmp/httpd.conf owner=root group=root mode='0644'" http

fetch module
Retrieve files from remote machines and store them locally in a file tree, organized by hostname: ansible -m fetch -a "src=/etc/chrony/chrony.conf dest=/repo/" http

file module
Manage files and file properties: ansible -m file -a "path=/opt/mydir state=directory mode='0755'" db

service module
Manage services on remote hosts: ansible --become -m service -a "name=httpd state=reloaded" waf:web

package module
Installs, upgrade and removes packages using the underlying OS package manager: ansible --become -m package -a "name=curl state=latest" all

Facts
Facts represent discovered variables about a system. You can use facts to implement conditional execution of tasks but also just to get ad-hoc information about your systems:

ansible -m setup all

ansible -m setup -a 'gather_subset=!all,!any,!min,virtual' all

ansible -m setup -a 'filter=ansible_distribution*' all

Variables
Their default location are:
 * /etc/ansible/host_vars
 * /etc/ansible/group_vars

Roles
Their default location is:
 * /etc/ansible/roles

Playbooks
Playbook files list sets of tasks, in YAML format, that will be run against a particular server or set of servers.

Their default location is:
 * /etc/ansible/playbooks

This is an example of a playbook:

To run it: ansible-playbook [--become] [-u ] [--check] [--syntax-check] [--list-hosts] [--limit ] playbook.yml

Links

 * Ansible Official Documentation
 * Ansible Galaxy
 * Awesome Ansible