Skip to content

Commit

Permalink
Replace host by hosts.epp template
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd-ntrf committed Oct 27, 2023
1 parent bad0a3c commit a6a6dc9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
38 changes: 9 additions & 29 deletions site/profile/manifests/base.pp
Original file line number Diff line number Diff line change
Expand Up @@ -134,39 +134,19 @@
class profile::base::etc_hosts {
$domain_name = lookup('profile::freeipa::base::domain_name')
$int_domain_name = "int.${domain_name}"
$hostname = $facts['networking']['hostname']
$fqdn = "${hostname}.${int_domain_name}"
$interface = profile::getlocalinterface()
$ipaddress = $facts['networking']['interfaces'][$interface]['ip']
$instances = lookup('terraform.instances')

# build /etc/hosts
# Make sure /etc/hosts entry for the current host is managed by Puppet or
# that at least it is in entered in the right format.
exec { 'sed_fqdn':
command => "sed -i '/^${ipaddress}\\s/d' /etc/hosts",
unless => "grep -E '^${ipaddress}\\s+${fqdn}\\s+${hostname}$' /etc/hosts",
path => ['/bin'],
}

$instances = lookup('terraform.instances')
$hosts_to_add = Hash($instances.map |$k, $v| {
[
"${k}.${int_domain_name}",
{
ip => $v['local_ip'],
host_aliases => [$k],
require => Exec['sed_fqdn'],
before => Exec['sed_host_wo_fqdn'],
}
]
}
)
ensure_resources('host', $hosts_to_add)

exec { 'sed_host_wo_fqdn':
command => 'sed -i -E "/^[0-9]{1,3}(\\.[0-9]{1,3}){3}\\s+[a-z0-9-]+$/d" /etc/hosts',
onlyif => 'grep -E "^([0-9]{1,3}[\\.]){3}[0-9]{1,3}\\s+[a-z0-9-]+$" /etc/hosts',
path => ['/bin'],
file { '/etc/hosts':
mode => '0644',
content => epp('profile/base/hosts',
{
'instances' => $instances,
'int_domain_name' => $int_domain_name,
}
),
}
}

Expand Down
2 changes: 1 addition & 1 deletion site/profile/manifests/freeipa.pp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
timeout => 0,
require => [
Package['ipa-server-dns'],
Host[$fqdn],
File['/etc/hosts'],
],
notify => Service['systemd-logind'],
}
Expand Down
11 changes: 11 additions & 0 deletions site/profile/templates/base/hosts.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is managed by Puppet do not edit manually
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

<% if $facts['cloud']['provider'] == "gcp" { -%>
169.254.169.254 metadata.google.internal
<% } -%>

<% $instances.each|$key, $values| { -%>
<%= $values['local_ip'] %> <%= $key %>.<%= $int_domain_name%> <%= $key %>
<% } -%>

0 comments on commit a6a6dc9

Please sign in to comment.