2013-02-11 78 views
1

有没有办法将/root/.ssh/authorized_keys中所有现有的ssh密钥自动导入到puppet中?例如:将现有ssh_authorized_keys导入puppet资源

,如果我有/root/.ssh/ssh_authorized_keys以下内容:

ssh-rsa AAAAakljsehrkjysdfjkhasdkfhskjghg== [email protected] 
ssh-rsa AAAAajklrkljeykljrsyehkrjryekjdkj== [email protected] 

我想运行是这样的:

puppet resource ssh_authorized_key 

并获得以下输出:

ssh_authorized_key {'userA': 
    ensure => present, 
    key => 'AAAAakljsehrkjysdfjkhasdkfhskjghg==', 
    type => 'ssh-rsa', 
    name => '[email protected]', 
    user => 'root', 
} 
ssh_authorized_key {'userB': 
    ensure => present, 
    key => 'AAAAajklrkljeykljrsyehkrjryekjdkj==', 
    type => 'ssh-rsa', 
    name => '[email protected]', 
    user => 'root', 
} 

这是不是有可能?

回答

6

它可能是一个有点矫枉过正,但你可以运行blueprint抢所需的部分,或者一个简单的bash脚本:

while read line; do 
    keytype=$(echo $line | awk '{print $1}'); 
    keystr=$(echo $line | awk '{print $2}'); 
    username=$(echo $line | awk '{print $3}'); 

    echo "ssh_authorized_key {'$(echo $username | awk -F'@' '{print $1}')':"; 
    echo " ensure => present,"; 
    echo " key => '$keystr',"; 
    echo " type => '$keytype',"; 
    echo " name => '$username',"; 
    echo " user => '$(whoami)',"; 
    echo "}"; 
done < .ssh/authorized_keys 

来源:http://shtuff.it/article/7/Generate_Puppet_ssh_authorized_keys_resource_from_existing_keys

相关问题