On 1/14/21 4:28 PM, Go Away wrote:
> 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}}
>
Use getent module instead of the shell (remember that's the last resort):
- name: Retrieve user information
getent:
database: passwd
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:
sympa:
- x
- '1001'
- '1001'
- ''
- /home/sympa
- /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:
- set_fact:
users: "{{ users | default([]) + [item.key] }}"
when: item.value[4] is search('/home/remote')
with_dict: "{{ getent_passwd }}"
Regards
Racke
> --
> 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-proje...@googlegroups.com <mailto:
ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/ansible-project/14dc5938-8b92-4f89-ab10-c09dbcf2bd6cn%40googlegroups.com
> <
https://groups.google.com/d/msgid/ansible-project/14dc5938-8b92-4f89-ab10-c09dbcf2bd6cn%40googlegroups.com?utm_medium=email&utm_source=footer>.