Order of operations during Ansible playbook parsing explained!

We will delve into some specifics of how a playbook is parsed for Order of operations.

Ansible can be assumed to be executed in a top to bottom order; that is the operation listed at the top of a file will be accomplished before the operation listed at the bottom of a file. Having said that, there are a few caveats and even a few ways to influence the order of operations.

A playbook has only two main operations it can accomplish. It can either run a play, or it can include another playbook from somewhere on the filesystem. The order in which these are accomplished is simply the order in which they appear in the playbook file, from top to bottom. It is important to note that while the operations are executed in order, the entire playbook, and any included playbooks, is completely parsed before any executions. This means that any included playbook file has to exist at the time of the playbook parsing. They cannot be generated in an earlier operation.

Here is an example play with most of these operations shown:

- hosts: localhost
  gather_facts: false

    - a_var: derp

    - name: pretask
      debug: msg="a pre task"
      changed_when: true
      notify: say hi

    - role: simple
      derp: newval

    - name: task
      debug: msg="a task"
      changed_when: true
      notify: say hi

    - name: posttask
      debug: msg="a post task"
      changed_when: true
      notify: say hi


Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x