Inserting files into templates


#1

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


#2

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.
http://docs.opscode.com/essentials_nodes_chef_run.html

2013/9/10 Nick Bryant nick+opscode.chef@servermule.com

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


#3

Hey Jek,

Thanks for the response. You hit the nail on the head, the template
was being evaluated when the variable had no content…

I tried using ‘action: nothing’ in the template and doing a ‘notifies:
create’ from the ruby block, but sadly it still didn’t work.

Seems putting dynamic content into the template resource is pretty difficult!

Thanks anyhow,

Nick

On 10 September 2013 17:27, Jek Sirex jsirex@gmail.com wrote:

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.
http://docs.opscode.com/essentials_nodes_chef_run.html

2013/9/10 Nick Bryant nick+opscode.chef@servermule.com

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


#4

On Sep 11, 2013, at 7:10 AM, Nick Bryant nick+opscode.chef@servermule.com wrote:

I tried using ‘action: nothing’ in the template and doing a ‘notifies:
create’ from the ruby block, but sadly it still didn’t work.

Try doing a “subscribes” from the other direction. I’ve found that sometimes a notify won’t work but a subscribe will. In theory, it’s just a difference between a push and a pull notification, but you use whatever works – right?


Brad Knowles brad@shub-internet.org
LinkedIn Profile: http://tinyurl.com/y8kpxu


#5

On Wednesday, September 11, 2013 at 6:48 AM, Brad Knowles wrote:

On Sep 11, 2013, at 7:10 AM, Nick Bryant <nick+opscode.chef@servermule.com (mailto:nick+opscode.chef@servermule.com)> wrote:

I tried using ‘action: nothing’ in the template and doing a ‘notifies:
create’ from the ruby block, but sadly it still didn’t work.

Try doing a “subscribes” from the other direction. I’ve found that sometimes a notify won’t work but a subscribe will. In theory, it’s just a difference between a push and a pull notification, but you use whatever works – right?


Brad Knowles <brad@shub-internet.org (mailto:brad@shub-internet.org)>
LinkedIn Profile: http://tinyurl.com/y8kpxu

I would be pretty interested to see how this occurs. Notifies and subscribes are implemented in exactly the same way.


Daniel DeLeo


#6

Sounds like you want lazy attribute evaluation?

http://docs.opscode.com/release_notes.html

On 9/11/13 5:10 AM, Nick Bryant wrote:

Hey Jek,

Thanks for the response. You hit the nail on the head, the template
was being evaluated when the variable had no content…

I tried using ‘action: nothing’ in the template and doing a ‘notifies:
create’ from the ruby block, but sadly it still didn’t work.

Seems putting dynamic content into the template resource is pretty difficult!

Thanks anyhow,

Nick

On 10 September 2013 17:27, Jek Sirex jsirex@gmail.com wrote:

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.
http://docs.opscode.com/essentials_nodes_chef_run.html

2013/9/10 Nick Bryant nick+opscode.chef@servermule.com

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