Server Roles

89 views
Skip to first unread message

Frank

unread,
Sep 28, 2012, 7:05:18 AM9/28/12
to puppet...@googlegroups.com
Hi,

I've seen some blog posts about adding server roles to Puppet.

Some of them with this fact snippet:

if File.exists?("/etc/roles.txt")
File.readlines("/etc/roles.txt").each do |line|
if line =~ /^(.+)=(.+)$/
fact  = $1
value = $2

Facter.add(fact) do
# confine :kernel => "Linux"
setcode {value}
end
end
end
end


Since I have both linux and windows boxes backed with Puppet, it is correct to add the "confine :kernel => "Linux"" next to "Facter.add(fact) and replicate the whole block changing this "confine :kernel to "Windows" and the path (to match window paths c:\foo)?

Is this the right approach?

thanks in advance.

Cheers,
-- 
Frank

jcbollinger

unread,
Sep 28, 2012, 9:35:06 AM9/28/12
to puppet...@googlegroups.com


No. The code you presented tests the Unix-style path well before it gets to the point where your confine enters the picture.  If the file was found at all then you do not need to 'confine' to Linux (unless you need to distinguish that case from other Unixes, such as OS X or Solaris).

You could duplicate the code and change the path (still not needing a 'confine'), but if I were writing this I would probably test the "kernel" or "os" fact at the start to determine which path to look for in the first place, then use just one fact declaration (still with no need for a 'confine').


John

Frank

unread,
Sep 28, 2012, 9:44:25 AM9/28/12
to puppet...@googlegroups.com


-- 
Frank

Ok, it makes sense. Then for me to use $$::kernel variable on this fact definition, I must require 'facter' first or I just use it in a conditional clause like this?

if $::kernel = "linux" then
# linux fact code
else
# windows fact code
end 
 

Frank



John

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/q1jyUOogM0cJ.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

Frank

unread,
Sep 28, 2012, 11:00:58 AM9/28/12
to puppet...@googlegroups.com
I'm going this way:

roles_file = ""

if Facter.kernel = 'linux'
roles_file = "/etc/roles_facts.txt"
elsif Facter.kernel = 'windows'
roles_file = "C:\\roles_facts.txt"
end

if File.exists?(roles_files)
File.readlines(roles_file).each do |line|
if line =~ /^(.+)=(.+)$/
fact  = $1
value = $2

Facter.add(fact) do
setcode {value}
end
end
end
end

:)

-- 
Frank

Reply all
Reply to author
Forward
0 new messages