[chef] Re: Inserting files into templates


Chronological Thread 
  • From: Jek Sirex < >
  • To:
  • Subject: [chef] Re: Inserting files into templates
  • Date: Tue, 10 Sep 2013 10:27:32 +0300


grabbed_data on compile-time is ''. Also templates' variables are built on compile-time. As result you have built wrong template resource.
You have to move out `File.read` from ruby_block or read file inside template (not sure).
It is very very very very important for you (and for me, when I started to learn chef) to completely understand `anatomy of chef_run`.


2013/9/10 Nick Bryant < " target="_blank"> >
Hi,

New to chef, be gentle.

I'm trying to write output from a web server into a template, but for
some reason can't make the data available to the template.

In my recipe I have:

# Set a location for the grabbed data
my_file = Chef::Config[:file_cache_path] + '/http_output.php'

# Grab the data and write the file if it doesn't exist
remote_file my_file do
  source 'http://example.com/data_to_grab.html'
  action :create_if_missing
  mode 0644
end

#declare globally
gabbed_data = ''

#read the data into a variable
ruby_block 'read-grabbed-data' do
  block do
    grabbed_data = File.read(my_file)
  end
  action :create
end

# use the grabbed data in a template
template node['myapp']['path'] + '/config.php' do
  source 'config.php.erb'
  mode 0755
  owner 'root'
  group 'root'
  variables(
    :password        => node['myapp']['password'],
    :my_file      => grabbed_data)
end


The template creates the config.php but doesn't output anything for
the my_file part. I've checked inside the block that the data is read,
and it is. I've also replaced grabbed_data with a string in the
template, and it makes it into the config.php output so the template
is good. I'm guessing it's something to do with the scope of the
variable not being available to the template.

Any ideas?

N




Archive powered by MHonArc 2.6.16.

§