OK, it's working now.  Thanks!

  become: true
  become_user: root
  tasks:
    - block:
      #- debug:
      #    msg: "riga {{item}}"
      - include_tasks: user.yml
        when: item | search(":")
        with_items: [ "abc:/home/def", "ghi:/home/jkh"]

But as noted by floweryoursong...

Il giorno lunedì 26 febbraio 2018 15:38:58 UTC, Kai Stian Olstad ha scritto:
>
> On Monday, 26 February 2018 15.39.09 CET Nico Sabbi wrote: 
> > HI, 
> > can I combine when: and with_items clauses as in this example? 
> > 
> >   become: true 
> >   become_user: root 
> >   tasks: 
> >     - block: 
> >       - debug: 
> >           msg: "riga {{item}}" 
> >       - user: 
> >           name: "{{ item.split(':')[0] }}" 
> >           home: "{{ item.split(':')[1] }}" 
> >         when: item search(":") 
> >         with_items: [ "abc:def", "yyy.zzz"] 
> > 
> > The sad output is the following, indicating that "item" is undefined  in 
> > the loop. 
>
> when is executing before the loop so that's the reason this will not work. 
>
>
> > TASK [debug] 
> > 
> ***********************************************************************************************************************************************************************************************
>  
>
> > fatal: [localhost]: FAILED! => {"msg": "The task includes an option with 
> an 
> > undefined variable. The error was: 'item' is undefined\n\nThe error 
> appears 
> > to have been in '/home/nico2601/block.yml': line 25, column 9, but 
> may\nbe 
> > elsewhere in the file depending on the exact syntax problem.\n\nThe 
> > offending line appears to be:\n\n    - block:\n      - debug:\n        ^ 
> > here\n\nexception type: <class 
> > 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'item' is 
> undefined"} 
> > to retry, use: --limit @/home/nico2601/block.retry 
> > 
> > 
> > What I want to do is ensuring that the user before the : is set only 
> when 
> > the string that I'm looping over does contain : 
> > What's the right way to do it? 
>
> If you but the user code in a file and use include_tasks it will work. 
>
> user.yml 
> --- 
> - user: 
>     name: "{{ item.split(':')[0] }}" 
>     home: "{{ item.split(':')[1] }}" 
>
> Then replace the user in block with include_tasks 
>
> - include_tasks: user.yml 
>   with_items: [ "abc:def", "yyy.zzz"] 
>
>
> -- 
> Kai Stian Olstad 
>

-- 
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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/dcce0213-b0ee-4043-9201-89690e238f85%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to