[TOC]
# mkdir /root/.pip; chown root:root /root/.pip; chmod 755 /root/.pip
directory "/root/.pip" do
owner "root"
group "root"
mode 0755
action :create
end
# cp cookbook/files/pip.conf /root/.pip/pip.conf; chown root:root /root/.pip/pip.conf; chmod 755 /root/.pip/pip.conf
cookbook_file "/root/.pip/pip.conf" do
source "pip.conf"
mode 0755
owner "root"
group "root"
end
example of variables, if environment, building nginx from source, forcing rebuild by recursive deletion of directory if file not exist
nginx_version = node[:nginx][:version]
headers_more_version = node[:nginx][:headers_more_version]
ngx_aws_auth_version = node[:nginx][:ngx_aws_auth_version]
if node.normal.chef_environment == "dev"
node.set[:nginx][:configure_flags] << "--add-module=#{Chef::Config[:file_cache_path]}/ngx_aws_auth-#{ngx_aws_auth_version}"
# if the newest version of this module does not exist, force rebuilding nginx by deleting current nginx binary
if !File.exist?("#{Chef::Config[:file_cache_path]}/ngx_aws_auth-#{ngx_aws_auth_version}.tar.gz")
directory node[:nginx][:install_path] do
recursive true
action :delete
end
end
remote_file "#{Chef::Config[:file_cache_path]}/ngx_aws_auth-#{ngx_aws_auth_version}.tar.gz" do
source "https://s3.amazonaws.com/dependencies-stable/tarballs/ngx_aws_auth-#{ngx_aws_auth_version}.tar.gz"
action :create_if_missing
end
bash "extract_specific_nginx_modules" do
cwd Chef::Config[:file_cache_path]
code <<-EOH
set -e
tar zxf ngx_aws_auth-#{ngx_aws_auth_version}.tar.gz
EOH
end
remote_file "#{Chef::Config[:file_cache_path]}/nginx-#{nginx_version}.tar.gz" do
source node[:nginx][:source][:url]
action :create_if_missing
end
end
Chef attributes and environments
chef attributes are used to configure per environment variables so recipes can be "clean"
attributes/myexample.rb
default[:example][:cdn_prefix] = "https://myexample.cloudfront.net"
recipes/myexample.rb
# Generate the /example/config/site.json file
data = Hash.new
missing_data = false
environment = node[:example][:environment]
ipaddress_public = node[:ec2] ? node[:ec2][:public_ipv4] : node[:ipaddress]
# Basic data
data["environment"] = environment
data["fqdn"] = node[:fqdn]
data["hostname"] = node[:hostname]
data["ipaddress"] = node[:ipaddress]
data["external"] = {
"web_host" => node[:example][:web_host],
"api_host" => node[:example][:api_host],
"cdn_prefix" => "https://myexample.cloudfront.net"
}