Skip to content. | Skip to navigation

Personal tools
Log in
You are here: Home An opinionated short guide on how to use Ansible

An opinionated short guide on how to use Ansible

published Jun 30, 2017 12:35   by admin ( last modified Jun 30, 2017 12:39 )

Only use files that end in ".yaml". Except for the ansible.cfg, which need to be in INI format.


There are playbooks which configure your servers. You can have one playbook which is basically empty and is just listing what other playbooks to call, in sequence. So let's call the main playbook "manage.yaml" and the other ones "stage1.yaml", "stage2.yaml" and so on which are just listed in "manage.yaml", one after the other.

- hosts: cluster
- include: ./stages/stage1.yaml nodes=cluster

These playbooks all take their configuration variables from something called the inventory.


The inventory can be specified on the command line with  the "-i" flag. Here is what an inventory file could look like for a testing setup:

      name: node1
      some_app_privKey: 3132333435363738393031323334353637383930313233343536373839303131
      some_app_pubKey: 02ee99c1a7f1defbf22c5c6c58f738755836a6797e45ce569ffb80428e7d07540e
      name: node2
      some_app_privKey: 3132333435363738393031323334353637383930313233343536373839303132
      some_app_pubKey: 03c7a5a5f02f132b6047ee6bfc8a0b01a2b9c3f28f914fae750faf0e35b5d03e7c

  vars: # common values for all nodes, can be put per node above too, if need be
    ansible_user: user        # user name
    ansible_ssh_private_key_file: "{{ inventory_dir }}/id_rsa"
    production: False


The inventory is just a list of whatever data you need to deploy: Ip numbers, private keys for signing stuff, public keys, config variables and so on.

Sub playbooks - stages

Each playbook contains steps, which are done in sequence, once for each server listed in the inventory.

For each step you can do a test if the step should run at all, then test if the step is already installed, and if not run the step. The step mutates the server in some way: Installs a package, configures a config file, or copies over some files, or starts/restarts a server process. That is about it.


- hosts: "{{ nodes }}"

  become: true
  - name: Update repositories cache and ensure "libpq-dev" package is installed
      name: libpq-dev
      update_cache: yes


At first, Ansible seems to be very smooth but there are some problems mainly with syntax where you have to do a bit of googling on how to do stuff.

Here is a list of my ansible problems, to date: