working with multiple lists.. one defining the other?

31 views
Skip to first unread message

Joel Parker

unread,
Feb 20, 2015, 9:05:59 PM2/20/15
to ansible...@googlegroups.com
I already have tasks for creating users, groups, authorized_keys, etc. from the existing lists of users, by setting a variable that's linked to each of those.

What I want to do is have group_vars for an inventory that lists which groups I want added to servers. And then another inventory might have other group_vars where the groups are different. I want to define all users and what groups they're in in users.yml, and then simply specify which groups get different access to servers in my inventory by creating my user_groups list in a group_var.

in group_vars/users.yml:

devs:
  - "{{ ronda }}"
  - "{{ abe }}"
  - "{{ kathy }}"

sysadmins:
  - "{{ york }}"
  - "{{ kathy }}"
  - "{{ george }}"

managers:
  - "{{ brown }}"
  - "{{ lee }}"

ronda:
  - { name: "ronda", fullname: "Ronda", uid: "1001", gid: "1001", state: "present", remove: "no", ssh_key: "../ssh_keys/ronda.pub" }
etc..


in inventory/group_vars/single-environment.yml:

user_groups:
  - devs
  - sysadmins
  - managers

There could be another file in inventory/group_vars/second-environment.yml that defines user_groups differently:

user_groups:
  - sysadmins



Is this possible, at all? I feel like I should be able to reference this doing something like user_groups[item.name], user_groups[item.fullname], user_groups[uid], etc. I know that method isn't possible, but, there's gotta' be a way?

Serge van Ginderachter

unread,
Feb 21, 2015, 9:11:13 AM2/21/15
to ansible...@googlegroups.com
I'm actually using this very way of defining users and groups myself.

What you want to do is possible if you flatten the lists in user_groups using the flattened lookup.

Until now, you could only do that in a with_flattened lookup construct, but thanks to Brian's recent patch, you can now also do it (in latet devel) in a templating lookup():

https://github.com/ansible/ansible/pull/10298

  Serge


--
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/9ca01126-d634-4089-97d6-875f101739a9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joel Parker

unread,
Feb 22, 2015, 2:49:35 PM2/22/15
to ansible...@googlegroups.com
Thank you for the response. Can you provide a quick example of how you use the flattening lookup to accomplish this.. particularly showing the structure of the data you're using?

Serge van Ginderachter

unread,
Feb 23, 2015, 3:34:21 AM2/23/15
to ansible...@googlegroups.com
Given your example, pretty simple:

   with_flattened: user_groups

But that only works to feed a task. The new feature I referred to, is brand new, I didn't use it yet, but that would roughly translate to

  {{ lookup('flattened', user_groups, want_list=True) }}



On 22 February 2015 at 20:49, Joel Parker <root...@gmail.com> wrote:
Thank you for the response. Can you provide a quick example of how you use the flattening lookup to accomplish this.. particularly showing the structure of the data you're using?
--
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.
Reply all
Reply to author
Forward
0 new messages