gpt4 book ai didi

用于在 Ansible 的特定输出中获取用户及其组的 Linux 命令

转载 作者:太空宇宙 更新时间:2023-11-04 12:06:58 25 4
gpt4 key购买 nike

我有以下输出:

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

此输出是使用以下命令完成的:

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

我需要为每个用户名获取并添加属于 user-list.log 的组。例如:

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

我需要在一个命令中运行所有内容,并且所有引号都应该转义,因为我将在 Ansible 中运行它。如果有任何其他方法可以让它在 Ansible 中运行,我愿意接受任何建议。

最好的问候,罗曼

最佳答案

您不需要在一条又长又丑的命令中运行所有内容。稍加小心,您就可以运行任意复杂的代码,尽管我不会为此疯狂。根据您的需要进行调整 -

- 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
  • 或者更好的是,编写并模板化一个小脚本文件并调用它。

shell: |中的|表示后面的数据是单 block 标量。偷看here用于示例和详细说明,但它保留了线条。只需仔细格式化缩进。 :)

要加组,可以根据standard format解析行,但请注意,信息字段中可能有数据,也可能没有数据,并且它可能有些随意,可能是嵌入空格。

关于用于在 Ansible 的特定输出中获取用户及其组的 Linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50350226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com