| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2014, Brian Coca <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
module: debconf |
| 12 |
short_description: Configure a .deb package |
| 13 |
description: |
| 14 |
- Configure a .deb package using debconf-set-selections. |
| 15 |
- Or just query existing selections. |
| 16 |
version_added: "1.6" |
| 17 |
extends_documentation_fragment: |
| 18 |
- action_common_attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: full |
| 22 |
diff_mode: |
| 23 |
support: full |
| 24 |
platform: |
| 25 |
support: full |
| 26 |
platforms: debian |
| 27 |
notes: |
| 28 |
- This module requires the command line debconf tools. |
| 29 |
- Several questions have to be answered (depending on the package). |
| 30 |
Use 'debconf-show <package>' on any Debian or derivative with the package |
| 31 |
installed to see questions/settings available. |
| 32 |
- Some distros will always record tasks involving the setting of passwords as changed. This is due to C(debconf-get-selections) masking passwords. |
| 33 |
- It is highly recommended to add C(no_log=True) to the task while handling sensitive information using this module. |
| 34 |
- The M(ansible.builtin.debconf) module does not reconfigure packages, it just updates the debconf database. |
| 35 |
An additional step is needed (typically with C(notify) if debconf makes a change) |
| 36 |
to reconfigure the package and apply the changes. |
| 37 |
C(debconf) is extensively used for pre-seeding configuration prior to installation |
| 38 |
rather than modifying configurations. |
| 39 |
So, while C(dpkg-reconfigure) does use debconf data, it is not always authoritative |
| 40 |
and you may need to check how your package is handled. |
| 41 |
- Also note C(dpkg-reconfigure) is a 3-phase process. It invokes the |
| 42 |
control scripts from the C(/var/lib/dpkg/info) directory with the |
| 43 |
C(<package>.prerm reconfigure <version>), |
| 44 |
C(<package>.config reconfigure <version>) and C(<package>.postinst control <version>) arguments. |
| 45 |
- The main issue is that the C(<package>.config reconfigure) step for many packages |
| 46 |
will first reset the debconf database (overriding changes made by this module) by |
| 47 |
checking the on-disk configuration. If this is the case for your package then |
| 48 |
C(dpkg-reconfigure) will effectively ignore changes made by debconf. |
| 49 |
- However as C(dpkg-reconfigure) only executes the C(<package>.config) step if the file |
| 50 |
exists, it is possible to rename it to C(/var/lib/dpkg/info/<package>.config.ignore) |
| 51 |
before executing C(dpkg-reconfigure -f noninteractive <package>) and then restore it. |
| 52 |
This seems to be compliant with Debian policy for the .config file. |
| 53 |
requirements: |
| 54 |
- debconf |
| 55 |
- debconf-utils |
| 56 |
options: |
| 57 |
name: |
| 58 |
description: |
| 59 |
- Name of package to configure. |
| 60 |
type: str |
| 61 |
required: true |
| 62 |
aliases: [ pkg ] |
| 63 |
question: |
| 64 |
description: |
| 65 |
- A debconf configuration setting. |
| 66 |
type: str |
| 67 |
aliases: [ selection, setting ] |
| 68 |
vtype: |
| 69 |
description: |
| 70 |
- The type of the value supplied. |
| 71 |
- It is highly recommended to add C(no_log=True) to task while specifying O(vtype=password). |
| 72 |
- V(seen) was added in Ansible 2.2. |
| 73 |
- After Ansible 2.17, user can specify C(value) as a list, if C(vtype) is set as V(multiselect). |
| 74 |
type: str |
| 75 |
choices: [ boolean, error, multiselect, note, password, seen, select, string, text, title ] |
| 76 |
value: |
| 77 |
description: |
| 78 |
- Value to set the configuration to. |
| 79 |
- After Ansible 2.17, C(value) is of type C(raw). |
| 80 |
type: raw |
| 81 |
aliases: [ answer ] |
| 82 |
unseen: |
| 83 |
description: |
| 84 |
- Do not set C(seen) flag when pre-seeding. |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
author: |
| 88 |
- Brian Coca (@bcoca) |
| 89 |
""" |
| 90 |
|
| 91 |
EXAMPLES = r""" |
| 92 |
- name: Set default locale to fr_FR.UTF-8 |
| 93 |
ansible.builtin.debconf: |
| 94 |
name: locales |
| 95 |
question: locales/default_environment_locale |
| 96 |
value: fr_FR.UTF-8 |
| 97 |
vtype: select |
| 98 |
|
| 99 |
- name: Set to generate locales |
| 100 |
ansible.builtin.debconf: |
| 101 |
name: locales |
| 102 |
question: locales/locales_to_be_generated |
| 103 |
value: en_US.UTF-8 UTF-8, fr_FR.UTF-8 UTF-8 |
| 104 |
vtype: multiselect |
| 105 |
|
| 106 |
- name: Accept oracle license |
| 107 |
ansible.builtin.debconf: |
| 108 |
name: oracle-java7-installer |
| 109 |
question: shared/accepted-oracle-license-v1-1 |
| 110 |
value: 'true' |
| 111 |
vtype: select |
| 112 |
|
| 113 |
- name: Specifying package you can register/return the list of questions and current values |
| 114 |
ansible.builtin.debconf: |
| 115 |
name: tzdata |
| 116 |
|
| 117 |
- name: Pre-configure tripwire site passphrase |
| 118 |
ansible.builtin.debconf: |
| 119 |
name: tripwire |
| 120 |
question: tripwire/site-passphrase |
| 121 |
value: "{{ site_passphrase }}" |
| 122 |
vtype: password |
| 123 |
no_log: True |
| 124 |
""" |
| 125 |
|
| 126 |
RETURN = r"""#""" |
| 127 |
|
| 128 |
from ansible.module_utils.common.text.converters import to_text, to_native |
| 129 |
from ansible.module_utils.basic import AnsibleModule |
| 130 |
|
| 131 |
|
| 132 |
def get_password_value(module, pkg, question, vtype): |
| 133 |
getsel = module.get_bin_path('debconf-get-selections', True) |
| 134 |
cmd = [getsel] |
| 135 |
rc, out, err = module.run_command(cmd) |
| 136 |
if rc != 0: |
| 137 |
module.fail_json(msg=f"Failed to get the value '{question}' from '{pkg}': {err}") |
| 138 |
|
| 139 |
for line in out.split("\n"): |
| 140 |
if not line.startswith(pkg): |
| 141 |
continue |
| 142 |
|
| 143 |
# line is a collection of tab separated values |
| 144 |
fields = line.split('\t') |
| 145 |
if len(fields) <= 3: |
| 146 |
# No password found, return a blank password |
| 147 |
return '' |
| 148 |
try: |
| 149 |
if fields[1] == question and fields[2] == vtype: |
| 150 |
# If correct question and question type found, return password value |
| 151 |
return fields[3] |
| 152 |
except IndexError: |
| 153 |
# Fail safe |
| 154 |
return '' |
| 155 |
|
| 156 |
|
| 157 |
def get_selections(module, pkg): |
| 158 |
cmd = [module.get_bin_path('debconf-show', True), pkg] |
| 159 |
rc, out, err = module.run_command(' '.join(cmd)) |
| 160 |
|
| 161 |
if rc != 0: |
| 162 |
module.fail_json(msg=err) |
| 163 |
|
| 164 |
selections = {} |
| 165 |
|
| 166 |
for line in out.splitlines(): |
| 167 |
(key, value) = line.split(':', 1) |
| 168 |
selections[key.strip('*').strip()] = value.strip() |
| 169 |
|
| 170 |
return selections |
| 171 |
|
| 172 |
|
| 173 |
def set_selection(module, pkg, question, vtype, value, unseen): |
| 174 |
setsel = module.get_bin_path('debconf-set-selections', True) |
| 175 |
cmd = [setsel] |
| 176 |
if unseen: |
| 177 |
cmd.append('-u') |
| 178 |
|
| 179 |
data = ' '.join([pkg, question, vtype, value]) |
| 180 |
|
| 181 |
return module.run_command(cmd, data=data) |
| 182 |
|
| 183 |
|
| 184 |
def main(): |
| 185 |
module = AnsibleModule( |
| 186 |
argument_spec=dict( |
| 187 |
name=dict(type='str', required=True, aliases=['pkg']), |
| 188 |
question=dict(type='str', aliases=['selection', 'setting']), |
| 189 |
vtype=dict(type='str', choices=['boolean', 'error', 'multiselect', 'note', 'password', 'seen', 'select', 'string', 'text', 'title']), |
| 190 |
value=dict(type='raw', aliases=['answer']), |
| 191 |
unseen=dict(type='bool', default=False), |
| 192 |
), |
| 193 |
required_together=(['question', 'vtype', 'value'],), |
| 194 |
supports_check_mode=True, |
| 195 |
) |
| 196 |
|
| 197 |
# TODO: enable passing array of options and/or debconf file from get-selections dump |
| 198 |
pkg = module.params["name"] |
| 199 |
question = module.params["question"] |
| 200 |
vtype = module.params["vtype"] |
| 201 |
value = module.params["value"] |
| 202 |
unseen = module.params["unseen"] |
| 203 |
|
| 204 |
prev = get_selections(module, pkg) |
| 205 |
|
| 206 |
changed = False |
| 207 |
msg = "" |
| 208 |
|
| 209 |
if question is not None: |
| 210 |
if vtype is None or value is None: |
| 211 |
module.fail_json(msg="when supplying a question you must supply a valid vtype and value") |
| 212 |
|
| 213 |
# ensure we compare booleans supplied to the way debconf sees them (true/false strings) |
| 214 |
if vtype == 'boolean': |
| 215 |
value = to_text(value).lower() |
| 216 |
|
| 217 |
# if question doesn't exist, value cannot match |
| 218 |
if question not in prev: |
| 219 |
changed = True |
| 220 |
else: |
| 221 |
existing = prev[question] |
| 222 |
|
| 223 |
if vtype == 'boolean': |
| 224 |
existing = to_text(prev[question]).lower() |
| 225 |
elif vtype == 'password': |
| 226 |
existing = get_password_value(module, pkg, question, vtype) |
| 227 |
elif vtype == 'multiselect' and isinstance(value, list): |
| 228 |
try: |
| 229 |
value = sorted(value) |
| 230 |
except TypeError as exc: |
| 231 |
module.fail_json(msg="Invalid value provided for 'multiselect': %s" % to_native(exc)) |
| 232 |
existing = sorted([i.strip() for i in existing.split(",")]) |
| 233 |
|
| 234 |
if value != existing: |
| 235 |
changed = True |
| 236 |
|
| 237 |
if changed: |
| 238 |
if not module.check_mode: |
| 239 |
if vtype == 'multiselect' and isinstance(value, list): |
| 240 |
try: |
| 241 |
value = ", ".join(value) |
| 242 |
except TypeError as exc: |
| 243 |
module.fail_json(msg="Invalid value provided for 'multiselect': %s" % to_native(exc)) |
| 244 |
rc, msg, e = set_selection(module, pkg, question, vtype, value, unseen) |
| 245 |
if rc: |
| 246 |
module.fail_json(msg=e) |
| 247 |
|
| 248 |
curr = {question: value} |
| 249 |
if question in prev: |
| 250 |
prev = {question: prev[question]} |
| 251 |
else: |
| 252 |
prev[question] = '' |
| 253 |
|
| 254 |
diff_dict = {} |
| 255 |
if module._diff: |
| 256 |
after = prev.copy() |
| 257 |
after.update(curr) |
| 258 |
diff_dict = {'before': prev, 'after': after} |
| 259 |
|
| 260 |
module.exit_json(changed=changed, msg=msg, current=curr, previous=prev, diff=diff_dict) |
| 261 |
|
| 262 |
module.exit_json(changed=changed, msg=msg, current=prev) |
| 263 |
|
| 264 |
|
| 265 |
if __name__ == '__main__': |
| 266 |
main() |