I am trying to write an Ansible playbook that only compiles Nginx if it's not already present and at the current version. However it compiles every time which is undesirable.
This is what I have:
- shell: /usr/local/nginx/sbin/nginx -v 2>&1 register: nginxVersion - debug: var=nginxVersion - name: install nginx shell: /var/local/ansible/nginx/makenginx.sh when: "not nginxVersion == 'nginx version: nginx/1.8.0'" become: yes
The script all works apart from the fact that it runs the shell script every time to compile Nginx. The debug output for nginxVersion is:
ok: [server] => { "var": { "nginxVersion": { "changed": true, "cmd": "/usr/local/nginx/sbin/nginx -v 2>&1", "delta": "0:00:00.003752", "end": "2015-09-25 16:45:26.500409", "invocation": { "module_args": "/usr/local/nginx/sbin/nginx -v 2>&1", "module_name": "shell" }, "rc": 0, "start": "2015-09-25 16:45:26.496657", "stderr": "", "stdout": "nginx version: nginx/1.8.0", "stdout_lines": [ "nginx version: nginx/1.8.0" ], "warnings": [] } } }
According to the documentation I am on the right lines, what simple trick am I missing?
This module allows setting new variables. Variables are set on a host-by-host basis just like facts discovered by the setup module. These variables will be available to subsequent plays during an ansible-playbook run.
Defining multiple when conditions in Ansible I want to reboot Debian or Ubuntu Linux system after kernel update, and the inventory hostname must be aws-proxy-server . If both conditions are true, then issue the reboot command using the Ansible reboot module. Otherwise, skip the reboot option.
For example, you may need to check the current version of an application and compare it with the latest one to take a decision if update is required. To compare versions in Ansible we can use the version test, and in this note i will show the examples of how to use it.
Try:
when: nginxVersion.stdout != 'nginx version: nginx/1.8.0'
or
when: '"nginx version: nginx/1.8.0" not in nginxVersion.stdout'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With