# Dynamic list of items created on remote side

**URL:** <https://forum.ansible.com/t/dynamic-list-of-items-created-on-remote-side/34439>\
**Category:** Ansible Project\
**Created:** [January 14, 2021, 6:50am UTC](https://forum.ansible.com/t/dynamic-list-of-items-created-on-remote-side/34439 "2021-01-14T06:50:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Go\_Away](https://avatars.discourse-cdn.com/v4/letter/g/bc79bd/32.png) [@Go\_Away](https://forum.ansible.com/u/Go_Away)\
**Post date:** [January 14, 2021, 6:50am UTC](https://forum.ansible.com/t/dynamic-list-of-items-created-on-remote-side/34439/1 "2021-01-14T06:50:30Z")

</div>

And again I have a non-typical use case.

How would you approach such task? I have some servers on which there is a piece of software running that's not configured by me and thus I can't control the configuration with ansible. But I need to be able to make sure that for every entry in its configuration I make sure that something is done on the host.

For example (this is not this case; just for explanation what I mean) - someone else is responsible for creating users on the machine (or the users are created remotely and authenticated and authorized using pam\_ldap and nss\_ldap for example) and I can list them with gentent. I need to make sure that for each user I have a directory created on a host.

That would involve running a command on the remote host side (in our case - getent) to get an output, and then iterating over this output to make sure the directories are created.

Of course I could just do a remote command and do a script to be run on the remote side but it kinda defeats the purpose of having ansible in the first place.

Any better ideas?

Hmm... maybe doing a remote command, registering output and then looping a task over it? I'm not sure though how to split the result that I register in a variable into single lines.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 14, 2021, 6:58am UTC](https://forum.ansible.com/t/dynamic-list-of-items-created-on-remote-side/34439/2 "2021-01-14T06:58:09Z")

</div>

Ansible (and related software) helps you to ensure a system status that is defined by the tasks you run for that host,  
so ideally it works also when the installation and configuration isn't performed by Ansible.

I think it is a good idea to use Ansible modules whenever possible, and only fallback to commmand and shell (use that  
only at last resort) if not.

But it would be good if you describe concrete case we can help you with.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Go\_Away](https://avatars.discourse-cdn.com/v4/letter/g/bc79bd/32.png) [@Go\_Away](https://forum.ansible.com/u/Go_Away)\
**Post date:** [January 14, 2021, 3:28pm UTC](https://forum.ansible.com/t/dynamic-list-of-items-created-on-remote-side/34439/3 "2021-01-14T15:28:43Z")

</div>

Sure, I understand. Sometimes the business however begs to differ 😉  
In this case it’s just that some part of the solution (the OS-level) is managed by me, and higher up on this the customer builds his own solution. I can’t force him to do his work this way or another.  
Of course I can write a script that will manually connect to each server and checks something - and based on this does something else but using Ansible saves me a lot of hassle, mostly in the “connecting” part (sometimes I have to use a jumphost, sometimes I have to log in using one set of credentials, sometimes - another, and so on).  
So yes, I understand that I’m trying to use Ansible here a bit not for what it is meant for but hey, if it works, then why not 😉  
And more to the point, after some digging I believe that if I do a command and register output in a variable (let’s say it’s called output\_var), I can then loop another variable over output\_var.stdout\_lines. I still need to test it but it seems that it’s pretty much what I want.  
For example (don’t get attached to what it does; it’s just what I need in terms of interaction between those two actions):

- name: getusers  
shell: “getent passwd | grep /home/remote | cut -d : -f 1”  
register: remoteusers

- name: touchy  
file:

path: ‘/tmp/{{item}}’  
state: touch  
owner: ‘{{item}}’

loop: {{remoteusers.stdout\_lines}}

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 14, 2021, 4:43pm UTC](https://forum.ansible.com/t/dynamic-list-of-items-created-on-remote-side/34439/4 "2021-01-14T16:43:09Z")

</div>

> Sure, I understand. Sometimes the business however begs to differ 😉  
> In this case it's just that some part of the solution (the OS-level) is managed by me, and higher up on this the  
> customer builds his own solution. I can't force him to do his work this way or another.  
> Of course I can write a script that will manually connect to each server and checks something - and based on this does  
> something else but using Ansible saves me a lot of hassle, mostly in the "connecting" part (sometimes I have to use a  
> jumphost, sometimes I have to log in using one set of credentials, sometimes - another, and so on).  
> So yes, I understand that I'm trying to use Ansible here a bit not for what it is meant for but hey, if it works, then  
> why not 😉  
> And more to the point, after some digging I believe that if I do a command and register output in a variable (let's say  
> it's called output\_var), I can then loop another variable over output\_var.stdout\_lines. I still need to test it but it  
> seems that it's pretty much what I want.  
> For example (don't get attached to what it does; it's just what I need in terms of interaction between those two actions):
> 
> ```
> \- name: getusers
> shell: &quot;getent passwd | grep /home/remote | cut \-d : \-f 1&quot;
> register: remoteusers
> 
> \- name: touchy
> file:
> path: &#39;/tmp/\{\{item\}\}&#39;
> state: touch
> owner: &#39;\{\{item\}\}&#39;
> loop: \{\{remoteusers\.stdout\_lines\}\}
> 
> ```

Use getent module instead of the shell (remember that's the last resort):

&nbsp;&nbsp;&nbsp;&nbsp;- name: Retrieve user information  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getent:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database: passwd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;split: ':'

The logic used by this module for its result is a bit strange compared to others.  
It stores the output in the variable "getent\_passwd" (when using "passwd" database).

getent\_passwd is a dictionary, sample entry looks like:

&nbsp;&nbsp;sympa:  
&nbsp;&nbsp;&nbsp;&nbsp;- x  
&nbsp;&nbsp;&nbsp;&nbsp;- '1001'  
&nbsp;&nbsp;&nbsp;&nbsp;- '1001'  
&nbsp;&nbsp;&nbsp;&nbsp;- ''  
&nbsp;&nbsp;&nbsp;&nbsp;- /home/sympa  
&nbsp;&nbsp;&nbsp;&nbsp;- /bin/bash

The home directory at position 5, so you can retrieve it with "{{ getent\_passwd['sympa'][4] }}".

In order to convert the dict "getent\_passwd" to a list which only contains home directories matching /home/remote as  
grep does:

&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;users: "{{ users | default() + [item.key] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;when: item.value[4] is search('/home/remote')  
&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: "{{ getent\_passwd }}"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Go\_Away](https://avatars.discourse-cdn.com/v4/letter/g/bc79bd/32.png) [@Go\_Away](https://forum.ansible.com/u/Go_Away)\
**Post date:** [January 14, 2021, 5:02pm UTC](https://forum.ansible.com/t/dynamic-list-of-items-created-on-remote-side/34439/5 "2021-01-14T17:02:37Z")

</div>

Good to know about the getent module. Thx.

Still it's not about this module as such. As I wrote, it's not about what the example does, but how one action relies on the output from another. But it seems that stdout\_lines should be what I need. For some reason I somehow expected ansible to be able only to ingest an output from a command as a whole, not line by line.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 15, 2021, 6:31am UTC](https://forum.ansible.com/t/dynamic-list-of-items-created-on-remote-side/34439/6 "2021-01-15T06:31:36Z")

</div>

> > For example (don't get attached to what it does; it's just what I need in terms of interaction between those two  
> > actions):
> > 
> > > ```
> > > \- name: getusers
> > > shell: &quot;getent passwd | grep /home/remote | cut \-d : \-f 1&quot;
> > > register: remoteusers
> > > 
> > > \- name: touchy
> > > file:
> > > path: &#39;/tmp/\{\{item\}\}&#39;
> > > state: touch
> > > owner: &#39;\{\{item\}\}&#39;
> > > loop: \{\{remoteusers\.stdout\_lines\}\}
> > > 
> > > ```
> > 
> > Use getent module instead of the shell (remember that's the last resort):
> > 
> > ```
> > \- name: Retrieve user information
> > getent:
> > database: passwd
> > split: &#39;:&#39;
> > 
> > ```
> 
> Good to know about the getent module. Thx.
> 
> Still it's not about this module as such.

> As I wrote, it's not about what the example does, but how one action relies on

Yes, I understood that but I think it is a nice example for using getent and transforming information  
regardless :-).

> the output from another. But it seems that stdout\_lines should be what I need. For some reason I somehow expected  
> ansible to be able only to ingest an output from a command as a whole, not line by line.

It does, check out stdout instead of stdout\_lines.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
