You can check if something is installed from trying a simple command, register the output, use the output.failed
in the when:
section of your install tasks to only run when your check failed.
Using docker as an example.
Version command in a task:
- name: Check If Docker Is Installed
command: docker --version
register: docker_valid
ignore_errors: yes
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
- name: Debug Docker Output
debug:
var: docker_valid
Next task only runs if the docker_vaild.failed
returns true, because of the when clause. Add this when:
in each task you want to run when docker is not installed. If docker is installed then these tasks will be skipped.
- name: Install Docker pre-requisites
yum:
name: yum-utils
state: latest
when: docker_valid.failed
...
Using that format you can install docker or anything else you need.
To reverse the conditional then you add not
on the when:
clause.
- name: Run Task If Docker is Already Installed
debug:
msg: "will run if docker is installed"
when: not docker_valid.failed