how to execute a task/action just when a list of items really exist

387 views
Skip to first unread message

Esteban Freire

unread,
May 29, 2015, 10:10:23 AM5/29/15
to ansible...@googlegroups.com
Hello all,

I am using ansible for automate the users creation in one of our tools so I have the following users file in vars with the user's info and also, the user can have some secondary groups assigned besides the main group:

[root@admin oort]# cat roles/ON4_project_managment/vars/users.yml
---
users_account:
  user username:
    status: new
    login_name: testuser
    main_group_name: natalie-alpha
    secondary_groups:
    type_authentication: core
    mail: 'Email="test1mail"'
    telf: 'Telf="6123456789"'
    role: 'role="admin"'
    user_info: 'user_info="researcher"'
    start_date: 'start_date="2015/12/13"'
    end_date: 'end_date="2016/12/13"'
    network:
    acl:
  user2 username2:
    status: new
    login_name: testuser2
    main_group_name: natalie-alpha
    secondary_groups:
      - group1
      - oneadmin
      - SGE
    type_authentication: core
    mail: 'Email="test2@mail"'
    telf: 'Telf="465465465"'
    role: 'role="admin"'
    user_info: 'user_info="researcher"'
    start_date: 'start_date="2015/11/13"'
    end_date: 'end_date="2016/11/13"'
    network:
    acl:

Then, in the playbook I read the list and also the secondary groups to assign the secondary groups to the user. My problem is that it can happen that the user does not belong to any secondary group like in the users.yml example file from above so right now, if this happens, I get this error:

TASK: [ON4_project_managment | Assign the secondary groups for the user] ******
fatal: [m-opennebula4] => the key secondary_groups should point to a list, got 'None'

But I would like that the task is only executed when the user has some secondary group assigned.

This is the task (right now it it is just simplefied with "echo" but it is the same approach:

- name: Assign the secondary groups for the user
  sudo: True
  sudo_user: oneadmin
  shell: echo {{ item.0.login_name }};echo {{ item.1 }}
  with_subelements:
     - users_account
     - secondary_groups
  when: item.1 is defined
  tags:
    - Assign_secondary_group
    - esteban

I have been playing with != none or !="" or is defined but I always get the error :( .. Probably, I am not understanding it correctly and I am sure some of you already needed to face this issue, so I would appreciate your help :)

I hope I have explained myself correctly because I am a bit tired right now :/

Thanks in advance,
Esteban

Philippe Eveque

unread,
May 30, 2015, 4:37:18 AM5/30/15
to ansible...@googlegroups.com
HI

you need an empty list
something along the following should help:

secondary_groups: []

Phil


Esteban Freire

unread,
May 30, 2015, 9:51:57 AM5/30/15
to ansible...@googlegroups.com
Hi Phil,

Thank you very much for your answer :)

Unfortunatelly, I cannot try it until Monday but I will let you know if it works. Also, I understand that I must set up secondary_groups: [] in the user.yml file situated on the vars directory when an user does not have secondary groups assigned, right? or am I understand it wrong?

Thanks in advance,
Esteban

Philippe Eveque

unread,
May 30, 2015, 9:58:35 AM5/30/15
to ansible...@googlegroups.com
I meant, in your case with a declaration like
secondary_groups:

it is defined and is none
so  one possibility is to check against none

another possibility with
secondary_groups: []

you can check against empty list

HTH

Phil

Philippe Eveque

unread,
May 30, 2015, 9:59:31 AM5/30/15
to ansible...@googlegroups.com
yep

Phil

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f0d87c7d-ade4-42ec-9e9f-5f1eb6966eb7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Esteban Freire

unread,
May 30, 2015, 10:44:33 AM5/30/15
to ansible...@googlegroups.com
Hi Phil,

Thank you very much for your help!

On Monday, I will try to declare secondary_groups: [] and I will also try put this conditional when: item.1 != ""  because I only want that the action/command is just executed when secondary_groups really contains some secondary groups to assign to the users.

I will let you know by Monday :)

Thanks in advance,
Esteban

Esteban Freire

unread,
Jun 1, 2015, 8:33:56 AM6/1/15
to ansible...@googlegroups.com
Hi Phil,

I tried what you said, I declared secondary groups variable as  secondary_groups: [] in the users.yml file at the vars directory and then, on the main file for the task, I just defined when: (item.1 != "") and indeed, action is only executed when the secondary groups were assigned for the user and if the user does not have any secondary group assigned it does not do anything, what it is expected.

Thank you very much for your help :)

Cheers,
Esteban

Brian Coca

unread,
Jun 1, 2015, 11:54:42 AM6/1/15
to ansible...@googlegroups.com
actually, you won't be hitting that when condition, when with_ detects
an empty list it just skips the task. the when: is redundant and would
actually fail as there is no item.1 defined.



--
Brian Coca

Esteban Freire

unread,
Jun 2, 2015, 3:15:30 AM6/2/15
to ansible...@googlegroups.com
Hi Brian,

Thank you very much for letting me know :)

Cheers,
Esteban
Reply all
Reply to author
Forward
0 new messages