# register variable with\_items and failed\_when

**URL:** <https://forum.ansible.com/t/register-variable-with-items-and-failed-when/34624>\
**Category:** Ansible Developer\
**Created:** [February 16, 2021, 7:24am UTC](https://forum.ansible.com/t/register-variable-with-items-and-failed-when/34624 "2021-02-16T07:24:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mukuntha\_rajaa](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@Mukuntha\_rajaa](https://forum.ansible.com/u/Mukuntha_rajaa)\
**Post date:** [February 16, 2021, 7:24am UTC](https://forum.ansible.com/t/register-variable-with-items-and-failed-when/34624/1 "2021-02-16T07:24:35Z")

</div>

Hello,

I am using custom module and looping through items to do “kubectl apply -f”.

- name: create rbac objects  
kubectl:  
manifest: “{{ item }}”  
state: present  
with\_fileglob: “{{ CAAS.rbac\_manifests\_directory }}/\*”  
register: rbacresult  
failed\_when: ‘“v1beta1 is deprecated” not in rbacresult.results[0].stdout’  
tags: rbac

Here it fails with,

fatal: [localhost]: FAILED! =\> {“msg”: “The conditional check ‘v1beta1 not in rbacresult.results[0].stdout’ failed. The error was: error while evaluating conditional (v1beta1 not in rbacresult.results[0].stdout): Unable to look up a name or access an attribute in template string ({% if v1beta1 not in rbacresult.results[0].stdout %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like ‘-’: argument of type ‘AnsibleUndefined’ is not iterable”}

It would be nice, if I could loop through complete result variable and then fail only if “not of my specific-condition”.

---

<div class="post-metadata">

**Author:** ![Andreas\_Harpfer](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andreas\_Harpfer](https://forum.ansible.com/u/Andreas_Harpfer)\
**Post date:** [February 16, 2021, 2:53pm UTC](https://forum.ansible.com/t/register-variable-with-items-and-failed-when/34624/2 "2021-02-16T14:53:34Z")

</div>

At the level of a single item the `results` array for all items  
is not yet available. The whole array will only be available  
after the task has finished looping over all items. So you will  
have to check for `… not in rbacresult.stdout` in the above case.

This minimal example should illustrate the effect:

```auto
- hosts: localhost
  tasks:
    - name: Dummy
      shell:
        cmd: echo {{ item }}
      with_items:
        - "positive"
        - "negative"
      register: result
      failed_when: '"positive" not in result.stdout'
      ignore_errors: yes

    - name: Debug
      debug:
        var: result

```

-Andi
