mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
file.managed: templating contents, not just files
This commit is contained in:
parent
7fb2331ebc
commit
10d882296d
2 changed files with 72 additions and 0 deletions
|
@ -3324,6 +3324,61 @@ def source_list(source, source_hash, saltenv):
|
|||
return source, source_hash
|
||||
|
||||
|
||||
def apply_template_on_contents(
|
||||
contents,
|
||||
template,
|
||||
context,
|
||||
defaults,
|
||||
saltenv):
|
||||
'''
|
||||
Return the contents after applying the templating engine
|
||||
|
||||
contents
|
||||
template string
|
||||
|
||||
template
|
||||
template format
|
||||
|
||||
context
|
||||
Overrides default context variables passed to the template.
|
||||
|
||||
defaults
|
||||
Default context passed to the template.
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' file.apply_template_on_contents \\
|
||||
contents='This is a {{ template }} string.' \\
|
||||
template=jinja \\
|
||||
"context={}" "defaults={'template': 'cool'}" \\
|
||||
saltenv=base
|
||||
'''
|
||||
if template in salt.utils.templates.TEMPLATE_REGISTRY:
|
||||
context_dict = defaults if defaults else {}
|
||||
if context:
|
||||
context_dict.update(context)
|
||||
# Apply templating
|
||||
contents = salt.utils.templates.TEMPLATE_REGISTRY[template](
|
||||
contents,
|
||||
from_str=True,
|
||||
to_str=True,
|
||||
context=context_dict,
|
||||
saltenv=saltenv,
|
||||
grains=__grains__,
|
||||
pillar=__pillar__,
|
||||
salt=__salt__,
|
||||
opts=__opts__)['data'].encode('utf-8')
|
||||
else:
|
||||
ret = {}
|
||||
ret['result'] = False
|
||||
ret['comment'] = ('Specified template format {0} is not supported'
|
||||
).format(template)
|
||||
return ret
|
||||
return contents
|
||||
|
||||
|
||||
def get_managed(
|
||||
name,
|
||||
template,
|
||||
|
|
|
@ -1477,6 +1477,23 @@ def managed(name,
|
|||
contents = os.linesep.join(validated_contents)
|
||||
if contents_newline and not contents.endswith(os.linesep):
|
||||
contents += os.linesep
|
||||
if template:
|
||||
contents = __salt__['file.apply_template_on_contents'](
|
||||
contents,
|
||||
template=template,
|
||||
context=context,
|
||||
defaults=defaults,
|
||||
saltenv=__env__)
|
||||
if not isinstance(contents, six.string_types):
|
||||
if 'result' in contents:
|
||||
ret['result'] = contents['result']
|
||||
else:
|
||||
ret['result'] = False
|
||||
if 'comment' in contents:
|
||||
ret['comment'] = contents['comment']
|
||||
else:
|
||||
ret['comment'] = 'Error while applying template on contents'
|
||||
return ret
|
||||
|
||||
# Make sure that leading zeros stripped by YAML loader are added back
|
||||
mode = __salt__['config.manage_mode'](mode)
|
||||
|
|
Loading…
Add table
Reference in a new issue