# with\_items... in groups\[item.hostgroups\]

**URL:** <https://forum.ansible.com/t/with-items-in-groups-item-hostgroups/24967>\
**Category:** Ansible Project\
**Created:** [July 12, 2017, 3:33pm UTC](https://forum.ansible.com/t/with-items-in-groups-item-hostgroups/24967 "2017-07-12T15:33:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Gogoasa](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Sebastian\_Gogoasa](https://forum.ansible.com/u/Sebastian_Gogoasa)\
**Post date:** [July 12, 2017, 3:33pm UTC](https://forum.ansible.com/t/with-items-in-groups-item-hostgroups/24967/1 "2017-07-12T15:33:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Larry\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/d26b3c/32.png) [@Larry\_Smith1](https://forum.ansible.com/u/Larry_Smith1)\
**Post date:** [July 14, 2017, 4:59am UTC](https://forum.ansible.com/t/with-items-in-groups-item-hostgroups/24967/2 "2017-07-14T04:59:35Z")

</div>

Can you post your error(s) you are getting? Also assuming that your with\_items: “{{ users }}” is defined in users.yml? Such as,

users.yml

```auto
users:
- 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

```
