Skipping Tasks based on Changed

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $10.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Thanks to the when key, you can make your playbooks really really smart and really really fast. What else could we do? Well, sometimes, our code doesn't change. When that happens, the "Checkout Git repository" will report as "ok", so not Changed.

If we know that the code didn't change... then it might not make sense to install our composer dependencies. After all, if the code didn't change, how would the composer dependencies need to change? And we could skip other things, like running the migration or even clearing the cache.

Under the "Git" task, register a new variable: repo_code:

---
- hosts: vb
... lines 3 - 26
tasks:
... lines 28 - 138
- name: Checkout Git repository
... lines 140 - 143
register: repo_code
... lines 145 - 248

We already know from the output that we're looking for the changed key on this variable. That means, we could use repo_code.changed in the when option of some tasks to skip them.

Using set_fact to Clean Variables

But, we can get fancier! Below this task, add a new one called "Register code_changed variable". We'll use the set_fact module from earlier. This time, create a new variable called code_changed set to, very simply, repo_code.changed:

---
- hosts: vb
... lines 3 - 26
tasks:
... lines 28 - 138
- name: Checkout Git repository
... lines 140 - 143
register: repo_code
... lines 145 - 147
- name: Register code_changed variable
set_fact:
code_changed: repo_code.changed
... lines 151 - 250

The only reason we're doing this is to make our when statements a little cleaner. Add our tag onto that.

Down below, under "Install Composer's Dependencies", add when: code_changed:

---
- hosts: vb
... lines 3 - 26
tasks:
... lines 28 - 188
- name: Install Composer's dependencies
... lines 190 - 194
when: code_changed
... lines 196 - 250

Tip

Ansible does not allow evaluating bare variables anymore. The code_changed variable is not obviously a boolean value because it just references to another variable called repo_code.changed, so Ansible requires the |bool filter to be added to the code_changed in when clauses:

# ansible/playbook.yml
---
- hosts: vb
  # ...
  tasks:
    # ...
    - name: Install Composer's dependencies
      # ...
      when: code_changed | bool

Ah, so nice. Copy that and put it anywhere else it makes sense like "Execute migrations" and "Clear Cache":

---
- hosts: vb
... lines 3 - 26
tasks:
... lines 28 - 215
- name: Execute migrations
... lines 217 - 221
when: code_changed
... lines 223 - 230
- name: Clear cache
... lines 232 - 235
when: code_changed
... lines 237 - 250

Phew! Ok, run the playbook - but take off the verbose flag:

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini -t deploy

As cool as this is, you need to be careful. As you make your playbook smarter, it's also more and more possible that you introduce a bug: you skip a task when it should actually run. I've just made a small mistake... which we'll discover soon.

But for now, it's so cool: the 3 Composer tasks were skipped, as well as installing Composer's dependencies, migrations and the "Clear Cache". And thanks to that, the playbook ran way faster than before.

It's time to talk about getting our playbook a bit more organized. As you can see... it's getting big... and I'm getting a bit lost in it. Fortunately, we have a few good ways to fix this.

Leave a comment!

This tutorial is built using an older version of Symfony, but the core concepts of Ansible are still valid. New versions of Ansible may contain some features that we don't use here.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.12
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.0
        "snc/redis-bundle": "^2.0", // 2.0.0
        "predis/predis": "^1.1" // v1.1.1
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.8
        "symfony/phpunit-bridge": "^3.0", // v3.1.4
        "doctrine/data-fixtures": "^1.1", // 1.3.3
        "hautelook/alice-bundle": "^1.3" // v1.4.1
    }
}