diff --git a/modules/libs/lib/facter/enc_env.rb b/modules/libs/lib/facter/enc_env.rb new file mode 100644 index 0000000..2975c45 --- /dev/null +++ b/modules/libs/lib/facter/enc_env.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +Facter.add('enc_env') do + setcode do + require 'yaml' + # Check if the YAML file exists + if File.exist?('/root/.cache/custom_facts.yaml') + data = YAML.load_file('/root/.cache/custom_facts.yaml') + # Use safe navigation to return 'enc_env' or nil + data&.dig('enc_env') + end + end +end diff --git a/modules/libs/lib/facter/enc_role.rb b/modules/libs/lib/facter/enc_role.rb new file mode 100644 index 0000000..979b4bf --- /dev/null +++ b/modules/libs/lib/facter/enc_role.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +Facter.add('enc_role') do + setcode do + require 'yaml' + # Check if the YAML file exists + if File.exist?('/root/.cache/custom_facts.yaml') + data = YAML.load_file('/root/.cache/custom_facts.yaml') + # Use safe navigation to return 'enc_role' or nil + data&.dig('enc_role') + end + end +end diff --git a/site/profiles/manifests/base/facts.pp b/site/profiles/manifests/base/facts.pp index e234625..aa89994 100644 --- a/site/profiles/manifests/base/facts.pp +++ b/site/profiles/manifests/base/facts.pp @@ -12,18 +12,28 @@ class profiles::base::facts { mode => '0755', } - # facts to create + # cleanup old facts files $fact_list = [ 'enc_role', 'enc_env' ] - - # Manage the external fact file with content from the template $fact_list.each | String $item | { file { "${facts_d_path}/${item}.txt": - ensure => file, - owner => 'root', - group => 'root', - mode => '0644', - content => template("profiles/base/facts/${item}.erb"), - require => File[$facts_d_path], + ensure => absent, } } + + # ensure the path to the custom store exists + file { '/root/.cache': + ensure => directory, + owner => 'root', + group => 'root', + mode => '0750', + } + + # create the file that will be read + file { '/root/.cache/custom_facts.yaml': + ensure => absent, + owner => 'root', + group => 'root', + mode => '0644', + content => template('profiles/base/facts/custom_facts.yaml.erb'), + } } diff --git a/site/profiles/templates/base/facts/custom_facts.yaml.erb b/site/profiles/templates/base/facts/custom_facts.yaml.erb new file mode 100644 index 0000000..e4b3895 --- /dev/null +++ b/site/profiles/templates/base/facts/custom_facts.yaml.erb @@ -0,0 +1,3 @@ +--- +enc_role: <%= @enc_role[0] %> +enc_env: <%= @enc_env %>