Ansible Conditionals not working as expected.

So This code here should only run if {{ passphrase_length.results[0].stdout }} is != "0"

However it seems to ignore the comparison.

I have even tried:

  when: passphrase_length.results[0].stdout|int != 0

No matter what I do it does not seem to behave as expected.

I have reached the point of desperation as this should work as coded, is there any strange trick required?

- name: Create empty Passphrase file
  file: path="{{ ssl_client_dir }}/{{ item.first_name }}_{{ item.surname }}.password" state=touch owner=root group=root mode=0755
  with_items: ssl_user_clients

- name: check passphrase
  shell: "cat {{ ssl_client_dir }}/{{ item.first_name }}_{{ item.surname }}.password | grep '^[0-9a-zA-Z!@$%^&*()_?~+-=/\\].*' | wc -c"
  with_items: ssl_user_clients
  register: passphrase_length
  ignore_errors: yes

- debug: msg="{{ passphrase_length.results[0].stdout }}"

- debug: var=passphrase_length

- name: Email Passphrase
  action: mail host=localhost port=25 subject="trial password" body="your password is {{ generated_passphrase.stdout }}" from="sender@example.com" to={{ item.email }} headers=Reply-To="sender@example.com"|X-Special="Something or other" charset=utf8
  with_items: ssl_user_clients
  register: pass_emailed
  when: passphrase_length.results[0].stdout != "0"

- name: Email Certificate
  action: mail host=localhost port=25 subject="Welcome to the Trial" body="{{ email_body }}" from="sender@example.com" to={{ item.email }} attach="{{ ssl_client_dir }}/{{ item.first_name }}_{{ item.surname }}.p12 {{ ssl_certs_dir }}/ca.crt" headers=Reply-To="sender@example.com"|X-Special="Something or other" charset=utf8
  with_items: ssl_user_clients
  register: cert_emailed
  when: passphrase_length.results[0].stdout != "0"

- name: Write password out
  lineinfile: dest="{{ ssl_client_dir }}/{{ item.first_name }}_{{ item.surname }}.password" state=present line="{{ generated_passphrase.stdout }}"
  with_items: ssl_user_clients
  when: passphrase_length.results[0].stdout != "0"

The log of the run:

TASK: [clientcert | Create empty Passphrase file] *****************************
changed: [10.76.71.194] => (item={'org_name': 'Acme Limited', 'first_name': 'road', 'surname': 'runner', 'locality': 'desert', 'country': 'Cartoonland', 'state': 'neverneverland', 'email': 'road.runner@acme.Cartoonland'})

TASK: [clientcert | check passphrase] *****************************************
changed: [10.76.71.194] => (item={'org_name': 'Acme Limited', 'first_name': 'road', 'surname': 'runner', 'locality': 'desert', 'country': 'Cartoonland', 'state': 'neverneverland', 'email': 'road.runner@acme.Cartoonland'})

TASK: [clientcert | debug msg="{{ passphrase_length.results[0].stdout }}"] ****
ok: [10.76.71.194] => {
    "msg": "51"
}

TASK: [clientcert | debug var=passphrase_length] ******************************
ok: [10.76.71.194] => {
    "var": {
        "passphrase_length": {
            "changed": true,
            "msg": "All items completed",
            "results": [
                {
                    "changed": true,
                    "cmd": "cat /etc/ssl/acme/clients/road_runner.password | grep '^[0-9a-zA-Z!@$%^&*()_?~+-=/\\].*' | wc -c",
                    "delta": "0:00:00.011639",
                    "end": "2015-04-29 10:04:39.908388",
                    "invocation": {
                        "module_args": "cat /etc/ssl/acme/clients/road_runner.password | grep '^[0-9a-zA-Z!@$%^&*()_?~+-=/\\].*' | wc -c",
                        "module_name": "shell"
                    },
                    "item": {
                        "country": "Cartoonland",
                        "email": "road.runner@acme.Cartoonland",
                        "first_name": "road",
                        "locality": "desert",
                        "org_name": "Acme Limited",
                        "state": "neverneverland",
                        "surname": "runner"
                    },
                    "rc": 0,
                    "start": "2015-04-29 10:04:39.896749",
                    "stderr": "",
                    "stdout": "51",
                    "stdout_lines": [
                        "51"
                    ],
                    "warnings":
                }
            ]
        }
    }
}

TASK: [clientcert | Email Passphrase] *****************************************
ok: [10.76.71.194] => (item={'org_name': 'Acme Limited', 'first_name': 'road', 'surname': 'runner', 'locality': 'desert', 'country': 'Cartoonland', 'state': 'neverneverland', 'email': 'road.runner@acme.Cartoonland'})

TASK: [clientcert | Email Certificate] ****************************************
ok: [10.76.71.194] => (item={'org_name': 'Acme Limited', 'first_name': 'road', 'surname': 'runner', 'locality': 'desert', 'country': 'Cartoonland', 'state': 'neverneverland', 'email': 'road.runner@acme.Cartoonland'})

TASK: [clientcert | Write password out] ***************************************
changed: [10.76.71.194] => (item={'org_name': 'Acme Limited', 'first_name': 'road', 'surname': 'runner', 'locality': 'desert', 'country': 'Cartoonland', 'state': 'neverneverland', 'email': 'road.runner@acme.Cartoonland'})

In your first example, too many moustaches!!, when has an implied {{ }} around the expression, it won’t work when you add {{ }} inside a when.

have you tried this?:

when: not passphrase_length.results[0].stdout == “0”