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.

Playbooks


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.

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:

cluster:
  hosts:
    192.168.0.79:
      name: node1
      some_app_privKey: 3132333435363738393031323334353637383930313233343536373839303131
      some_app_pubKey: 02ee99c1a7f1defbf22c5c6c58f738755836a6797e45ce569ffb80428e7d07540e
    192.168.1.86:
      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.

(stage1.yaml)

- hosts: "{{ nodes }}"

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

Problems


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: