Defined Type: psick::openssh::keyscan

Defined in:
manifests/openssh/keyscan.pp

Overview

Define: psick::openssh::keyscan Scans an host key and add it to known_hosts fileof the defined user

user

Set the user for which to add the hsot key to ~/.ssh/known_hosts. Default is taken from the title.

host

The hostname of the remote host to scan

known_hosts_path

The absolute path where to write the remote host ssh key. Overrides default ~/.ssh/known_hosts

create_ssh_dir

If to create the .ssh directory in the user’s home

Parameters:

  • user (String) (defaults to: 'root')
  • host (String) (defaults to: $title)
  • known_hosts_path (Optional[Stdlib::AbsolutePath]) (defaults to: undef)
  • create_ssh_dir (Boolean) (defaults to: false)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'manifests/openssh/keyscan.pp', line 14

define psick::openssh::keyscan (
  String                         $user             = 'root',
  String                         $host             = $title,
  Optional[Stdlib::AbsolutePath] $known_hosts_path = undef,
  Boolean                        $create_ssh_dir   = false,
) {
  $known_hosts_path_real = $known_hosts_path ? {
    undef   => $user ? {
      'root'  => '/root/.ssh/known_hosts',
      default => "/home/${user}/.ssh/known_hosts",
    },
    default => $known_hosts_path,
  }

  $known_hosts_dir = dirname($known_hosts_path_real)

  exec { "ssh-keyscan-${title}":
    command => "ssh-keyscan ${host} >> ${known_hosts_path_real}",
    user    => $user,
    unless  => "grep ${host} ${known_hosts_path_real}",
    path    => $facts['path'],
  }

  if $create_ssh_dir {
    psick::tools::create_dir { "openssh_keyscan_${known_hosts_dir}":
      path   => $known_hosts_dir,
      owner  => $user,
      before => Exec["ssh-keyscan-${title}"],
    }
  }
}