I am trying to do a lookup of the UID for each user that is defined in a list of dictionaries, where the list contains dictionaries “user”, “val1”, “val2”. I was attempting to use the getent module to access the “passwd” database and then display (debug) and then access these values in a subsequent task to create a file named with the UID and the key value pairs for the “val1”, “val2” dictionaries in the list written out as contents of the file.
I can’t get the syntax correct. For example, the list is defined as follows:
vars:
tgt_users:
– username: u1
val1: 10
val2: ab9d
– username: u2
val1: 60
val2: fdde
Assuming u1 and u2 exist in /etc/passwd on the target host(s), the end result would be debug output similar to the following:
‘User: u1, UID: 1001, val1: 10, val2: ab9d’
‘User: u2, UID: 1002, val1: 69, val2: fdde’
And, assuming target path of /tmp/, the following files would be created on the target host(s):
/tmp/u-1001.out
/tmp/u-1002.out
The file contents would be as follows:
$ cat /tmp/u-1001.out
val1=10
val2=ab9d
$ cat /tmp/u-1002.out
val1=60
val2=fdde
If creating a new list/array that contains all 4 values and then iterating over the new list to create the output files is easier, that would be fine as well.
I have tried everything I can think of, but continue to encounter syntax errors when trying to loop through the list, lookup the matching uid and get all pieces of information output via debug and written by another task to the desired files.
Any thoughts are greatly appreciated.
tedallen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.