Unable to figure out arrays

65 views
Skip to first unread message

Michael Bushey

unread,
Oct 9, 2014, 7:36:25 PM10/9/14
to ansible...@googlegroups.com
I would like to be able to put my users into a list and be able to select what user has access to each machine.

---
- name: Set up users
  hosts: localhost
  gather_facts: no


  vars:
    - alice:
        name: Alice Appleworth
        mail: al...@appleworth.com
        pass: ..hash...
    - bob:
        name: Bob Bananarama
        mail: b...@bananarama.com
        pass: ..hash..
        mail:

  tasks:
    - name: Add user
      debug: msg="Add user {{ item.key }}  password {{ item.pass }}"
      with_items:
        - "{{ alice }}"


I am not able to figure out how to get the user name, ie "alice" as item.key is not valid. I think this is because using "{{ alice }}" does not preserve the name. Does anyone know how I can structure this? I've tried putting them all under users, but with_items: users.alice does not work.

Thanks in advance for any help. The docs do not seem to cover this and I've Googled Ansible arrays and dicts to the point where I'm not finding any new pages.


Michael Bushey

unread,
Oct 9, 2014, 7:56:01 PM10/9/14
to ansible...@googlegroups.com
Please ignore the second mail: under bob

mvermaes

unread,
Oct 10, 2014, 2:20:11 AM10/10/14
to ansible...@googlegroups.com
Hi Michael, I think you want something like:

---
- name: Set up users
  hosts: localhost
  gather_facts: no

  vars:
    users:
      alice:
        name: Alice Appleworth
        mail: al...@appleworth.com
        pass: ..hash...
      bob:
        name: Bob Bananarama
        mail: b...@bananarama.com
        pass: ..hash..

  tasks:
    - name: Add user
      debug: msg="Add user {{ item.key }}  password {{ item.value.pass }}"
      with_dict: users


Here's the section of the docs you want - http://docs.ansible.com/playbooks_loops.html#looping-over-hashes

Michael Bushey

unread,
Oct 10, 2014, 2:59:25 AM10/10/14
to ansible...@googlegroups.com
Thanks for your response Michael. I've already read through that page
a few times. It does not work for me because I need an array of users,
and then I need to be able to select which user have access to each
class of server. This method would give every user access to every
machine. I need to be able to specify something like with items -
users.alice - users.bob and not have carol included.
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/-TzTFS57j7A/unsubscribe.
> To unsubscribe from this group and all its topics, 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/90226003-8d97-4e92-8bb5-c8ec56b3717c%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Oct 10, 2014, 8:36:23 AM10/10/14
to ansible...@googlegroups.com
for that just do :

vars:
....
allowed_users_host1: ['alice,'bob']
....
- name: Add user
debug: msg="Add user {{ item.key }} password {{ item.value.pass }}"
with_dict: users
when: item.key in allowed_users_host1
> 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/CAPJTyAX%3DA%2Bv8YkX42UNvpJGmjr%2B9QCLy1BDUdr1nypcEriyErQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Adam Heath

unread,
Oct 10, 2014, 12:36:05 PM10/10/14
to ansible...@googlegroups.com
- name: foo
shell: echo "{{ users[item].name }}"
with_items:
- alice
- bob

Michael Bushey

unread,
Oct 10, 2014, 2:51:50 PM10/10/14
to ansible-project
Awesome! Thank you so much for your responses Michael, Brian, and
Adam. Both solutions from Brian and Adam work, thank you! If any of
you get to Los Angeles I owe you a drink!

This is working great:

- name: Users | create .gitconfig
shell: git config --global user.name '{{ item.value.name }}'; git
config --global user.email '{{ item.value.mail }}'
args:
creates: /home/{{ item.key }}/.gitconfig
sudo: yes
sudo_user: "{{ item.key }}"
with_dict: users
when: item.key in allowed_users_dev
tags: git

What I like about the with_dict approach is I can do "when: item.key
not in allowed_users_dev" with "user: name={{ item.key }}
state=absent".
> https://groups.google.com/d/msgid/ansible-project/54380AE6.3070902%40brainfood.com.
Reply all
Reply to author
Forward
0 new messages