From 95434214a9559b9e09c8cb99fc50cc15fc2688be Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Sun, 22 Oct 2023 00:32:10 +1100 Subject: [PATCH] feat: add management of /etc/hosts - add class to manage the /etc/hosts file - add static hosts to /etc/hosts file via hiera array/hash --- hieradata/common.yaml | 15 +++++++++++++ site/profiles/manifests/base.pp | 3 +++ site/profiles/manifests/base/hosts.pp | 30 ++++++++++++++++++++++++++ site/profiles/templates/base/hosts.erb | 15 +++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 site/profiles/manifests/base/hosts.pp create mode 100644 site/profiles/templates/base/hosts.erb diff --git a/hieradata/common.yaml b/hieradata/common.yaml index d07c72a..1520bd2 100644 --- a/hieradata/common.yaml +++ b/hieradata/common.yaml @@ -43,3 +43,18 @@ puppetdb::master::config::puppetdb_host: "%{lookup('profiles::puppet::puppetdb:: profiles::accounts::sysadmin::sshkeys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDZ8SRLlPiDylBpdWR9LpvPg4fDVD+DZst4yRPFwMMhta4mnB1H9XuvZkptDhXywWQ7QIcqa2WbhCen0OQJCtwn3s7EYtacmF5MxmwBYocPoK2AArGuh6NA9rwTdLrPdzhZ+gwe88PAzRLNzjm0ZBR+mA9saMbPJdqpKp0AWeAM8QofRQAWuCzQg9i0Pn1KDMvVDRHCZof4pVlHSTyHNektq4ifovn0zhKC8jD/cYu95mc5ftBbORexpGiQWwQ3HZw1IBe0ZETB1qPIPwsoJpt3suvMrL6T2//fcIIUE3TcyJKb/yhztja4TZs5jT8370G/vhlT70He0YPxqHub8ZfBv0khlkY93VBWYpNGJwM1fVqlw7XbfBNdOuJivJac8eW317ZdiDnKkBTxapThpPG3et9ib1HoPGKRsd/fICzNz16h2R3tddSdihTFL+bmTCa6Lo+5t5uRuFjQvhSLSgO2/gRAprc3scYOB4pY/lxOFfq3pU2VvSJtRgLNEYMUYKk= ben@unkin.net + + +profiles::base::hosts::additional_hosts: + - ip: 198.18.17.3 + hostname: prodinf01n01.main.unkin.net + aliases: + - prodinf01n01 + - puppet + - puppetmaster + - puppetca + - ip: 198.18.17.4 + hostname: prodinf01n04.main.unkin.net + aliases: + - prodinf01n04 + - puppetdb diff --git a/site/profiles/manifests/base.pp b/site/profiles/manifests/base.pp index 8e56160..e5831df 100644 --- a/site/profiles/manifests/base.pp +++ b/site/profiles/manifests/base.pp @@ -26,6 +26,9 @@ class profiles::base ( # include admin scripts include profiles::base::scripts + # include admin scripts + include profiles::base::hosts + # include the python class class { 'python': manage_python_package => true, diff --git a/site/profiles/manifests/base/hosts.pp b/site/profiles/manifests/base/hosts.pp new file mode 100644 index 0000000..922b244 --- /dev/null +++ b/site/profiles/manifests/base/hosts.pp @@ -0,0 +1,30 @@ +# basic class to manage the /etc/hosts file from a template +# +# @param additional_hosts: +# An array of hashes with ip/hostname/aliases +# Aliases is an array in case there is a need for multiple aliases +# +# class { 'profiles::base::hosts': +# additional_hosts => [ +# { 'ip' => '192.168.0.10', 'hostname' => 'server1.example.com', 'aliases' => ['server1'] }, +# { 'ip' => '192.168.0.11', 'hostname' => 'server2.example.com' }, +# # ... and so on +# ], +# } +# +class profiles::base::hosts ( + Array[Hash] $additional_hosts = [] +) { + + $fqdn = $facts['networking']['fqdn'] + $hostname = $facts['networking']['hostname'] + + # Ensure the file exists and manage its content + file { '/etc/hosts': + ensure => file, + owner => 'root', + group => 'root', + mode => '0644', + content => template('profiles/base/hosts.erb'), + } +} diff --git a/site/profiles/templates/base/hosts.erb b/site/profiles/templates/base/hosts.erb new file mode 100644 index 0000000..45bf0d2 --- /dev/null +++ b/site/profiles/templates/base/hosts.erb @@ -0,0 +1,15 @@ +# /etc/hosts file managed by Puppet + +# The following lines are desirable for IPv4 capable hosts +127.0.0.1 <%= @fqdn %> <%= @hostname %> +127.0.0.1 localhost.localdomain localhost +127.0.0.1 localhost4.localdomain4 localhost4 + +# The following lines are desirable for IPv6 capable hosts +::1 <%= @fqdn %> <%= @hostname %> +::1 localhost.localdomain localhost +::1 localhost6.localdomain6 localhost6 + +<% @additional_hosts.each do |host| -%> +<%= host['ip'] %> <%= host['hostname'] %> <%= host['aliases'].join(' ') if host['aliases'] %> +<% end -%>