Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible condition when string not matching

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?

like image 684
Steve E. Avatar asked Sep 25 '15 15:09

Steve E.


People also ask

What does Set_fact do in Ansible?

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.

Can we define multiple conditions in Ansible?

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.

How do I compare versions in Ansible?

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.


1 Answers

Try:

when: nginxVersion.stdout != 'nginx version: nginx/1.8.0' 

or

when: '"nginx version: nginx/1.8.0" not in nginxVersion.stdout' 
like image 158
Vor Avatar answered Sep 19 '22 22:09

Vor