#!/usr/bin/python # -*- coding: utf-8 -*- """ Ansible module for reconfiguring debian packages. (c) 2013, Sebastien Bocahu 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 . """ DOCUMENTATION = ''' --- module: dpkg_reconfigure short_description: Reconfigure a Debian package. description: - Reconfigure a Debian package using dpkg-reconfigure. version_added: "1.2" notes: - A number of questions has to be answered (depending on the package). Use 'DEBIAN_FRONTED=editor dpkg-reconfigure $pkg' for finding them. options: pkg: description: - Package to configure. required: true default: null aliases: [] answers: description: - Debconf configuration answer(s)/question(s). required: true default: null aliases: [] author: Sebastien Bocahu ''' EXAMPLES = ''' # Set default locale to fr_FR.UTF-8, and generate en_US.UTF-8 as well: dpkg_reconfigure: pkg: locales answers: locales/default_environment_locale: fr_FR.UTF-8 locales/locales_to_be_generated: en_US.UTF-8 UTF-8, fr_FR.UTF-8 UTF-8 # Reconfigure roundcube, using configuration answers stored in a file: dpkg_reconfigure: pkg=roundcube answers='$FILE(/path/dpkg-reconfigure/roundcube)'" ''' import sys import os import pwd import os.path import re import tempfile def get_selections(module, pkg): cmd = [module.get_bin_path('debconf-show', True)] cmd.append(' %s' % pkg) rc, out, err = module.run_command(' '.join(cmd)) if rc == 0: selections = {} for answer in out.split('\n'): item = re.search('^[* ] ([^:]+): (.*)$', answer) if item: value = item.group(2).strip() if value == 'true': value = 'yes' elif value == 'false': value = 'no' selections[ item.group(1).strip() ] = value return selections else: module.fail_json(msg=err) def dpkg_reconfigure(module, pkg, wanted_config): editor_script = [ '#!/bin/sh', 'sed -i "$1" -f - < 1: wanted_config[ item[0].strip() ] = ' '.join(item[1:]) elif len(item) == 1: wanted_config[ item[0].strip() ] = '' for key in wanted_config: value = wanted_config[key] if isinstance(value, bool): if value: wanted_config[key] = 'yes' else: wanted_config[key] = 'no' current_config = get_selections(module, params["pkg"]) already_configured = 1 for answer in wanted_config: if not answer in current_config or current_config[answer] != wanted_config[answer]: already_configured = 0 if already_configured: module.exit_json(changed=False, msg="Already configured") else: rc, msg = dpkg_reconfigure(module, pkg, wanted_config) if not rc: module.fail_json(msg=msg) params['changed'] = True params['msg'] = msg return params def main(): module = AnsibleModule( argument_spec = dict( pkg = dict(required=True), answers = dict(required=True), ) ) results = enforce_state(module, module.params) module.exit_json(**results) # this is magic, see lib/ansible/module_common.py #<> main()