If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeThanks 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.
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.
Hey Cédric,
Thank you for your feedback! That's great you can follow the videos and understand it. Btw, we also have English subtitles, you can enable them in the video player with "CC" button in case you don't know.
Also, thank you for mention this new deprecation, I added a note about it: https://github.com/knpunive...
Cheers!
I have noticed what I think is a bug. When you check if the repo has changed, I had to check for remote_url_changed however this only works after the repo has already been cloned once. The first time you run this against a new vagrant instance the remote_url_changed key does not exist.
Hey Simon,
Hm, interesting find! I agree, it's a bit weird. Well, maybe Ansible devs decided if that URL has not been set yet - then it wasn't changed for the first time or, probably, they just missed it. Anyway, I think you can open an issue in Ansible to discus this behavior, probably, you really found a bug.
Cheers!
// 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
"composer/package-versions-deprecated": "^1.11" // 1.11.99
},
"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
}
}
Hello,
My first message on the platform!
Thank you very much for the quality of your tutorials.
Despite my poor English proficiency, I can follow the courses.
Ansible does notice: [DEPRECATION WARNING]: evaluating repo_code.changed as a bare variable.
To solve this notification, just add a pipe bool (|bool) like this :
when: code_changed|bool
Cheers!