register variable with_items and failed_when

955 views
Skip to first unread message

Mukuntha rajaa

unread,
Feb 16, 2021, 2:24:35 AM2/16/21
to Ansible Development
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".




Andreas Härpfer

unread,
Feb 16, 2021, 9:53:41 AM2/16/21
to Ansible Development
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:

```
- 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

Reply all
Reply to author
Forward
0 new messages