Skip to content

vars pre post and handlers

Mohit edited this page Aug 16, 2017 · 4 revisions

variables

Instead of passing the variables via the command line, we can pass the variables inline under the vars_files.

- hosts: all
  vars:
    ghost_username: ghost
    nginx_confd: /etc/nginx/nginx.conf 

However, soon you will have more variables and it will make your playbook look a bit cluttered. Luckily, Ansible also allows us to use a YAML file for declaring variables (e.g. vars.yml) and pass that under vars_files`. This helps in keeping the main playbook file clean and organized. In the playbook file, we need to tell Ansible where to look for variables. Your playbook.yml file would look something like this:

- hosts: all
  vars_files:
    - vars.yml

pre_tasks and post_tasks

Using pre_tasks and post_tasks user can run certain tasks before or after running the main set of tasks. A general use case for pre_task could be when you need to make sure that your system package manager's cache is updated. For post_task something like waiting for web servers to come back up after a (rolling-) update or something similar. In your main playbook, this would look something like this:

...
pre_tasks:
  - name: Updating apt cache if needed
    apt: update_cache=yes cache_valid_time=3600
...
post_tasks:
  - name: wait for webserver for come up
    wait_for: 'host={{ inventory_hostname }} port=80 state=started timeout=80'
...

Note how we reference to the variable using jinja2 template.

In the above example for pre_tasks asks Ansible to update the apt cache if it's older than the cache_valid_time. (This option is set in seconds).
For post_tasks, wait_for waits for a condition before continuing. In our case, it waits for the current host's (specified by inventory_hostname) port 80 to be opened. It will wait for timeout seconds before skipping.

handlers

These are a special type to tasks that are invoked by using notify in the main tasks. An important thing to remember is that if you have a group of tasks and somewhere some task in between calls notify: <handler_name>, the task specific to that handler will be fired only after the whole group has successfully finished executing. If any task fails, the handler won't be notified.

To make sure the handler is notified even in the case of a failure of other tasks in the group, pass the flag --force-handlers to ansible-playbook command.

The handlers look something like this:

handlers:
  - name: restart nginx
    service: name=nginx state=restarted

You can also put handlers in different file just like variables.

For a sample playbook file containing above materials, refer test/vagrant3

This test environment contains vagrant configuration with Ansible provisioning for running a ghost blogging platform behind Nginx which is serving as a reverse proxy.

Don't worry about the specific contents of playbook.yml file in it which may look overwhelming, we will cover that in upcoming posts (and also make it tidy and organized)