We are adding some additional tasks to our existing playbook which install Nginx. But we dont want Nginx Installation task to be tried everytime, we only want it to be tried when it is not already installed.
Is there a way to skip Installation task if Nginx is already present?
- name: Install nginx
yum:
name: nginx
state: present
when: Nginx already present?
become_user: root
2
Answers
As you can see at yum module documentation website, the present parameter checks whether given application is present on the machine. If it is already installed, the terminal will return green OK, which means that the state of the application (nginx) has been checked and is as intended. Ansible will not install nginx again, as it exists, so in fact, this step (Install nginx) will be skipped. Ansible returns something for every step, which helps debugging and keeping track of the progress.
Ansible works based on idempotency which implies that if the package is not installed will attempt to install it but if it’s already present, will do nothing that it’s what you want, to skip the task if it’s already present.
There’s no divergence to what you want if you do in the way you already have as there’s no attempts to install as it’s detected that it’s already present and in the latest version from your repositories.
E.G.: