Using facts inside a module to push data to file

560 Views Asked by At

I'm trying to create a simple module that will use facts from the agent to push the relevant output to file..

I've already managed to do it in one module but for an unknown reason it doesn't work as expected..

this is what I did

class testrepo {
    case $facts['os']['family'] {
            'RedHat':   {

            file_line { 'dscrp to local repo file':
            path => '/etc/yum.repos.d/test.repo',
            line => "name=${::description}",
            ensure => present,
            }

            file_line { 'repo from agent':
            path => '/etc/yum.repos.d/test.repo',
            line => "baseurl=file:///usr/local/src/RHEL/RHEL-${::full}-${::architecture}",
            ensure => present,
            }

in the first file_line the output in file is "name=". and in the second file_line it doesn't translate the ${::full} but I get the ${::architecture}

file_line { 'Add fdqn to /etc/hosts':
    path => '/etc/hosts',
    line => "${::ipaddress} ${::fqdn}    ${::hostname}",
    ensure => present,
    }

the above is working as expected

right now I'm not sure which direction should I check I've tried $facts['os']['familiy']['full'] , it also doesn't work

could anyone give me some advice here

thank you

1

There are 1 best solutions below

0
On

Architecture, fqdn and ipaddress are all facts available at the top level, if you jump onto the target node and run facter architecture you'll get an answer;

[root@example ~]# facter ipaddress
10.10.10.110
[root@example ~]# facter architecture
x86_64

"full" is part of the OS nested fact:

[root@example ~]# facter full

[root@r2h-bg5ore5nix0 ~]# facter os
{
  architecture => "x86_64",
  family => "RedHat",
  hardware => "x86_64",
  name => "CentOS",
  release => {
    full => "7.7.1908",
    major => "7",
    minor => "7"
  },
  selinux => {
    config_mode => "enforcing",
    config_policy => "targeted",
    current_mode => "enforcing",
    enabled => true,
    enforced => true,
    policy_version => "31"
  }
}

So you'll have to drill down through the os facts hash to do that, on the command line that's;

[root@example ~]# facter os.release.full
7.7.1908

In code you can experiment with;

   notify { 'message':
    message => "message is ${::os['release']['full']}", 
    }

or

  notify { 'message':
    message => "message is ${::facts['os']['release']['full']}", 
    }

So what you're going to need to do in your code is use:

line => "baseurl=file:///usr/local/src/RHEL/RHEL-${::os['release']['full']}-${::architecture}",