I’m trying to make a conditional “when” in my ansible playbook.
If docker not installed, install docker.
So i have a playbook, with a role with some tasks in it.
And i would like to do something like
when: docker != not exist
or
when: docker == false
When i get setup, from one with docker installed i get this:
"ansible_docker0": {
"active": true,
"device": "docker0",
"features": {
When no docker :
SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
3
Answers
It just seems that, the task still running, and installing docker. ? Should it not be skipped if it is installed ?
I'm also looking for the other way around. If the docker (or something else ) is installed, then skip the rest of the tasks.
A: Use package – Generic OS package manager. The task below will install the
package
if it has not been installed yet. It’s conditional by default.A: For details see How to get the installed yum packages with Ansible?.
You can check if something is installed from trying a simple command, register the output, use the
output.failed
in thewhen:
section of your install tasks to only run when your check failed.Using docker as an example.
Version command in a task:
If you don’t include
ignore_errors: yes
and you don’t have it installed, then your playbook running the task will exit, and we want to continue.You can use the debug module to print the registered variables to the output
Next task only runs if the
docker_vaild.failed
returns true, because of the when clause. Add thiswhen:
in each task you want to run when docker is not installed. If docker is installed then these tasks will be skipped.Using that format you can install docker or anything else you need.
To reverse the conditional then you add
not
on thewhen:
clause.