Unverified Commit 20d141de authored by j.knedlik's avatar j.knedlik Committed by GitHub
Browse files

Render specified fields using ERB (#2)

* Add field 'template_fields' that runs ERB /fields

* Run template_field test only once per resource

* Also expand resource 'name' when specified

* Add small comments for erb expansion

* Enable trim mode for one line 'each'

* Updated Readme for ERB style attributes

* Remove unneeded node configs

* Fix minor typos
parent 26ced380
...@@ -144,11 +144,48 @@ default_attributes( ...@@ -144,11 +144,48 @@ default_attributes(
``` ```
### ERB style attributes
Additionally you can use ERB template style attributes for generic/instantiatable roles and a boilerplate-less alternative to Chef::Resource::Template.
This is implemented for all attributes that have plain String content, including the user defined resource _name_.
To enable this feature one has to configure an additional attribute _template_fields_ (String/Array of String) of the resource, specifying the attributes that should be expanded/rendered by ERB using the Chef _node_-object.
```ruby
#... part of default_attributes(
file: {
##
# Time Synchronisation
#
"<%= node['ntp_config_location'] %>" => {
content: %q<
<% node['ntp_servers'].each do |server| %>
<%- %>server = <%= server -%>
<%- end %>
<% node['ntp_config_extra'].each do |conf| %>
<%=- conf -%>
<% end %>
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
restrict ::1
>,
"template_fields":["name","content"]
}
},
"ntp_config_location" => "/etc/ntp.conf",
"ntp_servers" => ["0.pool.ntp.org","1.pool.ntp.org","2.pool.ntp.org","3.pool.ntp.org"],
"ntp_config_extra" => ["driftfile /var/lib/ntp/ntp.drift",
"statistics loopstats peerstats clockstats",
"filegen loopstats file loopstats type day enable",
"filegen peerstats file peerstats type day enable",
"filegen clockstats file clockstats type day enable"]
#... end of default_attributes(
```
## License ## License
Author:: Victor Penso Author:: Victor Penso, Jan Knedlik
Copyright:: 2017 Copyright:: 2017, 2022
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
# limitations under the License. # limitations under the License.
# #
require 'erb'
resource_list = %w( resource_list = %w(
group group
user user
...@@ -70,9 +72,27 @@ resource_list.each do |resource| ...@@ -70,9 +72,27 @@ resource_list.each do |resource|
node[resource].each do |name,conf| node[resource].each do |name,conf|
public_send(resource, name) do # Get config fields which are to be expanded via ERB
case conf['template_fields']
when String
template_fields = [conf['template_fields']]
when Chef::Node::ImmutableArray
template_fields = conf['template_fields']
else
template_fields = []
end
# Expand user defined 'name' of resource via ERB if specified
expanded_name = template_fields.include?("name") ?
ERB.new(name,nil,'-').result_with_hash(node:node) :
name
public_send(resource, expanded_name) do
conf.each do |key,value| conf.each do |key,value|
# Expand config field via ERB if specified
value=ERB.new(value,nil,'-').result_with_hash(node:node) if template_fields.include?key
case key case key
...@@ -124,7 +144,7 @@ resource_list.each do |resource| ...@@ -124,7 +144,7 @@ resource_list.each do |resource|
end end
# Ignore the following keys... # Ignore the following keys...
when 'banner' when 'banner','template_fields'
next next
# Pass all attributes as resource properties by default # Pass all attributes as resource properties by default
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment