How to iterate over register variables?

86 views
Skip to first unread message

J Dav

unread,
Apr 11, 2014, 5:10:42 PM4/11/14
to ansible...@googlegroups.com
I am trying to create an ansible play that 

  1. Establishes if a user exists  on a host and records the stdout as a register variable. Referencing a dict file which contains what users should be on the host.
  2. Creates a group for the user which will be created (with data from the dict) if the stdout == 0
  3. Creates a user account (with data from the dict) if the stdout == 0
  4. Copies over a sudoers.d template for a pre-existing ops group
And I saw at https://groups.google.com/forum/#!topic/ansible-project/htHzfr5HzQE that the register is turn into a list when combine in with_items. I can't seem to figure out how to iterate over a register variable list. Essentially, I'd like for my OpsTeam.yaml variables to correlate with the register variables and use that to determine if action should be taken. Note: this play currently does not work

Here is my code:

---
 - name: Deploy Users and Keys
   hosts: tag_AddUsers_
   sudo: yes
   vars_files:
    - "OpsTeam.yaml"
   tasks:
    - name: establish if user exists
      shell: /usr/bin/getent passwd {{ item.name }} | /usr/bin/wc -l | tr -d ' '
      register: user_exists
      with_items: users

    - name: create user group if doesnt already exist
      command: /usr/sbin/groupadd {{ item.name }}
      when: user_exists.stdout == 0
      with_together:
       - users
       - user_exists

    - name: Add in Ops sudoers.d file
      template: src=./ops dest=/etc/sudoers.d/ops

    - name: add users
      user: name={{ item.name }} createhome=yes group={{ item.name }} groups=ops append=no state=present shell=/bin/bash
      when: user_exists.stdout == 0
      with_together:
       - users
       - user_exists




Thanks in advance

Serge van Ginderachter

unread,
Apr 14, 2014, 9:42:18 AM4/14/14
to ansible...@googlegroups.com

On 11 April 2014 23:10, J Dav <defio...@gmail.com> wrote:
with_together:
       - users
       - user_exists

​The list is within users_exists.results​

Reply all
Reply to author
Forward
0 new messages