On Tuesday, January 7, 2014 1:38:06 AM UTC-8, hafizh wrote:
>
>
> I notice that the tasks are very 'strict'. Once we provide an optional 
> param, the rest of the lists must provide it too. 
>
> Say. I want to create two users:
>     vars:
>       - users:
>         - { name: test1, uid: 1234 }
>         - { name: test2 }
>
> Ideal case would be:
>     tasks:
>         - name: creating user
>           user: name={{ item.name }} uid={{ item.uid }}
>           with_items: users
>
>     ie, I hope Ansible will be smart enough to ignore the optional 'uid' 
> for 'test2' but it wont. 
>
>
>
Actually, I think that you can do this, with a little creativity...

vars: 
  - users:
    - { name: test1, uid: 1234}
    - { name: test2}

tasks:
  -name: Create user
   debug: msg="user name={{item.name}} {% if item.uid is defined %} 
'uid='{{item.uid}}{% endif %}"
   with_items: users


And here was the output from my testing...
TASK: [debug msg="user name={{item.name}} {% if item.uid is defined %} 
'uid='{{item.uid}}{% endif %}"] *** 
ok: [10.246.72.170] => (item={'name': 'test1', 'uid': 1234}) => {
    "item": {
        "name": "test1", 
        "uid": 1234
    }, 
    "msg": "user name=test1  'uid='1234"
}
ok: [10.246.72.170] => (item={'name': 'test2'}) => {
    "item": {
        "name": "test2"
    }, 
    "msg": "user name=test2 "
}

Of course you would want to replace the
debug: msg="user name={{item.name}} {% if item.uid is defined %} 
'uid='{{item.uid}}{% endif %}"
with
user: name={{item.name}} {% if item.uid is defined %} 'uid='{{item.uid}}{% 
endif %}
but I think that that should work...  It is certainly worth a try... :-)

Adam



 

-- 
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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to