Facts as values in parameterized classes

At the moment, no. However, I wrote a patch for it a while back[1] which is
awaiting a merge decision. Feel free to try it out and let us know if it
solves your usecase. Note that the patch allows for use of arbitrary ERB in
parameters, so I think it would be <%= @host.facts_hash['ipaddress_eth0']
%> and you'll probably have to disable Safemode Rendering in the
Provisioning Settings.

Note this patch is crafted for applying to the latest git head - it might
require some changes to patch against 1.1. I can help if you get stuck.

Cheers,
Greg

Sign in to GitHub · GitHub;

··· On 12 April 2013 14:42, Andy Bohne wrote:

Hello all,

Is there a way to set this such that my node will actually use the value
of the $::ipaddress_eth0 fact as the parameter?

Thanks for the info.

It does look like it would require some changes to patch against 1.1. The
main one being that there is no directory app/models/host, so there is no
app/models/host/managed.rb to patch. It looks like app/models/host.rb has
similar lines as listed in the PR for managed.rb. Should I apply those
changes at that point?

··· On Friday, April 12, 2013 10:45:36 AM UTC-4, Greg Sutcliffe wrote: > > On 12 April 2013 14:42, Andy Bohne <andy....@gmail.com >wrote: > >> Hello all, >> >> Is there a way to set this such that my node will actually use the value >> of the $::ipaddress_eth0 fact as the parameter? >> > > At the moment, no. However, I wrote a patch for it a while back[1] which > is awaiting a merge decision. Feel free to try it out and let us know if it > solves your usecase. Note that the patch allows for use of arbitrary ERB in > parameters, so I think it would be <%= @host.facts_hash['ipaddress_eth0'] > %> and you'll probably have to disable Safemode Rendering in the > Provisioning Settings. > > Note this patch is crafted for applying to the latest git head - it might > require some changes to patch against 1.1. I can help if you get stuck. > > Cheers, > Greg > > https://github.com/theforeman/foreman/pull/435 >

You can disregard my last post. I made the host/managed.rb changes to
host.rb and the rest of the changes as listed in the pull request. This
seems to have worked perfectly and completely solves my use case. Thanks
so much!

··· On Friday, April 12, 2013 11:55:51 AM UTC-4, Andy Bohne wrote: > > Thanks for the info. > > It does look like it would require some changes to patch against 1.1. The > main one being that there is no directory app/models/host, so there is no > app/models/host/managed.rb to patch. It looks like app/models/host.rb has > similar lines as listed in the PR for managed.rb. Should I apply those > changes at that point? > > On Friday, April 12, 2013 10:45:36 AM UTC-4, Greg Sutcliffe wrote: >> >> On 12 April 2013 14:42, Andy Bohne wrote: >> >>> Hello all, >>> >>> Is there a way to set this such that my node will actually use the value >>> of the $::ipaddress_eth0 fact as the parameter? >>> >> >> At the moment, no. However, I wrote a patch for it a while back[1] which >> is awaiting a merge decision. Feel free to try it out and let us know if it >> solves your usecase. Note that the patch allows for use of arbitrary ERB in >> parameters, so I think it would be <%= @host.facts_hash['ipaddress_eth0'] >> %> and you'll probably have to disable Safemode Rendering in the >> Provisioning Settings. >> >> Note this patch is crafted for applying to the latest git head - it might >> require some changes to patch against 1.1. I can help if you get stuck. >> >> Cheers, >> Greg >> >> https://github.com/theforeman/foreman/pull/435 >> >

Yes the files have changed location as part of the work for the next
release - glad you figured it out, and glad it works for you :slight_smile:

··· On 12 April 2013 17:12, Andy Bohne wrote:

You can disregard my last post. I made the host/managed.rb changes to
host.rb and the rest of the changes as listed in the pull request. This
seems to have worked perfectly and completely solves my use case. Thanks
so much!

I may have spoken too soon. There is another parameter in the class that
is an array. Since applying these changes, that parameter is no longer
functioning. It shows up as follows:

Before:

classes:
example:
seeds:

    - &quot;172.23.4.126&quot;
    - &quot;172.23.4.127&quot;

After:

classes:
example:
seeds:

It only shows up this way in the yaml. i.e. The correct values are in the
database (I verified this with the SQL statements output in debug mode).

··· On Friday, April 12, 2013 1:04:16 PM UTC-4, Greg Sutcliffe wrote: > > On 12 April 2013 17:12, Andy Bohne <andy....@gmail.com >wrote: > >> You can disregard my last post. I made the host/managed.rb changes to >> host.rb and the rest of the changes as listed in the pull request. This >> seems to have worked perfectly and completely solves my use case. Thanks >> so much! > > > Yes the files have changed location as part of the work for the next > release - glad you figured it out, and glad it works for you :) >

I think I know what's causing that. I can push a fix later on this weekend
(or Monday at the latest).

··· On 12 April 2013 21:59, Andy Bohne wrote:

I may have spoken too soon. There is another parameter in the class that
is an array. Since applying these changes, that parameter is no longer
functioning. It shows up as follows:

Andy, I've pushed a fix for Array and Hash valued ENC data to the branch -
can you try this one and see if it works for you?

Regards,
Greg

··· On 13 April 2013 12:34, Greg Sutcliffe wrote:

On 12 April 2013 21:59, Andy Bohne andy.bohne@gmail.com wrote:

I may have spoken too soon. There is another parameter in the class that
is an array. Since applying these changes, that parameter is no longer
functioning. It shows up as follows:

I think I know what’s causing that. I can push a fix later on this weekend
(or Monday at the latest).

Tested it out this morning and it seems to solve the issue. Thanks!

··· On Monday, April 15, 2013 8:59:01 AM UTC-4, Greg Sutcliffe wrote: > > On 13 April 2013 12:34, Greg Sutcliffe <greg.su...@gmail.com > > wrote: > >> On 12 April 2013 21:59, Andy Bohne <andy....@gmail.com >wrote: >> >>> I may have spoken too soon. There is another parameter in the class >>> that is an array. Since applying these changes, that parameter is no >>> longer functioning. It shows up as follows: >>> >>> >> I think I know what's causing that. I can push a fix later on this >> weekend (or Monday at the latest). >> > > Andy, I've pushed a fix for Array and Hash valued ENC data to the branch - > can you try this one and see if it works for you? > > Regards, > Greg >

One more finding after playing around a bit more.

I'm trying to use the following ERB in a value for the parameter named
profile:

<% if @host.facts_hash['is_virtual'] then -%>
virtual-guest
<% else -%>
default
<% end -%>

This results in a value of profile: "virtual-guest\n" or "default\n"

I looked at how you declared the ERB object in safe_render.rb and it seems
like it has trim_mode set to '-', yet I can't seem to get rid of the
trailing \n unless I define the value as
<% if @host.facts_hash['is_virtual'] then -%>virtual-guest<% else
-%>default<% end -%>

Am I writing this wrong or is something else going on?

··· On Monday, April 15, 2013 3:39:52 PM UTC-4, Andy Bohne wrote: > > Tested it out this morning and it seems to solve the issue. Thanks! > > On Monday, April 15, 2013 8:59:01 AM UTC-4, Greg Sutcliffe wrote: >> >> On 13 April 2013 12:34, Greg Sutcliffe wrote: >> >>> On 12 April 2013 21:59, Andy Bohne wrote: >>> >>>> I may have spoken too soon. There is another parameter in the class >>>> that is an array. Since applying these changes, that parameter is no >>>> longer functioning. It shows up as follows: >>>> >>>> >>> I think I know what's causing that. I can push a fix later on this >>> weekend (or Monday at the latest). >>> >> >> Andy, I've pushed a fix for Array and Hash valued ENC data to the branch >> - can you try this one and see if it works for you? >> >> Regards, >> Greg >> >

Hi Greg -

Was there a bug entered for this issue? I'm just curious if your patch is
going to make it into the 1.2 release…

Thanks! – Kal

··· On Monday, April 15, 2013 5:59:01 AM UTC-7, Greg Sutcliffe wrote: > > On 13 April 2013 12:34, Greg Sutcliffe <greg.su...@gmail.com > > wrote: > >> On 12 April 2013 21:59, Andy Bohne <andy....@gmail.com >wrote: >> >>> I may have spoken too soon. There is another parameter in the class >>> that is an array. Since applying these changes, that parameter is no >>> longer functioning. It shows up as follows: >>> >>> >> I think I know what's causing that. I can push a fix later on this >> weekend (or Monday at the latest). >> > > Andy, I've pushed a fix for Array and Hash valued ENC data to the branch - > can you try this one and see if it works for you? > > Regards, > Greg >

Thanks for testing :slight_smile:

There's not much we can do about this - it's part of how the strings are
handled. If you think about it, when you give Foreman this config:

Type: String
Value:
foo
bar

Then that's actually stored as ""foo\nbar". So with your ERB, it's being
stored as "<% if @host.facts_hash['is_virtual'] then -%>virtual-guest\n<%
else -%>default\n<% end -%>" which is then parsed by the rendering engine
when the ENC data is requested.

I'm debating whether it's a good idea to add a .chomp to the final return
of the parsed string, but I'm concerned it may have consequences for other
things using the rendering engine. Seems like specifying the ERB on a
single line is ok for now - what do you think?

Greg

··· On 15 April 2013 22:48, Andy Bohne wrote:

One more finding after playing around a bit more.

I’m trying to use the following ERB in a value for the parameter named
profile:

<% if @host.facts_hash[‘is_virtual’] then -%>
virtual-guest
<% else -%>
default
<% end -%>

This results in a value of profile: “virtual-guest\n” or “default\n”

I looked at how you declared the ERB object in safe_render.rb and it seems
like it has trim_mode set to ‘-’, yet I can’t seem to get rid of the
trailing \n unless I define the value as
<% if @host.facts_hash[‘is_virtual’] then -%>virtual-guest<% else
-%>default<% end -%>

Am I writing this wrong or is something else going on?

Actually, I did find one way to keep it on multiple lines, but it's not
exactly any more readable :wink:

<% unless @host.facts_hash['is_virtual'] then -%>
<%= "virtual-guest" -%>
<% else -%>
<%= "default" -%>
<% end -%>

Not exactly pretty, but it seems to work.

Makes sense. I found a third way, all 3 being equally unreadable. :slight_smile:

<% if @host.facts_hash['is_virtual'] then -%>
virtual-guest<%= -%>
<%- else -%>
default<%= -%>
<%- end -%>

··· On Tuesday, April 16, 2013 7:31:31 AM UTC-4, Greg Sutcliffe wrote: > > Actually, I did find one way to keep it on multiple lines, but it's not > exactly any more readable ;) > > <% unless @host.facts_hash['is_virtual'] then -%> > <%= "virtual-guest" -%> > <% else -%> > <%= "default" -%> > <% end -%> > > Not exactly pretty, but it seems to work. >