another alternative if you don't want to use an external plugin is to 
predefine your variable names and then combine them..

ie...

> users_global
>
users_group 

users_host (variable you set in host_vars)


and then use the "combine 
<https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#combining-hashes-dictionaries>"
 
filter to combine them together . 
ie..

> users_global | combine(users_group, recursive=True) | combine(users_host, 
> recursive=True)



I agree though, I am also coming from puppet and miss hiera.   I may still 
consider going back to using it as there appears to be support 
<https://docs.ansible.com/ansible/latest/plugins/lookup/hiera.html> for it, 
but it would be nice to be fully integrated

The strategy I ended up going with for the similar problem you have is
1. I created a "users" hash in vars/ which contains all users that can be 
managed
2. then in my host & group_vars I populate a list such as 
"linux_users_global" or linux_users_unique_name which contains the list of 
users that should be managed either globally, group or at the host level
3. then in my playbook I merge all the variables that match the pattern 
"linux_users.*" . (see this link 
<https://docs.ansible.com/ansible/devel/plugins/lookup/varnames.html>)
4. then I loop through that merged list and manage those users

for me, the above solution works quite well.


On Thursday, March 21, 2019 at 12:07:09 PM UTC-7, Suhail Choudhury wrote:
>
> Hi. 
>
> Thanks for reply Kai. 
>
> Default Ansible cannot solve this problem, which I find very odd as it 
> was a common issue that is very elegantly addressed in Pupept using 
> Hiera. 
>
> However.... a solution has been found! 
>
> Have a look at: 
>
>
> http://leapfrogonline.io/articles/2017-02-22-explicit-merging-of-ansible-variables/
>  
> https://github.com/leapfrogonline/ansible-merge-vars 
>
> They have solved exactly this issue and I'm testing their plugin and 
> so far so good. Certainly not as elegant as Puppet/Hiera but it's a 
> working solution. 
>
> Regards, 
> Suhail. 
>
> On Thu, 21 Mar 2019 at 18:49, Kai Stian Olstad 
> <[email protected] <javascript:>> wrote: 
> > 
> > On 21.03.2019 12:58, Suhail Choudhury wrote: 
> > > Thanks for your earlier reply Adam. 
> > > 
> > > So how can I combine variables for a list of "users" defined like so: 
> > > 
> > > *group_vars/all* 
> > > 
> > > users: 
> > >    - name: alice 
> > >      comment: Alice 
> > > 
> > > *group_vars/group1* 
> > > 
> > > users: 
> > >    - name: bob 
> > >      comment: Bob 
> > > 
> > > *host_vars/host1* 
> > > 
> > > users: 
> > >    - name: charlie 
> > >      comment: Charlie 
> > > 
> > > How can all these variables be combined using "loop" or "with_items"? 
> > 
> > You can't, they will be overwritten in the order listed here 
> > 
> https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable
>  
> > 
> > > What if there are duplicates? 
> > 
> > They are duplicates since all are call users and will be overwritten. 
> > 
> > 
> > > Can the "host_vars/host1" variables override and take precedence over 
> the 
> > > "group_vars/all" variables? 
> > 
> > It does. 
> > 
> > 
> > -- 
> > 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] <javascript:>. 
> > To post to this group, send email to [email protected] 
> <javascript:>. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/86213972-a12c-474a-0284-747eaa825cdd%40olstad.com.
>  
>
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
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/b03d7f87-aef1-4d96-a1cd-77285441af13%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to