如何使用 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”替换为用户的帐户名?此 ERB 配置文件将用于多个用户,因此我需要参数化文件的该部分。
尝试使用 <%= @name %> 最终将“accounts_global::tharold”放入文件中。
答案1
您需要按照以下步骤将您的课程更改为定义,以便其他用户可以重复使用:
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': }
User
顺便说一句,除非远程用户名不同,否则您不需要在 SSH 配置中传递参数- 默认情况下,SSH 会尝试使用当前用户名进行连接。
答案2
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