2013-02-11 42 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

相關問題