puppet执行一个python脚本,其中os.system(…)命令不起作用

我想用puppet来pipe理Unix用户。 Puppet提供了足够的工具来创build帐户并提供authorized_keys文件,但是没有设置用户密码,并告诉用户。

我所做的是一个生成随机密码并通过电子邮件发送给用户的python脚本。 问题是,用python启动passwd Unix命令是不可能的,于是我用命令写了一个bash脚本:

echo -ne "$password\n$password\n" | passwd $user passwd -e $user 

手动启动,脚本工作正常,创build的用户通过电子邮件发送密码。 但是当puppet启动它时,只有python脚本被执行,就好像os.system('/ bin / bash my_bash_script')被忽略。 没有错误显示。 用户获取密码,但passwd命令不会启动。

傀儡防止有什么限制来执行我所描述的? 或者,我怎样才能改变用户帐号,到期,并通过电子邮件发送密码?

我可以提供更多的信息,但现在,我不知道哪一个是准确的。

非常感谢!

编辑:这是一个基本的代码具有相同的症状:

python:setuserpassword.py

 #!/usr/bin/python import os import sys user = sys.argv[1] mail = sys.argv[2] os.system('/bin/bash /root/tools/setuserpassword.sh '+user+' '+mail) 

bash:setuserpassword.sh

 #!/bin/bash # Password setup for the account password=`pwgen -N1 --secure 10` echo -ne "$password\n$password\n" | passwd $USER > /dev/null 2>&1 [[ $? -ne 0 ]] && exit 2 # Setup the expirancy label to make sure user # change its password upon fist success login chage -d0 $USER # Email sending to inform user of his new password echo -ne "Hello, $USER;\n The password is:\n $password\n" | mail -s "$USER account created" $MAIL 

这里是模块(这方面没有问题)

 define add_user ( $email, $uid, $gid ) { $username = $title user { $username: comment => "$email", home => "/home/$username", shell => "/bin/bash", uid => $uid, gid => $gid, } exec { "/root/tools/setuserpassword.py $username $email": path => "/bin:/usr/bin:/usr/sbin/sbin", refreshonly => true, subscribe => user[$username], onlyif => "cat /etc/shadow | grep $username | grep '!'", require => Package['pwgen'], } 

不需要使用bash脚本,只需使用下一个我用于其他一些东西来生成阴影encryption密码的函数:

  def shadow(self,password): SomeZolt="$6$"+''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(9))+"$" return crypt.crypt(password, SomeZolt) 

之后,将额外的function写入您的python脚本,这将打开/ etc / shadow文件,并通过使用此function为此用户写入encryption的密码,从而制作如下行:

 some_user:!!:15579:0:99999:7::: 

使看起来像:

 some_user:$1$JShdquwdjsd38hSJhdqwdkwd:15579:0:99999:7::: 

因此some_user将拥有分配的密码。
另外,你可以通过改变它来直接在puppet语句中设置用户:

 user { $username: comment => "$email", home => "/home/$username", shell => "/bin/bash", uid => $uid, gid => $gid, password => "SomeAlreadyEncryptedPassword", } 

您可以使用指定的函数获取encryption的密码,然后将其插入到语句中。

感谢您的回答,我遵循了您的build议,并编写了一个def来生成密码哈希,并将其放在/ etc / shadow文件中。

这是我写的脚本,如果有人可能有兴趣:

 class Unix_Account: def __init__(self, user): self.user = user self.password = ''.join( random.choice( string.ascii_uppercase + string.ascii_lowercase + string.digits ) for x in range(password_length) ) self.salt = '$6$'+''.join( random.choice(string.ascii_uppercase + string.digits ) for x in range(9))+'$' self.line = self.user+':'+crypt.crypt(self.password, self.salt)+':0:0:99999:7:::' def change_password(self): self.result = re.sub(self.user+".*", self.line, file('/etc/shadow', 'r').read()) file('/etc/shadow', 'w').write(self.result) 

然后另一个class级将密码发送给用户。

密码过期date设置为0,以便用户在第一次连接时被强制更改。

连接是通过SSH进行的,必须使用相应的密钥来设置密码。 密码用于sudo命令。

再次感谢!