# traversing directory structure

**URL:** <https://forum.ansible.com/t/traversing-directory-structure/31357>\
**Category:** Ansible Project\
**Created:** [September 24, 2019, 7:57pm UTC](https://forum.ansible.com/t/traversing-directory-structure/31357 "2019-09-24T19:57:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregory\_Edigarov](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gregory\_Edigarov](https://forum.ansible.com/u/Gregory_Edigarov)\
**Post date:** [September 24, 2019, 7:57pm UTC](https://forum.ansible.com/t/traversing-directory-structure/31357/1 "2019-09-24T19:57:35Z")

</div>

hello,

let’s look at the following structure:var/  
ssh/  
user1  
user2  
user3

hosts/  
host1/  
user1  
user3

host2/  
user2  
user3

under /var/ssh there are public keys for users

files under host1,host2 catalog are empty files just pointing ansible on which host which user must be added

i did the task by reading the hosts/ one level into an array, then looping over it  
like:

hosts: localhost  
tasks:

- name: get hosts from hosts directory  
command: ls -1 var/hosts  
register: ssh\_hosts

- name: set ssh\_hosts\_fact  
set\_fact:  
ssh\_hosts\_fact: “{{ ssh\_hosts.stdout\_lines }}”

- name: include subtasks  
include: ssh\_body.yml host=“{{ item }}”  
with\_items:

- “{{ ssh\_hosts\_fact }}”

then  
in included file:

- name: get usernames for host  
command: ls -1 var/hosts/{{ host }}  
register: usernames\_host

- name: add users onto host  
user:  
name: “{{ user\_item }}”  
shell: /bin/bash  
group: sudo  
with\_items:  
“{{ usernames\_host.stdout\_lines }}”  
delegate\_to: “{{ host }}”  
loop\_control:  
loop\_var: user\_item  
become: true

- name: add user keys  
authorized\_key:  
user: “{{ user\_item }}”  
key: “{{ lookup(‘file’, ‘var/ssh/{{ user\_item }}’ ) }}”  
with\_items:  
“{{ usernames\_host.stdout\_lines }}”  
delegate\_to: “{{ host }}”  
loop\_control:  
loop\_var: user\_item  
become: true

it works, but I think there is more effective way of doing thing, at least with no includes  
any suggestions?

thank you

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 24, 2019, 10:17pm UTC](https://forum.ansible.com/t/traversing-directory-structure/31357/2 "2019-09-24T22:17:00Z")

</div>

> hello,
> 
> let's look at the following structure:  
> var/  
> &nbsp;&nbsp;ssh/  
> &nbsp;&nbsp;&nbsp;user1  
> &nbsp;&nbsp;&nbsp;user2  
> &nbsp;&nbsp;&nbsp;user3
> 
> &nbsp;&nbsp;hosts/  
> &nbsp;&nbsp;&nbsp;host1/  
> &nbsp;&nbsp;&nbsp;&nbsp;user1  
> &nbsp;&nbsp;&nbsp;&nbsp;user3  
> &nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;host2/  
> &nbsp;&nbsp;&nbsp;user2  
> &nbsp;&nbsp;&nbsp;user3
> 
> under /var/ssh there are public keys for users
> 
> files under host1,host2 catalog are empty files just pointing ansible on  
> which host which user must be added

Short answer: Below is the playbook that you probably want.

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: get hosts from hosts directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: ls -1 var/hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: ssh\_hosts  
&nbsp;&nbsp;&nbsp;&nbsp;- name: create my\_group  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'my\_group'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ ssh\_hosts.stdout\_lines }}"

&nbsp;&nbsp;- hosts: my\_group  
&nbsp;&nbsp;&nbsp;&nbsp;become: true  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: get usernames  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "ls -1 var/hosts/{{ inventory\_hostname }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: usernames\_host  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: add users  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: /bin/bash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: sudo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ usernames\_host.stdout\_lines }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: add user keys  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;authorized\_key:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key: "{{ lookup('file', 'var/ssh/' ~ item ) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ usernames\_host.stdout\_lines }}"

Details:

Create a group "my\_group" instead of variable "ssh\_hosts\_fact"

&nbsp;&nbsp;&nbsp;&nbsp;- name: create my\_group  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'my\_group'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ ssh\_hosts.stdout\_lines }}"

> &nbsp;&nbsp;- name: set ssh\_hosts\_fact  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssh\_hosts\_fact: "{{ ssh\_hosts.stdout\_lines }}"

Start another play with the group "my\_group" instead of the "include". No  
delegation and loop\_control is needed.

&nbsp;&nbsp;- hosts: my\_group  
&nbsp;&nbsp;&nbsp;&nbsp;become: true  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:

> &nbsp;&nbsp;- name: include subtasks  
> &nbsp;&nbsp;&nbsp;&nbsp;include: ssh\_body.yml host="{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ ssh\_hosts\_fact }}"

Cheers,

&nbsp;&nbsp;-vlado
