# Using variables in changed\_when/failed\_when directives

**URL:** <https://forum.ansible.com/t/using-variables-in-changed-when-failed-when-directives/10652>\
**Category:** Get Help\
**Tags:** variables, playbook, ansible-core, kubernetes\
**Created:** [November 3, 2024, 12:16pm UTC](https://forum.ansible.com/t/using-variables-in-changed-when-failed-when-directives/10652 "2024-11-03T12:16:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![S1ructure](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/s1ructure/32/5335_2.png) [@S1ructure](https://forum.ansible.com/u/S1ructure)\
**Post date:** [November 3, 2024, 12:16pm UTC](https://forum.ansible.com/t/using-variables-in-changed-when-failed-when-directives/10652/1 "2024-11-03T12:16:57Z")

</div>

Hello lovley ansible community,

i have a question regarding the usage of variables in changed\_when/failed\_when directives due to the warning `[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}`

I am aware of:

- [Error handling in playbooks — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_error_handling.html)
- [ansible - [WARNING]: when statements should not include jinja2 templating delimiters - Stack Overflow](https://stackoverflow.com/questions/42673045/warning-when-statements-should-not-include-jinja2-templating-delimiters)

All the examples refer to variables introduced via the `register` directive.

I am using the following playbook for creating a serviceaccount via SSH, to use later on the k8s module itself:

The playbook looks like this:

```auto
- name: K8s
  hosts: k8s_executor
  vars:
    serviceaccountuser: ansible-user

  tasks:

    - name: Create service account on k8s
      ansible.builtin.shell: kubectl --kubeconfig=$HOME/.kube/config create serviceaccount {{ serviceaccountuser }} --namespace kube-system
      register: tmp
      changed_when:
        - tmp.rc == 0
        - "tmp.stdout == \"serviceaccount/{{ serviceaccountuser }} created\""
      failed_when:
        - tmp.rc > 0
        - "'already exists' not in tmp.stderr"
  ....

```

As u can see i want to avoid retyping `ansible-user` everywhere, why i have introduced the variable.

Q: Is there the possibility to reference those `vars` in the changed\_when or failed\_when directives?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [November 3, 2024, 3:40pm UTC](https://forum.ansible.com/t/using-variables-in-changed-when-failed-when-directives/10652/2 "2024-11-03T15:40:49Z")

</div>

> [@S1ructure](#):
>
> All the examples refer to variables introduced via the `register` directive.

All variables are referenced the same way, so that’s irrelevant.

> [@S1ructure](#):
>
> `- "tmp.stdout == \"serviceaccount/{{ serviceaccountuser }} created\""`

Conditional statements should not include Jinja delimiters, because it’s already a Jinja context. Just reference variables by name.

```auto
- tmp.stdout == "serviceaccount/" ~ serviceaccountuser ~ "created"

```

---

<div class="post-metadata">

**Author:** ![S1ructure](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/s1ructure/32/5335_2.png) [@S1ructure](https://forum.ansible.com/u/S1ructure)\
**Post date:** [November 3, 2024, 4:54pm UTC](https://forum.ansible.com/t/using-variables-in-changed-when-failed-when-directives/10652/3 "2024-11-03T16:54:04Z")

</div>

amazing - that was the missing link.

thanks a lot.

Guess ill raise a PR to add this to the docs. Somehow missing tbh 😃

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 3, 2024, 4:54pm UTC](https://forum.ansible.com/t/using-variables-in-changed-when-failed-when-directives/10652/4 "2024-12-03T16:54:42Z")

</div>

Closed on December 3, 2024, 4:54pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
