# Trying to recreate an if statement inside a for loop with ansible

**URL:** <https://forum.ansible.com/t/trying-to-recreate-an-if-statement-inside-a-for-loop-with-ansible/21773>\
**Category:** Ansible Project\
**Created:** [June 14, 2016, 3:15pm UTC](https://forum.ansible.com/t/trying-to-recreate-an-if-statement-inside-a-for-loop-with-ansible/21773 "2016-06-14T15:15:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Shelton](https://avatars.discourse-cdn.com/v4/letter/c/7bcc69/32.png) [@Chris\_Shelton](https://forum.ansible.com/u/Chris_Shelton)\
**Post date:** [June 14, 2016, 3:15pm UTC](https://forum.ansible.com/t/trying-to-recreate-an-if-statement-inside-a-for-loop-with-ansible/21773/1 "2016-06-14T15:15:22Z")

</div>

So I have an on-line form in ansible tower that lets people pick a group to included in. I’m trying to set it up so they can pick multiple groups, but I’m having a tough time writing the logical checks I need.

In any other programming language I could load the array and write a for loop to go through every element and if one of them doesn’t exist, rewrite a binary variable that would be used in further logical checks. Something like this:

groups = [group1, group2, group3, …]  
does\_not\_exist\_check\_variable = 0

for i = 1:length(groups)

if group(i) does not exist

does\_not\_exist\_check\_variable = 1

end if

end

In ansible, the closest thing I can find to a for loop is with\_item which in all of the text I’ve read so far implies that the command is to be used to supplement certain items into a configuration one after another. I’ve attempted to recreate my for loop and if statements like this:

- name: Create dne\_check variable

set\_fact: dne\_check = 0

- name: Get groups names

shell: item.value.access.find(‘{{ group\_names }}’)

register: all\_group\_names

- name: Check for blank group names

set\_fact: dne\_check = 1

with\_items:" {{all\_group\_names}}"

when: item == -1

But it complains about my syntax at the last when statement. Any help on the matter would be greatly appreciated.
