Struggling to write custom facts

asked 2017-09-01 03:41:11 -0500

sinaowolabi gravatar image

updated 2017-09-01 07:41:46 -0500

DarylW gravatar image

Hi

Puppet old newb here. I am trying to understand how to write facts and I think my lack of ruby knowledge is also a problem. On my first try, I am trying to write a fact that will detect if the system is a docker host by checking if /sbin/docker or /usr/local/bin/docker-compose files exist on the server, then trying to figure out the next steps. My custom fact looks like this:

Facter.add(:docker_installed) do
        confine :kernel => :linux
        has_weight 100
        setcode do
                if FileTest.exist? '/bin/docker'
                        'true'
                end
        end
end

Facter.add(:docker_installed) do
        confine :kernel => :linux
        has_weight 50
        setcode do
                if FileTest.exist? '/usr/local/bin/docker-compose'
                        'true'
                end
        end
end

I setup an init.pp:

class docker_stuff {

        file {'/tmp/testfile.txt':
                ensure => file,
                content => "the value of the 'docker_installed' fact is: $::docker_installed \n",
        }
}

But when I try to run it it is empty: facter docker_installed And I dont see the /tmp/testfile.txt created.

Please what am I doing wrong?

edit retag flag offensive close merge delete

Comments

Are you sure Class[docker_stuff] is actually applied? Please temporarily introduce some syntax error in your docker_stuff class and try to run Puppet. It should fail. See also variable interpolation in double quoted strings https://docs.puppet.com/puppet/latest/lang_variables.html#interpolation

Kai Burghardt gravatar imageKai Burghardt ( 2017-09-01 05:49:57 -0500 )edit

What version of puppet are you using? If puppet4 or 5, you should be using the `$facts['docker']` hash (not directly your problem, but good to know)

DarylW gravatar imageDarylW ( 2017-09-01 07:45:07 -0500 )edit

Sorry just realized there were comments! I made a change in the init.pp file (changed "ensure => file," to "ensure = file;" but nothing happened when I ran `puppet agent --test` on the master and on an client. Puppet master version is 3.8.7 on Centos 6, clients are version 3.6.2 RHEL.

sinaowolabi gravatar imagesinaowolabi ( 2017-09-01 15:24:41 -0500 )edit

Took a long hard look at my foolishness: I forgot to add "include docker_stuf"f to the /etc/puppet/manifests/site.pp. After I did that the /tmp/testfile.txt is created with the text. I did notice that running "facter docker_installed" still returns a space for an answer, what is causing this though?

sinaowolabi gravatar imagesinaowolabi ( 2017-09-01 15:41:35 -0500 )edit

I don't understand, why you have to put everything in an if-then-end-clause. Just directly let your FileTest.exists? expression be the last statement in the code block. Are you testing your code on a docker_host btw?

Kai Burghardt gravatar imageKai Burghardt ( 2017-09-01 15:50:24 -0500 )edit