为使用puppet的用户pipe理〜/ .ssh / config?

如何使用Puppet中的ERB模板文件调整〜/ .ssh / config文件中的“用户”行,使其包含与帐户名称匹配的正确用户名?

class accounts_global::tharold { account { 'tharold': ensure => present, } file { "/home/tharold/.ssh/config" : require => Account['tharold'], owner => 'tharold', group => 'tharold', mode => '0600', content => template('accounts_global/user_ssh_config.erb'), } } 

user_ssh_config.erb文件的内容如下所示:

 Host ssh.example.com Port 22 User tharold IdentityFile ~/.ssh/ssh-key 

问题是,<%= something =%>应该怎样用模板文件中的“User tharold”replace用户的账户名? 这个ERBconfiguration文件将被用于多个用户,所以我需要参数化文件的这一部分。

尝试使用<%= @name%>最终将“accounts_global :: tharold”放在文件中。

您需要根据以下内容将您的课程更改为定义,以便其他用户可以重新使用它们:

 define accounts_global::account () { account { $name: ensure => present, } file { "/home/${name}/.ssh/config" : require => Account[$name], owner => $name, group => $name, mode => '0600', content => template('accounts_global/user_ssh_config.erb'), } } 

使用这个你的~/.ssh/config ERB模板:

 Host ssh.example.com Port 22 User <%= @name %> IdentityFile ~/.ssh/ssh-key 

然后将其添加到您的Puppet清单:

 accounts_global::account { 'tharold': } 

顺便提一下,除非远程用户名不同,否则不需要在SSHconfiguration中传递User参数 – 默认情况下,SSH尝试使用当前用户名进行连接。

 class accounts_global::tharold { account { 'tharold': ensure => present, } $ssh_user = 'tharold' file { "/home/tharold/.ssh/config" : require => Account['tharold'], owner => 'tharold', group => 'tharold', mode => '0600', content => template('accounts_global/user_ssh_config.erb'), } } 

那么你的模板看起来像

 Host ssh.example.com Port 22 User <%= ssh_user %> IdentityFile ~/.ssh/ssh-key