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

21 views
Skip to first unread message

Chris Shelton

unread,
Jun 14, 2016, 11:26:54 AM6/14/16
to Ansible Project
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. 

Reply all
Reply to author
Forward
0 new messages