为使用 puppet 的用户管理 ~/.ssh/config?

tgh*_*old 1 puppet template

如何使用 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'),
    }
} 
Run Code Online (Sandbox Code Playgroud)

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

Host ssh.example.com
Port 22
User tharold
IdentityFile ~/.ssh/ssh-key
Run Code Online (Sandbox Code Playgroud)

问题是,用用户的帐户名替换模板文件中的“User tharold”应该是什么样子的?这个 ERB 配置文件将被多个用户使用,所以我需要参数化文件的那部分。

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

Cra*_*son 5

您需要将类更改为定义,如下所示,以使其可重用于其他用户:

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'),
  }
}
Run Code Online (Sandbox Code Playgroud)

将此用于您的~/.ssh/configERB 模板:

Host ssh.example.com
Port 22
User <%= @name %>
IdentityFile ~/.ssh/ssh-key
Run Code Online (Sandbox Code Playgroud)

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

accounts_global::account { 'tharold': }
Run Code Online (Sandbox Code Playgroud)

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