0

I have the following output:

dante
Last password change                    : Aug 18, 2017
Password expires                    : never
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7
Linux shell                                         : /bin/bash

marion
Last password change                    : Aug 28, 2017
Password expires                    : never
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7
Linux shell                                         : /bin/bash 

This output is done with the following command:

cat /etc/passwd | xargs -n1 -I{} bash -c 'a=`echo "{}" | cut -f1 -d:`; echo -e "\n$a"; chage -l $a; echo -e "Linux shell\t: " `echo "{}" | cut -f7 -d:`' >> users-list.log

I would need for each username to get and add the groups that is belonging to in the user-list.log. For example:

dante
Last password change                    : Aug 18, 2017
Password expires                    : never
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7
Linux shell                                         : /bin/bash
Groups                                              : dante, prime, trm

marion
Last password change                    : Aug 28, 2017
Password expires                    : never
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7
Linux shell                                         : /bin/bash 
Groups                                              : marion, secondary, krq

I need to run everything in one command and all quotes should be escaped because I will run it in Ansible. If is any other way to make it run in Ansible, I am opened to any suggestions.

Best regards, Romain

Romain
  • 171
  • 1
  • 3
  • 15
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww May 16 '18 at 02:43

1 Answers1

0

You do not need to run everything in one long and ugly command. With a little care you can run arbitrarily complex code, though I wouldn't get crazy with it. Adjust this to your needs -

- name: run a complex shell command
  shell: |
    : careful with comments in here, and best to end lines with semicolons;
    while IFS=: read -r user x uid gid info home shell ;  
    do printf "\n$user\n";
       chage -l $user;
       printf "%-56s: $shell\n" "Linux shell"
       printf "%-56s: " "User Groups"
       grep $user /etc/group | cut -f 1 -d : | paste -s
    done < /etc/passwd > users-list.log
  • or better yet, write and template a small script file and call it.

The | in shell: | means the following data is a single block scalar. Peek here for examples and elaborations, but it preserves lines. Just format your indentations carefully. :)

To add groups, you can parse the line according to standard format, but beware that the info field may or may not have data in it, and it can be somewhat arbitrary, likely embedding spaces.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • Thank you, this was the way. I had many Linux distributions and for some of them works just if the last line is written like this: `done < /etc/passwd > users-list.log` – Romain May 17 '18 at 10:57