debconf 5.07 KB
Newer Older
Brian Coca committed
1 2 3 4
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
5
Ansible module to configure .deb packages.
Brian Coca committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
(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
27
short_description: Configure a .deb package
Brian Coca committed
28
description:
29
     - Configure a .deb package using debconf-set-selections. Or just query
30
       existing selections.
Brian Coca committed
31
version_added: "1.6"
Brian Coca committed
32
notes:
33
    - This module requires the command line debconf tools.
Brian Coca committed
34
    - A number of questions have to be answered (depending on the package).
35
      Use 'debconf-show <package>' on any Debian or derivative with the package
Brian Coca committed
36
      installed to see questions/settings available.
37
requirements: [ debconf, debconf-utils ]
Brian Coca committed
38 39 40 41 42 43 44
options:
  name:
    description:
      - Name of package to configure.
    required: true
    default: null
    aliases: ['pkg']
45
  question:
Brian Coca committed
46 47 48 49
    description:
      - A debconf configuration setting
    required: false
    default: null
50
    aliases: ['setting', 'selection']
Brian Coca committed
51
  vtype:
Brian Coca committed
52
    description:
53
      - The type of the value supplied
Brian Coca committed
54 55 56 57 58 59 60 61 62
    required: false
    default: null
    choices: [string, boolean, select, multiselect, note, text, password, title]
    aliases: []
  value:
    description:
      -  Value to set the configuration to
    required: false
    default: null
Brian Coca committed
63
    aliases: ['answer']
Brian Coca committed
64 65
  unseen:
    description:
66
      - Do not set 'seen' flag when pre-seeding
Brian Coca committed
67 68 69 70 71 72 73 74 75
    required: false
    default: False
    aliases: []
author: Brian Coca

'''

EXAMPLES = '''
# Set default locale to fr_FR.UTF-8
76
debconf: name=locales question='locales/default_environment_locale' value=fr_FR.UTF-8 vtype='select'
Brian Coca committed
77 78

# set to generate locales:
79
debconf: name=locales question='locales/locales_to_be_generated'  value='en_US.UTF-8 UTF-8, fr_FR.UTF-8 UTF-8' vtype='multiselect'
Brian Coca committed
80 81

# Accept oracle license
82
debconf: name='oracle-java7-installer' question='shared/accepted-oracle-license-v1-1' value='true' vtype='select'
83 84 85

# Specifying package you can register/return the list of questions and current values
debconf: name='tzdata'
Brian Coca committed
86 87
'''

Michael DeHaan committed
88 89
import pipes

Brian Coca committed
90 91 92 93
def get_selections(module, pkg):
    cmd = [module.get_bin_path('debconf-show', True), pkg]
    rc, out, err = module.run_command(' '.join(cmd))

Brian Coca committed
94
    if rc != 0:
Brian Coca committed
95 96
        module.fail_json(msg=err)

Brian Coca committed
97 98 99
    selections = {}

    for line in out.splitlines():
100
        (key, value) = line.split(':', 1)
Brian Coca committed
101 102 103 104
        selections[ key.strip('*').strip() ] = value.strip()

    return selections

Brian Coca committed
105

Brian Coca committed
106
def set_selection(module, pkg, question, vtype, value, unseen):
Brian Coca committed
107

108
    data = ' '.join([ question, vtype, value ])
Brian Coca committed
109 110

    setsel = module.get_bin_path('debconf-set-selections', True)
111
    cmd = ["echo %s %s |" % (pipes.quote(pkg), pipes.quote(data)), setsel]
Brian Coca committed
112 113 114
    if unseen:
        cmd.append('-u')

Michael DeHaan committed
115
    return module.run_command(' '.join(cmd), use_unsafe_shell=True)
Brian Coca committed
116 117 118 119 120 121

def main():

    module = AnsibleModule(
        argument_spec = dict(
           name = dict(required=True, aliases=['pkg'], type='str'),
122
           question = dict(required=False, aliases=['setting', 'selection'], type='str'),
Brian Coca committed
123
           vtype = dict(required=False, type='str', choices=['string', 'boolean', 'select',  'multiselect', 'note', 'text', 'password', 'title']),
Brian Coca committed
124 125 126
           value= dict(required=False, type='str'),
           unseen = dict(required=False, type='bool'),
        ),
127
        required_together = ( ['question','vtype', 'value'],),
Brian Coca committed
128 129 130
        supports_check_mode=True,
    )

131
    #TODO: enable passing array of options and/or debconf file from get-selections dump
Brian Coca committed
132
    pkg      = module.params["name"]
133
    question = module.params["question"]
134
    vtype    = module.params["vtype"]
Brian Coca committed
135 136 137 138 139 140 141 142 143 144
    value    = module.params["value"]
    unseen   = module.params["unseen"]

    prev = get_selections(module, pkg)
    diff = ''

    changed = False
    msg = ""

    if question is not None:
145
        if vtype is None or value is None:
146
            module.fail_json(msg="when supplying a question you must supply a valid vtype and value")
147

Brian Coca committed
148 149 150 151 152
        if not question in prev or prev[question] != value:
            changed = True

    if changed:
        if not module.check_mode:
Brian Coca committed
153
            rc, msg, e = set_selection(module, pkg, question, vtype, value, unseen)
Brian Coca committed
154 155
            if rc:
                module.fail_json(msg=e)
156 157 158 159 160 161

        curr = { question: value }
        if question in prev:
            prev = {question: prev[question]}
        else:
            prev[question] = ''
Brian Coca committed
162 163 164 165 166

        module.exit_json(changed=changed, msg=msg, current=curr, previous=prev)

    module.exit_json(changed=changed, msg=msg, current=prev)

Brian Coca committed
167 168
# import module snippets
from ansible.module_utils.basic import *
169 170

main()