Hi,
I'd need to create a provisioning template where a string variable is
assigned a value depending on the top level domain of the host, ignoring
any subdomains.
E.g.: myvar must be "first" when the TLD is ".mydomain", so hosts named
"myhost.sub.mydomain" or "newhost.mydomain" will have myvar = "first".
I suppose this is possible to achieve, but I'm rather new to ERB templates
and my attempts failed.
Reading some docs I tried stuff like this with many variations, but I got
various kind of errors, such as "cannot convert domain to string" or
"match2 is not allowed in safe mode", or parse errors:
Il giorno venerdì 27 giugno 2014 18:23:00 UTC+2, zerozer...@gmail.com ha
scritto:
> Reading some docs I tried stuff like this with many variations, but I got
> various kind of errors, such as "cannot convert domain to string" or
> "match2 is not allowed in safe mode", or parse errors:
>
> <% if /.mydomain/ =~ "@host.domain" -%>
> <% myvar = "first" -%>
> <% else -%>
> <% myvar = "second" -%>
> <% end -%>
>
Some updates…
If I just try to match on the full domain like this:
<% if @host.domain == "sub.mydomain" -%>
I get the following error:
undefined method '==' for Domain::Jail
This appears to be due to safemode rendering (but why?? Checking the domain name does not seem a security issue to me).
If I set safemode_render to false then the template is rendered, but the match does not work, it fails even if the domain is the same as the matching string (and I checked the domain name being used by adding a <%= @host.domain %> line just above the <%if-%>).
> Reading some docs I tried stuff like this with many variations, but I got
> various kind of errors, such as "cannot convert domain to string" or
> "match2 is not allowed in safe mode", or parse errors:
>
> <% if /.mydomain/ =~ "@host.domain" -%>
> <% myvar = "first" -%>
> <% else -%>
> <% myvar = "second" -%>
> <% end -%>
>
> Can anyone help?
Safemode templating engine does not allow match/match2 methods. You can
easily add them by editing lib/foreman/renderer.rb (ALLOWED_HELPERS).
For example if you add :match and :to_s method name there, you should be
able to do something like:
if @host.domain.to_s.match(/.mydomain/)
I hope I am right, please correct me.
If that works for you, please file a pull request, it should be safe to
allow these methods.
Or even better, you could create helper method in domain class named
match that would convert to string and match it, then you could only
allow :match method name.
> Il giorno venerdì 27 giugno 2014 18:23:00 UTC+2, zerozer...@gmail.com ha
> scritto:
>
>
>
>> Reading some docs I tried stuff like this with many variations, but I got
>> various kind of errors, such as "cannot convert domain to string" or
>> "match2 is not allowed in safe mode", or parse errors:
>>
>> <% if /.mydomain/ =~ "@host.domain" -%>
>> <% myvar = "first" -%>
>> <% else -%>
>> <% myvar = "second" -%>
>> <% end -%>
>>
>
> Some updates…
>
> If I just try to match on the full domain like this:
> <% if @host.domain == "sub.mydomain" -%>
>
try @host.domain.name == …
···
On Mon, Jun 30, 2014 at 12:26 PM, wrote:
I get the following error:
undefined method ‘==’ for Domain::Jail
This appears to be due to safemode rendering (but why?? Checking the domain name does not seem a security issue to me).
If I set safemode_render to false then the template is rendered, but the match does not work, it fails even if the domain is the same as the matching string (and I checked the domain name being used by adding a <%= @host.domain %> line just above the <%if-%>).
Thank you very much. @host.domain.name.match(/.mydomain$/) worked great after adding :match to
the allowed helpers, so I filed a pull request (and a Redmine issue before
that): https://github.com/theforeman/foreman/pull/1561
First time I do it, so I hope I got it right ;).
Bye.
Marco
···
Il giorno lunedì 30 giugno 2014 11:28:16 UTC+2, Lukas Zapletal ha scritto:
If that works for you, please file a pull request, it should be safe to
allow these methods.
Thank you, this worked fine indeed for exact domain name matches.
In the meantime for my specific need, before editing renderer.rb as
suggested by Lukas, I resorted to disabling safe mode rendering and using: @host.name =~ /.mydomain$/
Marco
···
Il giorno lunedì 30 giugno 2014 11:27:43 UTC+2, ohad ha scritto:
> @host.domain.name.match(/.mydomain$/) worked great after adding :match to
> the allowed helpers, so I filed a pull request (and a Redmine issue before
> that):
> https://github.com/theforeman/foreman/pull/1561
> First time I do it, so I hope I got it right ;).
Thanks, that was exactly what I thought. Well done!