ansible

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?


Solution

  • Try:

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

    or

    when: '"nginx version: nginx/1.8.0" not in nginxVersion.stdout'