Case condition for template resource path

Hello guys,
In a recipe i have to create a file from a template using
template "app.xml" do
path "/local/app/bin/app.xml"
source "app.xml.erb"
owner node['app']['user']
group node['app']['group']
mode "0644"
end

Having in mind that i have an attribute which sets the app version to install (allowing downgrade) and that the previous version it’s installed from an old rpm in another folder structure, I would need to use the same template resource, but when it’s the old version of app, to set another path, like (pseudo code):
template "app.xml" do
case new version:
path "/local/app/bin/app.xml"
case old version:
path "/anotherfolder/app/bin/app.xml"

source "app.xml.erb"
owner node['app']['user']
group node['app']['group']
mode "0644"
end

Any help would be appreciated.

Thank you,
Gabriel

I thought about it and the first solution that came to my mind was to create the same resource twice like in:
template "app.xml" do
path "/local/app/bin/app.xml"
source "app.xml.erb"
owner node['app']['user']
group node['app']['group']
mode "0644"
only_if node['app']['appversion']=4
end

and for the old version
template "app.xml" do
path "/anotherfolder/app/bin/app.xml"
source "app.xml.erb"
owner node['app']['user']
group node['app']['group']
mode "0644"
only_if node['app']['appversion']=3
end

Is there a better solution?

Thank you

One solution would be to move the logic to inside your erb template.

template 'app.xml' do
  source 'app.xml.erb'
  variables(:version => 3)
end

Then use xml “foo” if version 3 and “bar” if version 4
app.xml.erb

<xml>
<% if @version == 3 -%>
  <foo>
  </foo>
<% elseif @version == 4 -%>
  <bar>
  </bar>
<% end -%>
</xml>
1 Like

Thank you, but the xml files are quite big, so i decided to create a dedicated xml file for the old version which could be installed, using the same attributes where needed.

Your idea is great and i’ll remember to use it some other time

Thanks again,
Gabriel

For large xml files, it is best to keep a separate copy of each version, and use a variable in the source name

template 'app.xml' do
  source "app.xml-#{version}.erb"
end

app.xml-3.erb

<xml>
<foo/>
</xml>

app.xml-4.erb

<xml>
<bar/>
</xml>
1 Like