I have a question about ruby/chef, I am creating a recipe that will setup a cron job once we chef-client the node and I am getting a syntax error when I run my kitchen test
The following block of code is part of my recipe
action node["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["enabled"] = ? :create : :delete
minute node["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["minute"]
hour node["hadoop_temp"]["scripts"]["cron"]["clean_teamp"]["hour"]
day node["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["day"]
month node["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["month"]
weekday node["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["weekday"]
the following block is part of my attributes.
#Run the cron every day at 12AM cleans /temp
default["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["enabled"] = false
default["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["minute"] = "0"
default["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["hour"] = "0"
default["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["day"] = "*"
default["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["month"] = "*"
default["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["weekday"] = "*"
when I run my kitchen test I am getting the following error.
SyntaxError
-----------
/tmp/kitchen/cache/cookbooks/hadoop_temp/recipes/clean_temp.rb:25: syntax error, unexpected '?'
..."hadoop_temp"]["enabled"] = ? :create : :delete
I do not understand boolean 100% in ruby what I want to accomplish is if the entry exist create but if in the future I want to delete I just have to switch the attribute to true.
You could learn about the ternary operator in rubylearn about the ternary operator in ruby
I did took the = sign out of the recipe action. now it looks like:
action node["hadoop_temp"]["scripts"]["cron"]["clean_temp"]["enabled"] ? :create : :delete
Thank you #StephenKing the test passed
Related
I am new to chef and have little understanding of Ruby.
I am getting below value in 'storage_conn_str'
SAS Token: #Chef::DelayedEvaluator:0x0000000006e28c80#c:/chef/cache/cookbooks/*****/recipes/*****.rb:20
In my recipe, I am repalcing '&' with "&". I have used lazy to delay the execution of variable in ruby block and resource.
Below is my recipe
key_vault_name node['key_vault_names']['Test']
end
ruby_block 'modify_token' do
block do
sastoken = lazy { node.run_state['SAS_Token'] }
Chef::Log.info("SAS Token: #{sastoken}")
modified_token = lazy { sastoken.gsub(/[&]/, '&') }
Chef::Log.info("SAS Token after replacement: #{modified_token}")
storage_conn_str = lazy { File.join(storage_conn_str , modified_token)}
Chef::Log.info("storage connection string: #{storage_conn_str}")
end
action :run
end
webapp 'TableStorageAPI' do
source URI.join(node['binary_storage_uri'], app_node['source']).to_s
version app_version
appPoolName 'TableStorageAPI'
path '/V1/TableStorageAPI'
siteName 'SSL'
enable32Bit false
pipeline_mode :Integrated
use_servicebroker false
transform_variables(
storage_conn: lazy {storage_conn_str},
mail_to: app_node['mail_to'],
mail_from: app_node['mail_from'],
smtp_host: node['tps']['smtp']['server'],
log_location: app_node['log_location'],
env_name: app_node['env_name']
)
end```
----------------------------------------------
I am not sure why webapp resource is executing before the computation of 'storage_conn_str'.
"I am not sure why webapp resource is executing before the computation of 'storage_conn_str'."
That's because storage_conn_str is defined within ruby_block 'modify_token'. It is not visible to webapp resource.
What you can do is, create a attribute instead.
eg: node["mycb"]["storage_conn_str"] = ""
In ruby_block
ruby_block 'modify_token' do
block do
...
storage_conn_str = lazy { File.join(storage_conn_str , modified_token)}
Chef::Log.info("storage connection string: #{storage_conn_str}")
node.default["mycb"]["storage_conn_str"] = storage_conn_str
end
action :run
end
In webapp resource
webapp 'TableStorageAPI' do
...
storage_conn: node["mycb"]["storage_conn_str"]
...
end
This should solve the problem you are having
I'm trying to write a cookbook in Chef (very new) and I get the following error I cannot wrap my head around.
template node '/etc/selinux/config' do
source "config.erb"
mode "0644"
variables(
:selinux_state => node['selinux']['selinux-state'],
:selinux_type => node['selinux']['selinux-type']
)
owner duser
group dgroup
action :create
ignore_failure true
end
FATAL: ArgumentError: wrong number of arguments (given 1, expected 0)
I checked the documentation and as far as I can tell I am following the correct syntax.
Could anyone please enlighten me where the error is?
You have an extra node in the resource declaration. Change:
template node '/etc/selinux/config' do
to
template '/etc/selinux/config' do
I am trying to use whitespace arrays in chef template, like below and when I run the chef-client to execute the recipe getting an error saying: option variables must be a kind of [Hash]! below is recipe file
abc = node['abc']
def1 = node['def']
abc_sit = abc['sit']
def_sit = def1['sit']
%w{abc_sit def_sit}.each do | client |
template "/etc/#{client}.sh" do
source 'tunnel.erb'
owner 'root'
group 'root'
variables ("#{client}") --> At this line I am getting error
end
end
The error I am getting when I run the chef-client:
option variables must be a kind of [Hash]! You passed "abc_sit"
As it says, you have to pass in a Hash. Perhaps something like variables myclient: client and then <%= #myclient %> in the template.
I am trying to increment the value and use in another resource dynamically in recipe but still failing to do that.
Chef::Log.info("I am in #{cookbook_name}::#{recipe_name} and current disk count #{node[:oracle][:asm][:test]}")
bash "beforeTest" do
code lazy{ echo #{node[:oracle][:asm][:test]} }
end
ruby_block "test current disk count" do
block do
node.set[:oracle][:asm][:test] = "#{node[:oracle][:asm][:test]}".to_i+1
end
end
bash "test" do
code lazy{ echo #{node[:oracle][:asm][:test]} }
end
However I'm still getting the error bellow:
NoMethodError ------------- undefined method echo' for Chef::Resource::Bash
Cookbook Trace: ---------------
/var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb:3:in block (2 levels) in from_file'
Resource Declaration: ---------------------
# In /var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb
1: bash "beforeTest" do
2: code lazy{
3: echo "#{node[:oracle][:asm][:test]}"
4: }
5: end
Please can you help how lazy should be used in bash? If not lazy is there any other option?
bash "beforeTest" do
code lazy { "echo #{node[:oracle][:asm][:test]}" }
end
You should quote the command for the interpolation to work; if not, ruby would search for an echo command, which is unknown in ruby context (thus the error you get in log).
Warning: lazy has to be for the whole resource attribute; something like this WON'T work:
bash "beforeTest" do
code "echo node asm test is: #{lazy { node[:oracle][:asm][:test]} }"
end
The lazy evaluation takes a block of ruby code, as decribed here
You may have a better result with the log resource like this:
log "print before" do
message lazy { "node asm test is #{node[:oracle][:asm][:test]}" }
end
I've been drilling my head solving this problem until I came up with lambda expressions. But yet just using lambda didn't help me at all. So I thought of using both lambda and lazy evaluation. Though lambda is already lazy loading, when compiling chef recipe's, the resource where you call the lambda expression is still being evaluated. So to prevent it to being evaluated (somehow), I've put it inside a lazy evaluation string.
The lambda expression
app_version = lambda{`cat version`}
then the resource block
file 'tmp/validate.version' do
user 'user'
group 'user_group'
content lazy { app_version.call }
mode '0755'
end
Hope this can help others too :) or if you have some better solution please do let me know :)
I am using the msutter DSC module for puppet. While reading through the source code, I come across code like this (in dsc_configuration_provider.rb):
def create
Puppet.debug "\n" + ps_script_content('set')
output = powershell(ps_script_content('set'))
Puppet.debug output
end
What file defines the powershell function or method? Is it a ruby builtin? A puppet builtin? Inherited from a class? I know that it is being used to send text to powershell as a command and gathering results, but I need to see the source code to understand how to improve its error logging for my purposes, because certain powershell errors are being swallowed and no warnings are being printed to the Puppet log.
These lines in file dsc_provider_helpers.rb may be relevant:
provider.commands :powershell =>
if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
else
'powershell.exe'
end
Surely this defines where the Powershell executable is located, but gives no indication how it is called and how its return value is derived. Are stdout and stderr combined? Am I given the text output or just the error code? etc.
This is core Puppet logic. When a provider has a command, like
commands :powershell => some binary
That is hooked up as a function powershell(*args).
You can see it with other providers like Chocolatey:
commands :chocolatey => chocolatey_command
def self.chocolatey_command
if Puppet::Util::Platform.windows?
# must determine how to get to params in ruby
#default_location = $chocolatey::params::install_location || ENV['ALLUSERSPROFILE'] + '\chocolatey'
chocopath = ENV['ChocolateyInstall'] ||
('C:\Chocolatey' if File.directory?('C:\Chocolatey')) ||
('C:\ProgramData\chocolatey' if File.directory?('C:\ProgramData\chocolatey')) ||
"#{ENV['ALLUSERSPROFILE']}\chocolatey"
chocopath += '\bin\choco.exe'
else
chocopath = 'choco.exe'
end
chocopath
end
Then other locations can just call chocolatey like a function with args:
chocolatey(*args)