有没有一种方法可以将/root/.ssh/authorized_keys中的所有现有ssh密钥自动导入到puppet中?例:

如果我在/root/.ssh/ssh_authorized_keys中具有以下内容:

ssh-rsa AAAAakljsehrkjysdfjkhasdkfhskjghg== userA@hostA
ssh-rsa AAAAajklrkljeykljrsyehkrjryekjdkj== userB@hostB

我想运行类似的东西:
puppet resource ssh_authorized_key

并获得以下输出:
ssh_authorized_key {'userA':
    ensure => present,
    key    => 'AAAAakljsehrkjysdfjkhasdkfhskjghg==',
    type   => 'ssh-rsa',
    name   => 'userA@hostA',
    user   => 'root',
}
ssh_authorized_key {'userB':
    ensure => present,
    key    => 'AAAAajklrkljeykljrsyehkrjryekjdkj==',
    type   => 'ssh-rsa',
    name   => 'userB@hostB',
    user   => 'root',
}

这可能吗?

最佳答案

可能有点矫kill过正,但是您可以运行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

09-25 15:53