From ArchWiki
Jump to: navigation, search


Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.


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


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.

A template is available at GitHub example ansible.cfg.


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:

localhost ansible_connection=local


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:

  • local to deploy the playbook to the control machine itself
  • docker deploys the playbook directly into Docker containers

Check Ansible - intro inventory for details.


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

$ ansible all -m ping


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):

- name: All hosts up-to-date
  hosts: control managed
  become: yes
    - name: full system upgrade
        update_cache: yes
        upgrade: yes

Then, run the playbook script:

$ ansible-playbook --ask-become-pass syu.yml


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:

$ ansible-vault encrypt_string --vault-id vault_pass.txt 'the var content' --name varname

More securely, to avoid inputting the variable content in the command line and be prompted for it instead, one can use:

$ ansible-vault encrypt_string --vault-id vault_pass.txt --stdin-name varname
Reading plaintext input from stdin. (ctrl-d to end input)

The command returns directly the protected variable that can be inserted into a playbook. Encrypted and non-encrypted variables can coexist in a YAML file as illustrated below:

notsecret: myvalue

mysecret: !vault |

other_not_secret: othervalue

Package management

Official repositories

Ansible has a pacman module to handle installation, removal and system upgrades with pacman.


For the Arch User Repository (AUR), an external module is required: ansible-aur — Ansible module to install packages from the AUR || ansible-aur-gitAUR

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:

- user: name=aur_builder

- lineinfile:
     path: /etc/sudoers.d/aur_builder-allow-to-sudo-pacman
     state: present
     line: "aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman"
     validate: /usr/sbin/visudo -cf %s
     create: yes

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:

- user:
  name: user_name
  password: "{{ 'user_password' | password_hash('sha512', 'mypermsalt') }}"
  shell: /usr/bin/nologin
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:

$ openssl passwd -1 user_password
$ python -c 'import crypt; print(crypt.crypt("user_password", crypt.mksalt(crypt.METHOD_SHA512)))'

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].



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.

See also