0

I've created two ruby facts ecdsa.rb and ed25519.rb and it checks if file exists. If it does exists then add a line to a file. But it adds both lines even though the second file doesn't exists.

Facter shows that the second file doesn't exists.

root@hostname:~# facter --json -p ecdsa_key_exists
    {
      "ecdsa_key_exists": true
    }


root@hostname:~# facter --json -p ed25519_key_exists
    {
      "ed25519_key_exists": false
    }

Here is the custom fact I wrote.

ecdsa.rb content: File /etc/ssh/ssh_host_ecdsa_key Exists.

Facter.add('ecdsa_key_exists') do
  setcode do
    File.exists?('/etc/ssh/ssh_host_ecdsa_key')
  end
end

ed25519.rb content: File /etc/ssh/ssh_host_ed25519_key has been deleted from the test server.

Facter.add('ed25519_key_exists') do
  setcode do
    File.exists?('/etc/ssh/ssh_host_ed25519_key')
  end
end

Template test.erb:

<% if @ecdsa_key_exists -%>HostKey /etc/ssh/ssh_host_ecdsa_key<% end %>

<% if @ed25519_key_exists -%>HostKey /etc/ssh/ssh_host_ed25519_key<% end %>

But when I run puppet agent -t, both lines gets added even though @ed25519_key_exists returns false.

puppet module init.pp:

  file { 'test.conf':
    path => '/tmp/test.conf',
    ensure => file,
    content => template("ssh/test.erb"),
  }
user630702
  • 2,529
  • 5
  • 35
  • 98

1 Answers1

0

This depends on the version of puppet you are using, and if you have stringified facts turned on or off (in older puppet versions they were always stringified). When facts are stringified, you end up with the strings "false" or "true" and both of them are considered to be boolean true in Ruby.

Check this by printing out both the datatype and the values of your facts.

Henrik Lindberg
  • 1,096
  • 9
  • 7