apply recipe to all the servers whose hostnames matches regexp using CHEF - ruby

I'm creating a chef recipe to apply a configuration change on all servers whose hostname matches a specific pattern using regexp. However, I'm not sure how to do it.
Example: my hostname looks like this:
dvabwichf01
dvcdwichf01
my recipe in default.rb is :
case node['hostname']
when '*ab*'
template "/tmp/regextest" do
source "test_ab.erb"
mode "0644"
end
else
template "/tmp/regextest" do
source "test_cd.erb"
mode "0644"
end
end
But this is not working as expected, only the "else" template is updating on all servers. please assist.

You would need to use an actual regex, not a string like you have there (also you're using fnmatch glob matching, not a regex). That would only fix when the hostname is literally *ab*. A regexp literal in Ruby usually looks like /whatever/. so when /ab/ in this case.

I used switch for choosing values by adding a method in my helper file (in my case I put it into / app / helpers / application_helper.rb
Example below:
def name_of_your_method(hostname)
case hostname
when "Host1"
"template_1"
when "Host2"
"template_2"
when "Host2"
"template_3"
when "Host3"
"template_4"
else
"template_default"
end
end
Then in your code you would use the name in your method:
<%= user.hostname %>
And in your table(data) you would have a column for hostname(in this example)
Hope this helps

Related

How to assign file content to chef node attribute

I have fingreprint.txt at the location "#{node['abc.d']}/fingreprint.txt"
The contents of the file are as below:
time="2015-03-25T17:53:12C" level=info msg="SHA1 Fingerprint=7F:D0:19:C5:80:42:66"
Now I want to retrieve the value of fingerprint and assign it to chef attribute
I am using the following ruby block
ruby_block "retrieve_fingerprint" do
block do
path="#{node['abc.d']}/fingreprint.txt"
Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
command = 'grep -Po '(?<=Fingerprint=)[^"]*' path '
command_out = shell_out(command)
node.default['fingerprint'] = command_out.stdout
end
action :create
end
It seems not to be working because of missing escape chars in command = 'grep -Po '(?<=Fingerprint=)[^"]*' path '.
Please let me know if there is some other way of assigning file content to node attribute
Two ways to answer this: first I would do the read (IO.read) and parsing (RegExp.new and friends) in Ruby rather than shelling out to grep.
if IO.read("#{node['abc.d']}/fingreprint.txt") =~ /Fingerprint=([^"]+)/
node.default['fingerprint'] = $1
end
Second, don't do this at all because it probably won't behave how you expect. You would have to take in to account both the two-pass loading process and the fact that default attributes are reset on every run. If you're trying to make an Ohai plugin, do that instead. If you're trying to use this data in later resources, you'll probably want to store it in a global variable and make copious use of the lazy {} helper.

Generate a unique erb template for known hosts using the same cookbook - Chef

I have a number of hosts that needs to use different master server hosts and unique ports in a file. So the value of the ports and servers are known.
The end result is that my erb file should produce something of this nature for all the hosts that will run the cookbook:
PG_Host = -h myservername:unique_port
My challenge is how to get the values iterated in the recipe so depending on the short hostname when the server runs the cookbook, it picks up a specific port and a specific master server.
I am having difficulty matching the template with the erb file. I'd like some simple solutions on making this happen. Any pointers will be appreciated.
Here's my recipe:
template '/var/lib/pgsql/conf/mymonitor.sh' do
source 'mymonitor.sh.erb'
owner 'postgres'
action :create
variables(
master_server: 'someserver.fqn',
master_port: '897'
)
end
Template file:
PG_HOST= -h <%= #master_server %>:<%= #master_port %>
So how do I get it to churn out the similar files for anotherserver.fqn on port 5555 etc using some kind of a loop? I'm not sure how my variables for the other servers and their ports should look like.
The variables you pass to the template can be computed during chef-client runs, so in your recipe you can compute the port in the way you wish, and also the server name (master_server) can be calculated during chef-client run, or you can use some of the node's automatic attributes (collected by ohai) such for example node[:hostname] or node[:fqdn]. IE.:
Passing the recipes variables dynamically
server_port = "42#{node[:ipaddress].rpartition('.')[-1]}"
template '/var/lib/pgsql/conf/mymonitor.sh' do
source 'mymonitor.sh.erb'
owner 'postgres'
action :create
variables(
master_server: node[:fqdn],
master_port: server_port
)
end
Or you can use in your template node's attributes directly:
PG_HOST= -h <%= node[:fqdn] %>:<%= node[:mycookbook][:server_port] %>
or using string interpolation
PG_HOST= -h <%= "#{node[:fqdn]}:#{node[:mycookbook][:server_port]"} %>
EDIT:
The value of the attributes can be overridden for each node (or role), so easily you can assign different value for the attributes for each node. Check chef attribute documentation to see details about how attributes work.

How can I create a custom :host_role fact from the hostname?

I'm looking to create a role based on host name prefix and I'm running into some problems. Ruby is new to me and although I've done extensive searching for a solution, I'm still confused.
Host names look like this:
work-server-01
home-server-01
Here's what I've written:
require 'facter'
Facter.add('host_role') do
setcode do
hostname_array = Facter.value(:hostname).split('-')
first_in_array = hostname_array.first
first_in_array.each do |x|
if x =~ /^(home|work)/
role = '"#{x}" server'
end
role
end
end
I'd like to use variable interpolation within my role assignment, but I feel like using a case statement along with 'when' is incorrect. Please keep in mind that I'm new to Ruby.
Would anybody have any ideas on how I might achieve my goal?
Pattern-Matching the Hostname Fact
The following is a relatively DRY refactoring of your code:
require 'facter'
Facter.add :host_role do
setcode do
location = case Facter.value(:hostname)
when /home/ then $&
when /work/ then $&
else 'unknown'
end
'%s server' % location
end
end
Mostly, it just looks for a regex match, and assigns the value of the match to location which is then returned as part of a formatted string.
On my system the hostname doesn't match either "home" or "work", so I correctly get:
Facter.value :host_role
#=> "unknown server"

Chef check for hostname?

It turns out I can't check for a node(host) name in Chef, so I'm trying to figure out the best way to make the following happen:
If hostname is X
ldap_access_filter = memberOf=<%= node['sssd_ldap']['ldap_access_node_filter'] %>
else
ldap_access_filter = memberOf=<%= node['sssd_ldap']['ldap_access_filter'] %>
end
The idea is that when the node name (or some matching variable) is true, then it uses the ldap_access_node_filter, which is a unique value, else, it uses the default value. I'm basically configuring sssd config, and one of the hosts requires a special ldap access filter.
If there is a better way, please let me know.
Please help.
Use
node.name
or
node.name.split('.')[0]
Ohai should allow you to get hostname?
If you want something node specific it seems like you could just plug in to the attribute precedence in chef. You would set a default value for the attribute maybe at the cookbook level and then set an explicit attribute on the node. It also means if you need 2 of these servers to have the value you don't change the cookbook just the config on the servers.
I just ran into this trying to deploy a special version of a file to one hostname. I used not_if, only_if
cookbook_file 'file/to/replace' do
not_if {node.name == 'host.domain.name'}
source 'file_version.4'
action :create
end
cookbook_file '/file/to/replace' do
only_if {node.name == 'host.domain.name'}
source 'file_version.5'
action :create
end

Passing variables between chef resources

i would like to show you my use case and then discuss possible solutions:
Problem A:
i have 2 recipes, "a" and "b".. "a" installs some program on my file system (say at "/usr/local/bin/stuff.sh" and recipe "b" needs to run this and do something with the output.
so recipe "a" looks something like:
execute "echo 'echo stuff' > /usr/local/bin/stuff.sh"
(the script just echo(es) "stuff" to stdout)
and recipe "b" looks something like:
include_recipe "a"
var=`/usr/local/bin/stuff.sh`
(note the backquotes, var should contain stuff)
and now i need to do something with it, for instance create a user with this username. so at script "b" i add
user "#{node[:var]}"
As it happens, this doesn't work.. apparently chef runs everything that is not a resource and only then runs the resources so as soon as i run the script chef complains that it cannot compile because it first tries to run the "var=..." line at recipe "b" and fails because the "execute ..." at recipe a did not run yet and so the "stuff.sh" script does not exist yet.
Needless to say, this is extremely annoying as it breaks the "Chef runs everything in order from top to bottom" that i was promised when i started using it.
However, i am not very picky so i started looking for alternative solutions to this problem, so:
Problem B: i've run across the idea of "ruby_block". apparently, this is a resource so it will be evaluated along with the other resources. I said ok, then i'd like to create the script, get the output in a "ruby_block" and then pass it to "user". so recipe "b" now looks something like:
include_recipe "a"
ruby_block "a_block" do
block do
node.default[:var] = `/usr/local/bin/stuff.sh`
end
end
user "#{node[:var]}"
However, as it turns out the variable (var) was not passed from "ruby_block" to "user" and it remains empty. No matter what juggling i've tried to do with it i failed (or maybe i just didn't find the correct juggling method)
To the chef/ruby masters around: How do i solve Problem A? How do i solve Problem B?
You have already solved problem A with the Ruby block.
Now you have to solve problem B with a similar approach:
ruby_block "create user" do
block do
user = Chef::Resource::User.new(node[:var], run_context)
user.shell '/bin/bash' # Set parameters using this syntax
user.run_action :create
user.run_action :manage # Run multiple actions (if needed) by declaring them sequentially
end
end
You could also solve problem A by creating the file during the compile phase:
execute "echo 'echo stuff' > /usr/local/bin/stuff.sh" do
action :nothing
end.run_action(:run)
If following this course of action, make sure that:
/usr/local/bin exist during Chef's compile phase;
Either:
stuff.sh is executable; OR
Execute it through a shell (e.g.: var=`sh /usr/local/bin/stuff.sh`
The modern way to do this is to use a custom resource:
in cookbooks/create_script/resources/create_script.rb
provides :create_script
unified_mode true
property :script_name, :name_property: true
action :run do
execute "creating #{script_name}" do
command "echo 'echo stuff' > #{script_name}"
not_if { File.exist?(script_name) }
end
end
Then in recipe code:
create_script "/usr/local/bin/stuff.sh"
For the second case as written I'd avoid the use of a node variable entirely:
script_location = "/usr/local/bin/stuff.sh"
create_script script_location
# note: the user resources takes a username not a file path so the example is a bit
# strange, but that is the way the question was asked.
user script_location
If you need to move it into an attribute and call it from different recipes then there's no need for ruby_blocks or lazy:
some cookbook's attributes/default.rb file (or a policyfile, etc):
default['script_location'] = "/usr/local/bin/stuff.sh"
in recipe code or other custom resources:
create_script node['script_location']
user node['script_location']
There's no need to lazy things or use ruby_block using this approach.
There are actually a few ways to solve the issue that you're having.
The first way is to avoid the scope issues you're having in the passed blocks and do something like ths.
include_recipe "a"
this = self
ruby_block "a_block" do
block do
this.user `/usr/local/bin/stuff.sh`
end
end
Assuming that you plan on only using this once, that would work great. But if you're legitimately needing to store a variable on the node for other uses you can rely on the lazy call inside ruby to do a little work around of the issue.
include_recipe "a"
ruby_block "a_block" do
block do
node.default[:var] = `/usr/local/bin/stuff.sh`.strip
end
end
user do
username lazy { "#{node[:var]}" }
end
You'll quickly notice with Chef that it has an override for all default assumptions for cases just like this.

Resources