State debconf.set_file now accepts a template as a file

It follows the same convention as `file.managed` for example.

This also introduces a new function in the `debconfmod` module to set the
Debconf selections from a template or, as before, from a simple file.

Fixes #7331
This commit is contained in:
Jonathan Ballet 2014-11-19 18:02:49 +01:00
parent 97959959ca
commit 56ef62b0a7
2 changed files with 75 additions and 3 deletions

View File

@ -133,6 +133,42 @@ def set_(package, question, type, value, *extra):
return True
def set_template(path, template, context, defaults, saltenv='base', **kwargs):
'''
Set answers to debconf questions from a template.
path
location of the file containing the package selections
template
template format
context
variables to add to the template environment
default
default values for the template environment
CLI Example:
.. code-block:: bash
salt '*' debconf.set_template salt://pathto/pkg.selections.jinja jinja None None
'''
path = __salt__['cp.get_template'](
path=path,
dest=None,
template=template,
saltenv=saltenv,
context=context,
defaults=defaults,
**kwargs)
return set_file(path, saltenv, **kwargs)
def set_file(path, saltenv='base', **kwargs):
'''
Set answers to debconf questions from a file.

View File

@ -57,9 +57,9 @@ def __virtual__():
return __virtualname__
def set_file(name, source, **kwargs):
def set_file(name, source, template=None, context=None, defaults=None, **kwargs):
'''
Set debconf selections from a file
Set debconf selections from a file or a template
.. code-block:: yaml
@ -71,20 +71,56 @@ def set_file(name, source, **kwargs):
debconf.set_file:
- source: salt://pathto/pkg.selections?saltenv=myenvironment
<state_id>:
debconf.set_file:
- source: salt://pathto/pkg.selections.jinja2
- template: jinja2
- context:
some_value: "false"
source:
The location of the file containing the package selections
template
If this setting is applied then the named templating engine will be
used to render the package selections file, currently jinja, mako, and
wempy are supported
context
Overrides default context variables passed to the template.
defaults
Default context passed to the template.
'''
ret = {'name': name,
'changes': {},
'result': True,
'comment': ''}
if context is None:
context = {}
elif not isinstance(context, dict):
return _error(
ret, 'Context must be formed as a dict')
if defaults is None:
defaults = {}
elif not isinstance(defaults, dict):
return _error(
ret, 'Defaults must be formed as a dict')
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'Debconf selections would have been set.'
return ret
if __salt__['debconf.set_file'](source, **kwargs):
if template:
result = __salt__['debconf.set_template'](source, template, context, defaults, **kwargs)
else:
result = __salt__['debconf.set_file'](source, **kwargs)
if result:
ret['comment'] = 'Debconf selections were set.'
else:
ret['result'] = False