2013-12-19 22:54:02 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
2013-12-23 19:35:27 +01:00
|
|
|
Ansible module to configure .deb packages.
|
2013-12-19 22:54:02 +01:00
|
|
|
(c) 2014, Brian Coca <briancoca+ansible@gmail.com>
|
|
|
|
|
|
|
|
This file is part of Ansible
|
|
|
|
|
|
|
|
Ansible is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Ansible is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: debconf
|
2013-12-23 19:35:27 +01:00
|
|
|
short_description: Configure a .deb package
|
2013-12-19 22:54:02 +01:00
|
|
|
description:
|
2014-02-10 02:47:01 +01:00
|
|
|
- Configure a .deb package using debconf-set-selections. Or just query
|
2014-03-03 19:48:44 +01:00
|
|
|
existing selections.
|
2014-03-02 04:28:51 +01:00
|
|
|
version_added: "1.6"
|
2013-12-19 22:54:02 +01:00
|
|
|
notes:
|
2014-02-09 23:46:18 +01:00
|
|
|
- This module requires the command line debconf tools.
|
2013-12-19 22:54:02 +01:00
|
|
|
- A number of questions have to be answered (depending on the package).
|
2013-12-23 19:35:27 +01:00
|
|
|
Use 'debconf-show <package>' on any Debian or derivative with the package
|
2013-12-19 22:54:02 +01:00
|
|
|
installed to see questions/settings available.
|
2014-12-31 23:10:18 +01:00
|
|
|
- Some distros will always record tasks involving the setting of passwords as changed. This is due to debconf-get-selections masking passwords.
|
2014-04-01 22:51:12 +02:00
|
|
|
requirements: [ debconf, debconf-utils ]
|
2013-12-19 22:54:02 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of package to configure.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: ['pkg']
|
2014-02-09 23:46:18 +01:00
|
|
|
question:
|
2013-12-19 22:54:02 +01:00
|
|
|
description:
|
|
|
|
- A debconf configuration setting
|
|
|
|
required: false
|
|
|
|
default: null
|
2014-02-09 23:46:18 +01:00
|
|
|
aliases: ['setting', 'selection']
|
2014-02-06 00:08:01 +01:00
|
|
|
vtype:
|
2013-12-19 22:54:02 +01:00
|
|
|
description:
|
2016-06-20 14:58:33 +02:00
|
|
|
- The type of the value supplied.
|
|
|
|
- C(seen) was added in 2.2.
|
2013-12-19 22:54:02 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
2016-06-20 14:58:33 +02:00
|
|
|
choices: [string, password, boolean, select, multiselect, note, error, title, text, seen]
|
2013-12-19 22:54:02 +01:00
|
|
|
value:
|
|
|
|
description:
|
|
|
|
- Value to set the configuration to
|
|
|
|
required: false
|
|
|
|
default: null
|
2014-02-06 00:08:01 +01:00
|
|
|
aliases: ['answer']
|
2013-12-19 22:54:02 +01:00
|
|
|
unseen:
|
|
|
|
description:
|
2013-12-23 19:35:27 +01:00
|
|
|
- Do not set 'seen' flag when pre-seeding
|
2013-12-19 22:54:02 +01:00
|
|
|
required: false
|
|
|
|
default: False
|
2015-05-14 06:49:57 +02:00
|
|
|
author: "Brian Coca (@bcoca)"
|
2013-12-19 22:54:02 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Set default locale to fr_FR.UTF-8
|
2014-04-23 22:51:01 +02:00
|
|
|
debconf: name=locales question='locales/default_environment_locale' value=fr_FR.UTF-8 vtype='select'
|
2013-12-19 22:54:02 +01:00
|
|
|
|
|
|
|
# set to generate locales:
|
2014-04-23 22:51:01 +02:00
|
|
|
debconf: name=locales question='locales/locales_to_be_generated' value='en_US.UTF-8 UTF-8, fr_FR.UTF-8 UTF-8' vtype='multiselect'
|
2013-12-19 22:54:02 +01:00
|
|
|
|
|
|
|
# Accept oracle license
|
2014-02-06 00:28:17 +01:00
|
|
|
debconf: name='oracle-java7-installer' question='shared/accepted-oracle-license-v1-1' value='true' vtype='select'
|
2014-02-10 02:47:01 +01:00
|
|
|
|
|
|
|
# Specifying package you can register/return the list of questions and current values
|
|
|
|
debconf: name='tzdata'
|
2013-12-19 22:54:02 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
def get_selections(module, pkg):
|
|
|
|
cmd = [module.get_bin_path('debconf-show', True), pkg]
|
|
|
|
rc, out, err = module.run_command(' '.join(cmd))
|
|
|
|
|
2014-02-06 00:08:01 +01:00
|
|
|
if rc != 0:
|
2013-12-19 22:54:02 +01:00
|
|
|
module.fail_json(msg=err)
|
|
|
|
|
2014-02-06 00:08:01 +01:00
|
|
|
selections = {}
|
|
|
|
|
|
|
|
for line in out.splitlines():
|
2014-03-23 03:35:10 +01:00
|
|
|
(key, value) = line.split(':', 1)
|
2014-02-06 00:08:01 +01:00
|
|
|
selections[ key.strip('*').strip() ] = value.strip()
|
|
|
|
|
|
|
|
return selections
|
|
|
|
|
2013-12-19 22:54:02 +01:00
|
|
|
|
2014-02-06 00:08:01 +01:00
|
|
|
def set_selection(module, pkg, question, vtype, value, unseen):
|
2013-12-19 22:54:02 +01:00
|
|
|
|
|
|
|
setsel = module.get_bin_path('debconf-set-selections', True)
|
2015-01-12 18:52:25 +01:00
|
|
|
cmd = [setsel]
|
2013-12-19 22:54:02 +01:00
|
|
|
if unseen:
|
|
|
|
cmd.append('-u')
|
|
|
|
|
2016-08-17 16:32:49 +02:00
|
|
|
if vtype == 'boolean':
|
|
|
|
if value == 'True':
|
|
|
|
value = 'true'
|
|
|
|
elif value == 'False':
|
|
|
|
value = 'false'
|
2015-01-12 18:52:25 +01:00
|
|
|
data = ' '.join([pkg, question, vtype, value])
|
|
|
|
|
|
|
|
return module.run_command(cmd, data=data)
|
2013-12-19 22:54:02 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
name = dict(required=True, aliases=['pkg'], type='str'),
|
2014-02-09 23:46:18 +01:00
|
|
|
question = dict(required=False, aliases=['setting', 'selection'], type='str'),
|
2016-06-20 14:58:33 +02:00
|
|
|
vtype = dict(required=False, type='str', choices=['string', 'password', 'boolean', 'select', 'multiselect', 'note', 'error', 'title', 'text', 'seen']),
|
2016-06-22 14:53:16 +02:00
|
|
|
value = dict(required=False, type='str', aliases=['answer']),
|
2013-12-19 22:54:02 +01:00
|
|
|
unseen = dict(required=False, type='bool'),
|
|
|
|
),
|
2014-02-10 02:47:01 +01:00
|
|
|
required_together = ( ['question','vtype', 'value'],),
|
2013-12-19 22:54:02 +01:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
2014-04-01 22:51:12 +02:00
|
|
|
#TODO: enable passing array of options and/or debconf file from get-selections dump
|
2013-12-19 22:54:02 +01:00
|
|
|
pkg = module.params["name"]
|
2014-02-09 23:46:18 +01:00
|
|
|
question = module.params["question"]
|
2014-04-01 22:51:12 +02:00
|
|
|
vtype = module.params["vtype"]
|
2013-12-19 22:54:02 +01:00
|
|
|
value = module.params["value"]
|
|
|
|
unseen = module.params["unseen"]
|
|
|
|
|
|
|
|
prev = get_selections(module, pkg)
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
msg = ""
|
|
|
|
|
|
|
|
if question is not None:
|
2014-02-10 02:47:01 +01:00
|
|
|
if vtype is None or value is None:
|
2014-04-01 22:51:12 +02:00
|
|
|
module.fail_json(msg="when supplying a question you must supply a valid vtype and value")
|
2014-02-10 02:47:01 +01:00
|
|
|
|
2013-12-19 22:54:02 +01:00
|
|
|
if not question in prev or prev[question] != value:
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if changed:
|
|
|
|
if not module.check_mode:
|
2014-02-06 00:08:01 +01:00
|
|
|
rc, msg, e = set_selection(module, pkg, question, vtype, value, unseen)
|
2013-12-19 22:54:02 +01:00
|
|
|
if rc:
|
|
|
|
module.fail_json(msg=e)
|
2013-12-20 18:50:06 +01:00
|
|
|
|
|
|
|
curr = { question: value }
|
|
|
|
if question in prev:
|
|
|
|
prev = {question: prev[question]}
|
|
|
|
else:
|
|
|
|
prev[question] = ''
|
2016-09-05 22:06:18 +02:00
|
|
|
if module._diff:
|
|
|
|
after = prev.copy()
|
|
|
|
after.update(curr)
|
2016-09-12 10:25:28 +02:00
|
|
|
diff_dict = {'before': prev, 'after': after}
|
2016-09-05 22:06:18 +02:00
|
|
|
else:
|
|
|
|
diff_dict = {}
|
2013-12-19 22:54:02 +01:00
|
|
|
|
2016-09-05 22:06:18 +02:00
|
|
|
module.exit_json(changed=changed, msg=msg, current=curr, previous=prev, diff=diff_dict)
|
2013-12-19 22:54:02 +01:00
|
|
|
|
|
|
|
module.exit_json(changed=changed, msg=msg, current=prev)
|
|
|
|
|
2014-02-06 00:08:01 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2014-02-06 17:59:37 +01:00
|
|
|
|
|
|
|
main()
|