Using the value from an XML file in a template variable

I’ve got an xml file that is generated during a chef run by a third party
utility, and I want to retrieve a value from this xml file and use it in a
template variable. How should I go about doing that?

I was thinking maybe something like this, but it didn’t work. other_thing
ends up being a blank value:

template “/some/file.xml” do
source "source_file.xml.erb
variables(
:some_thing = node[“some”][“thing”],
:other_thing = lazy {
require “rexml/document”

REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")),
’//id’).text
}
)
end

Use LWRP to create the template, as the variables being assigned using
template resource would be evaluated at compile time. And as you are
getting that value from run time generated file, it would fail.

Moreover, if you will try using lazy attribute evaluation that will also
not work as it would put lazy object in place of variable in XML template.
On Sep 28, 2014 4:46 PM, "Greg Barker" fletch@fletchowns.net wrote:

I've got an xml file that is generated during a chef run by a third party
utility, and I want to retrieve a value from this xml file and use it in a
template variable. How should I go about doing that?

I was thinking maybe something like this, but it didn't work. other_thing
ends up being a blank value:

template "/some/file.xml" do
source "source_file.xml.erb
variables(
:some_thing = node["some"]["thing"],
:other_thing = lazy {
require "rexml/document"

REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")),
'//id').text
}
)
end

On Sep 28, 2014, at 4:15 AM, Greg Barker fletch@fletchowns.net wrote:

I've got an xml file that is generated during a chef run by a third party utility, and I want to retrieve a value from this xml file and use it in a template variable. How should I go about doing that?

I was thinking maybe something like this, but it didn't work. other_thing ends up being a blank value:

template "/some/file.xml" do
source "source_file.xml.erb
variables(
:some_thing = node["some"]["thing"],
:other_thing = lazy {
require "rexml/document"
REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")), '//id').text
}
)
end

You need to use lazy at the top level of the attribute:

require "rexml/document"

template "/some/file.xml" do
source "source_file.xml.erb
variables(lazy do
:some_thing => node["some"]["thing"],
:other_thing => REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")), '//id').text
end)
end

On Sep 28, 2014, at 4:15 AM, Greg Barker fletch@fletchowns.net wrote:

I've got an xml file that is generated during a chef run by a third party utility, and I want to retrieve a value from this xml file and use it in a template variable. How should I go about doing that?

I was thinking maybe something like this, but it didn't work. other_thing ends up being a blank value:

template "/some/file.xml" do
source "source_file.xml.erb
variables(
:some_thing = node["some"]["thing"],
:other_thing = lazy {
require "rexml/document"
REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")), '//id').text
}
)
end

You need to use lazy at the top level of the attribute:

require "rexml/document"

template "/some/file.xml" do
source "source_file.xml.erb
variables(lazy do
:some_thing => node["some"]["thing"],
:other_thing => REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")), '//id').text
end)
end

Thanks! That got me on the right track. I got a syntax error though, I had
to change it to this:

template "/some/file.xml" do
source "source_file.xml.erb
variables(lazy {{
:some_thing => node["some"]["thing"],
:other_thing => REXML::XPath.first(REXML::
Document.new(File.new("#{node[:some][:dir]}/config.xml")), '//id').text
}})
end

Thanks again!

On Sun, Sep 28, 2014 at 9:47 AM, Noah Kantrowitz noah@coderanger.net
wrote:

On Sep 28, 2014, at 4:15 AM, Greg Barker fletch@fletchowns.net wrote:

I've got an xml file that is generated during a chef run by a third
party utility, and I want to retrieve a value from this xml file and use it
in a template variable. How should I go about doing that?

I was thinking maybe something like this, but it didn't work.
other_thing ends up being a blank value:

template "/some/file.xml" do
source "source_file.xml.erb
variables(
:some_thing = node["some"]["thing"],
:other_thing = lazy {
require "rexml/document"

REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")),
'//id').text

    }
)

end

You need to use lazy at the top level of the attribute:

require "rexml/document"

template "/some/file.xml" do
source "source_file.xml.erb
variables(lazy do
:some_thing => node["some"]["thing"],
:other_thing =>
REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")),
'//id').text
end)
end

Oops I didn't tried that way. Thanks Noah.
On Sep 28, 2014 11:34 PM, "Greg Barker" fletch@fletchowns.net wrote:

Thanks! That got me on the right track. I got a syntax error though, I had
to change it to this:

template "/some/file.xml" do
source "source_file.xml.erb
variables(lazy {{
:some_thing => node["some"]["thing"],
:other_thing => REXML::XPath.first(REXML::
Document.new(File.new("#{node[:some][:dir]}/config.xml")), '//id').text
}})
end

Thanks again!

On Sun, Sep 28, 2014 at 9:47 AM, Noah Kantrowitz noah@coderanger.net
wrote:

On Sep 28, 2014, at 4:15 AM, Greg Barker fletch@fletchowns.net wrote:

I've got an xml file that is generated during a chef run by a third
party utility, and I want to retrieve a value from this xml file and use it
in a template variable. How should I go about doing that?

I was thinking maybe something like this, but it didn't work.
other_thing ends up being a blank value:

template "/some/file.xml" do
source "source_file.xml.erb
variables(
:some_thing = node["some"]["thing"],
:other_thing = lazy {
require "rexml/document"

REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")),
'//id').text

    }
)

end

You need to use lazy at the top level of the attribute:

require "rexml/document"

template "/some/file.xml" do
source "source_file.xml.erb
variables(lazy do
:some_thing => node["some"]["thing"],
:other_thing =>
REXML::XPath.first(REXML::Document.new(File.new("#{node[:some][:dir]}/config.xml")),
'//id').text
end)
end