community.general/lib/ansible/modules/system/debconf.py

195 lines
5.5 KiB
Python
Raw Normal View History

2014-09-26 01:01:01 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2014, Brian Coca <briancoca+ansible@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2014-09-26 01:01:01 +00:00
from __future__ import absolute_import, division, print_function
__metaclass__ = type
2014-09-26 01:01:01 +00:00
2017-08-16 03:16:38 +00:00
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['stableinterface'],
'supported_by': 'core'}
DOCUMENTATION = r'''
2014-09-26 01:01:01 +00:00
---
module: debconf
short_description: Configure a .deb package
description:
- Configure a .deb package using debconf-set-selections.
- Or just query existing selections.
2014-09-26 01:01:01 +00:00
version_added: "1.6"
notes:
- This module requires the command line debconf tools.
- A number of questions have to be answered (depending on the package).
Use 'debconf-show <package>' on any Debian or derivative with the package
installed to see questions/settings available.
- Some distros will always record tasks involving the setting of passwords as changed. This is due to debconf-get-selections masking passwords.
requirements:
- debconf
- debconf-utils
2014-09-26 01:01:01 +00:00
options:
name:
description:
- Name of package to configure.
type: str
2014-09-26 01:01:01 +00:00
required: true
aliases: [ pkg ]
2014-09-26 01:01:01 +00:00
question:
description:
- A debconf configuration setting.
type: str
aliases: [ selection, setting ]
2014-09-26 01:01:01 +00:00
vtype:
description:
2016-06-20 12:58:33 +00:00
- The type of the value supplied.
- C(seen) was added in Ansible 2.2.
type: str
choices: [ boolean, error, multiselect, note, password, seen, select, string, text, title ]
2014-09-26 01:01:01 +00:00
value:
description:
- Value to set the configuration to.
type: str
aliases: [ answer ]
2014-09-26 01:01:01 +00:00
unseen:
description:
- Do not set 'seen' flag when pre-seeding.
type: bool
default: no
author:
- Brian Coca (@bcoca)
2014-09-26 01:01:01 +00:00
'''
EXAMPLES = r'''
- name: Set default locale to fr_FR.UTF-8
debconf:
name: locales
question: locales/default_environment_locale
value: fr_FR.UTF-8
vtype: select
2014-09-26 01:01:01 +00:00
- name: set to generate locales
debconf:
name: locales
question: locales/locales_to_be_generated
value: en_US.UTF-8 UTF-8, fr_FR.UTF-8 UTF-8
vtype: multiselect
2014-09-26 01:01:01 +00:00
- name: Accept oracle license
debconf:
name: oracle-java7-installer
question: shared/accepted-oracle-license-v1-1
value: 'true'
vtype: select
2014-09-26 01:01:01 +00:00
- name: Specifying package you can register/return the list of questions and current values
debconf:
name: tzdata
2014-09-26 01:01:01 +00:00
'''
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import AnsibleModule
2014-09-26 01:01:01 +00:00
def get_selections(module, pkg):
cmd = [module.get_bin_path('debconf-show', True), pkg]
rc, out, err = module.run_command(' '.join(cmd))
if rc != 0:
module.fail_json(msg=err)
selections = {}
for line in out.splitlines():
(key, value) = line.split(':', 1)
selections[key.strip('*').strip()] = value.strip()
2014-09-26 01:01:01 +00:00
return selections
def set_selection(module, pkg, question, vtype, value, unseen):
setsel = module.get_bin_path('debconf-set-selections', True)
cmd = [setsel]
2014-09-26 01:01:01 +00:00
if unseen:
cmd.append('-u')
if vtype == 'boolean':
if value == 'True':
value = 'true'
elif value == 'False':
value = 'false'
data = ' '.join([pkg, question, vtype, value])
return module.run_command(cmd, data=data)
2014-09-26 01:01:01 +00:00
def main():
2014-09-26 01:01:01 +00:00
module = AnsibleModule(
2017-03-23 01:50:28 +00:00
argument_spec=dict(
name=dict(type='str', required=True, aliases=['pkg']),
question=dict(type='str', aliases=['selection', 'setting']),
vtype=dict(type='str', choices=['boolean', 'error', 'multiselect', 'note', 'password', 'seen', 'select', 'string', 'text', 'title']),
value=dict(type='str', aliases=['answer']),
unseen=dict(type='bool'),
2014-09-26 01:01:01 +00:00
),
required_together=(['question', 'vtype', 'value'],),
2014-09-26 01:01:01 +00:00
supports_check_mode=True,
)
# TODO: enable passing array of options and/or debconf file from get-selections dump
pkg = module.params["name"]
2014-09-26 01:01:01 +00:00
question = module.params["question"]
vtype = module.params["vtype"]
value = module.params["value"]
unseen = module.params["unseen"]
2014-09-26 01:01:01 +00:00
prev = get_selections(module, pkg)
changed = False
msg = ""
if question is not None:
if vtype is None or value is None:
module.fail_json(msg="when supplying a question you must supply a valid vtype and value")
# if question doesn't exist, value cannot match
if question not in prev:
2014-09-26 01:01:01 +00:00
changed = True
else:
2019-03-07 16:23:07 +00:00
existing = prev[question]
# ensure we compare booleans supplied to the way debconf sees them (true/false strings)
if vtype == 'boolean':
value = to_text(value).lower()
existing = to_text(prev[question]).lower()
if value != existing:
changed = True
2014-09-26 01:01:01 +00:00
if changed:
if not module.check_mode:
rc, msg, e = set_selection(module, pkg, question, vtype, value, unseen)
if rc:
module.fail_json(msg=e)
curr = {question: value}
2014-09-26 01:01:01 +00:00
if question in prev:
prev = {question: prev[question]}
else:
prev[question] = ''
if module._diff:
after = prev.copy()
after.update(curr)
diff_dict = {'before': prev, 'after': after}
else:
diff_dict = {}
2014-09-26 01:01:01 +00:00
module.exit_json(changed=changed, msg=msg, current=curr, previous=prev, diff=diff_dict)
2014-09-26 01:01:01 +00:00
module.exit_json(changed=changed, msg=msg, current=prev)
if __name__ == '__main__':
main()