Reputation: 15619
I am writing a chef recipe using the below logic:
if grep -q -i "release 6" /etc/redhat-release
then
upload **file1** using cookbook_file resource
else if grep -q -i "release 7" /etc/redhat-release
then
upload **file2** using cookbook_file resource
fi
Please let me know how a chef recipe with above logic will look like??
What chef resources can I leverage?
Upvotes: 2
Views: 865
Reputation: 15784
Using a cookbook_file
resource you're not uploading a file, you're copying it locally as it has benn downloaded with the cookbook on the node (or it can be downloaded 'on-demand', depending on your client.rb configuration.
The files
directory in cookbook_file
allow to use file_specificity for this exact use case so in your context your recipe would be only:
cookbook_file '/path/to/target' do
source 'my_source_file'
action :create
end
And your cookbook files directory would look like this (the file in default will be used when there's no other matching directory, see the full doc in link above):
cookbook/
├── files
│ └── default
│ └── my_source_file
│ └── redhat_6.4
│ └── my_source_file
│ └── redhat_7.1
│ └── my_source_file
If you really want to use only the major version then you can remove the minor in the directory structure and use the Ohai attributes in the source property like this (use double quotes for interpolation of variables):
cookbook_file '/path/to/target' do
source "#{node[platform]}-#{node[platform_version][/(\d).\d/,1]}/my_source_file"
action :create
end
Upvotes: 2
Reputation: 5758
I recommend you to use the Ohai automatic attributes to get information of the underlying operating system.
# On all Fedora and RedHat based platforms
if ['fedora', 'rhel'].include?(node['platform_family'])
if node['platform_version'].to_i == 6
cookbook_file '/path/to/file1' do
source 'file1'
end
# ...
elsif node['platform_version'].to_i == 7
cookbook_file '/path/to/file2' do
source 'file2'
end
end
end
You can also use the Ruby case
statement if you prefer:
case node['platform_family']
# On all Fedora and RedHat based platforms
when 'fedora', 'rhel'
case node['platform_version'].to_i
when 6
cookbook_file '/path/to/file1' do
source 'file1'
end
# ...
when 7
cookbook_file '/path/to/file2' do
source 'file2'
end
end
end
You can also use a variable to save the file to upload as Vineeth Guna said:
myfile =
if ['fedora', 'rhel'].include?(node['platform_family']) && node['platform_version'].to_i == 6
'file1'
else
'file2'
end
cookbook_file "/path/to/#{myfile}" do
source myfile
end
See the recipe DSL documentation for more information and examples.
Upvotes: 1
Reputation: 398
As we can use ruby in chef recipes we can leverage it and upload the files as shown in below code
Make sure you have file1, file2
in files directory of your cookbook
file_to_upload = nil
if system("grep -q -i \"release 6\" /etc/redhat-release")
file_to_upload = <file1_name>
elsif system("grep -q -i \"release 7\" /etc/redhat-release")
file_to_upload = <file2_name>
fi
cookbook_file "<directory_where_file_needs_to_be_uploaded>/#{file_to_upload}"
source file_to_upload
action :create
end
Upvotes: -1