Hello,

I'm trying to create new users but I've encountered a problem with the when 
statement which doesn't allows me to move forward.

users.yml
  - fullname: Full Name Here1
    username: sshusernamehere1
    userkey: ../files/ssh-keys/sshusernamehere1/authorized_keys
    access:
      state: present
      groups:
        - wheel
        - sftpusers
      hosts:
        - all-management-servers
        - all-jenkins-servers

  - fullname: Full Name Here2
    username: sshusernamehere2
    userkey: ../files/ssh-keys/sshusernamehere2/authorized_keys
    access:
      state: present
      groups:
        - agroup
        - sftpusers
      hosts:
        - all-servers
        - all-web-servers


main.yml
- name: USERS | SSH USERS | Checking user(s) presence within the group...
  user: name="{{ item.username }}" groups="{{ item.access.groups }}" 
append=yes createhome=yes state="{{ item.access.state }}"
  when:
    - item.access.state == "present"
    - item.access.hosts is defined
    - inventory_hostname in groups['item.access.hosts']
  with_items: "{{ users }}"

- name: USERS | SSH USERS | Deploying SSH Key(s) for our user(s)...
  authorized_key: user="{{ item.username }}" state="{{ item.access.state 
}}" key="{{ lookup('file', item.userkey)}}"
  when:
    - item.access.state == "present"
    - item.access.hosts is defined
    - inventory_hostname in groups['item.access.hosts']
  with_items: "{{ users }}"

What's wrong with this or how may I change my main.yml code in order to 
deploy the users only if that inventory_hostname is part of the hosts: 
group (which is defined on ansible hosts file).

Thanks you!

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d86c44a1-3163-4ebd-84d7-34ee524d08ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to