Do basic math inside Hiera

送分小仙女□ 提交于 2019-12-10 15:59:10

问题


I'm trying to set a crontab's weekday in hiera based on a custom fact and a basic modulo but I can't even figure out if it's possible.

I'd like to do something like:

  cron-job:
    command:  "do something"
    user:    myuser
    hour:    "%{::instance}"
    minute:  "%{::instance}"
    weekday: "%{::instance}" % 7

Can that even be done?


回答1:


No, this is not possible. Please keep in mind that the YAML is just data, not code.

Hiera does offer some transformations using Interpolation Tokens, but there are only two functions that you can use with these, no arithmetics.




回答2:


I'm not sure I follow the use case, but you could probably get away with using inline_epp or inline_template to make it look like this feature exists.

For example:

# In real usage this would be the result of a hiera lookup
$simple_lookup_result = '9 % 7'
$simple_evaluated = inline_template("<%= ${simple_lookup_result} %>")

exec { 'simple':
  command => "/bin/echo $simple_evaluated",
  logoutput => true,
}

# Again, hiera...
$complex_lookup_result = 'sprintf("The value is %i", 9 % 7)'
$complex_evaluated = inline_template("<%= ${complex_lookup_result} %>")

exec { 'complex':
  command => "/bin/echo $complex_evaluated",
  logoutput => true,
}

And the results:

$ puppet apply eval.pp 
Notice: Compiled catalog for box in environment production in 0.06 seconds
Notice: /Stage[main]/Main/Exec[simple]/returns: 2
Notice: /Stage[main]/Main/Exec[simple]/returns: executed successfully
Notice: /Stage[main]/Main/Exec[complex]/returns: The value is 2
Notice: /Stage[main]/Main/Exec[complex]/returns: executed successfully
Notice: Applied catalog in 0.05 seconds

Keep in mind that Hiera can interpolate variables or Hiera lookups, and lookups can also be done within the code that inline_epp or inline_template will ultimately evaluate.

N.B. that this is an example, and you shouldn't pass Hiera input into a shell command unless you trust your users and really like headaches.



来源:https://stackoverflow.com/questions/28775785/do-basic-math-inside-hiera

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!