Installation

On the control machine (master), install the ansible package. python is a dependency of the package and is required on the master.

On the managed machines (nodes), where you want to automate deployment or configuration tasks, python is required and it may be necessary to indicate the specific #Python binary location in some circumstances.
A way to communicate with the node is also necessary, this is usually SSH. Note that a functioning SSH key setup eases the use of Ansible but is not required.

Basic usage

Configuration

Ansible parameters are set in the configuration file which can either be ansible.cfg in the current directory, .ansible.cfg in the home directory or /etc/ansible/ansible.cfg, whichever it finds first.

Inventory

The infrastructure is listed in the Ansible inventory file, which defaults to being saved in the location /etc/ansible/hosts or one can specify a different inventory file using the -i command line switch. For instance, the following inventory defines a cluster with 7 nodes organized into two groups:

One can assign specific attributes to every node in the infrastructure file at the corresponding line or in the ansible.cfg configuration file.
By default Ansible executes playbooks over SSH, the ansible_connection parameter extends the connection to:

Ping

You may check if all the nodes listed in the inventory are alive by

$ ansible all -m ping

Playbook

Playbooks are the main organizational unit to configure and deploy the whole infrastructure. Check the official document for more details. Here is an extremely simple demonstration, where the administrator of the above inventory wants to perform a full system upgrade on a set of Arch Linux hosts. First, create a playbook file, with YAML formatting (always 2 spaces indentation):

Vault

A vault can be used to keep sensitive data in an encrypted form in playbooks or roles, rather than in plaintext.
The vault password can be stored in plaintext in a file, for example vault_pass.txt containing myvaultpassword, to be used later on as a command parameter:

$ ansible-playbook site.yml --vault-id vault_pass.txt

In order to encrypt the content the var content of a variable named varname using the password stored in vault_pass.txt, the following command should be used:

AUR

While Ansible expects to ssh as root, AUR helpers do not allow executing operations as root, they all fail with "you cannot perform this operation as root". For Ansible automation, it is therefore recommended to create a user, for example named aur_builder, that has no need for password with pacman in sudoers. This can be done in Ansible with the following actions:

Then, AUR helpers or makepkg can be used associated with the Ansible parameters become: yes and become_user: aur_builder

Tips and tricks

User account creation

Ansible can manage user accounts and in particular it is able to create new ones. This is achieved in playbooks with the user module which takes an optional password argument to set the user's password. It is the hashed value of the password that needs to be provided to the module.
The hashing can simply be performed on the fly within Ansible using one of its internal hash-filters:

Tip: The salt should be fixed and explicitly supplied as a second parameter of the hash function for the operation to be idempotent (can be repeated without changing the state of the system).

With this approach it is recommended to vault-encrypt user_password so that it does not appear in plain text, see #Vault. However, an encrypted variable cannot be piped directly and will first need to be assigned to another one that will be piped.

Alternatively, the hashing can be performed outside Ansible. The following commands return respectively the MD5 and the SHA512 hashed values of user_password:

Python binary location

Ansible requires Python on the target machine. By default Ansible assumes it can find a /usr/bin/python on the remote system that is a 2.X or 3.X version, specifically 2.6 or higher.

If some of your modules specifically require Python2, you need to inform Ansible about its location by setting the ansible_python_interpreter variable in the inventory file. This can be done by using host groups in the inventory:

More information about Python version support in Ansible is available in [1], [2] and [3].

Troubleshooting

Unarchive

The unarchive module unpacks an archive. However tar files are not well supported and several outstanding issues are reported in GitHub - unarchive.
In particular when the parameter keep_newer is set to yes, idempotence is not observed. In case you face an issue with the module, you can use instead the zip format which is better integrated in ansible.