Blame roles/ipaserver/library/ipaserver_load_cache.py

Packit 8cb997
# -*- coding: utf-8 -*-
Packit 8cb997
Packit 8cb997
# Authors:
Packit 8cb997
#   Thomas Woerner <twoerner@redhat.com>
Packit 8cb997
#
Packit 8cb997
# Based on ipa-client-install code
Packit 8cb997
#
Packit 8cb997
# Copyright (C) 2017  Red Hat
Packit 8cb997
# see file 'COPYING' for use and warranty information
Packit 8cb997
#
Packit 8cb997
# This program is free software; you can redistribute it and/or modify
Packit 8cb997
# it under the terms of the GNU General Public License as published by
Packit 8cb997
# the Free Software Foundation, either version 3 of the License, or
Packit 8cb997
# (at your option) any later version.
Packit 8cb997
#
Packit 8cb997
# This program is distributed in the hope that it will be useful,
Packit 8cb997
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8cb997
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8cb997
# GNU General Public License for more details.
Packit 8cb997
#
Packit 8cb997
# You should have received a copy of the GNU General Public License
Packit 8cb997
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8cb997
Packit 8cb997
from __future__ import print_function
Packit 8cb997
Packit 8cb997
ANSIBLE_METADATA = {
Packit 8cb997
    'metadata_version': '1.0',
Packit 8cb997
    'supported_by': 'community',
Packit 8cb997
    'status': ['preview'],
Packit 8cb997
}
Packit 8cb997
Packit 8cb997
DOCUMENTATION = '''
Packit 8cb997
---
Packit 8cb997
module: ipaserver_load_cache
Packit 8cb997
short description: Load cache file
Packit 8cb997
description: Load cache file
Packit 8cb997
options:
Packit 8cb997
  dm_password:
Packit 8cb997
    description: Directory Manager password
Packit 8cb997
    required: no
Packit 8cb997
author:
Packit 8cb997
    - Thomas Woerner
Packit 8cb997
'''
Packit 8cb997
Packit 8cb997
EXAMPLES = '''
Packit 8cb997
'''
Packit 8cb997
Packit 8cb997
RETURN = '''
Packit 8cb997
'''
Packit 8cb997
Packit 8cb997
import os
Packit 8cb997
Packit 8cb997
from ansible.module_utils.basic import AnsibleModule
Packit 8cb997
from ansible.module_utils.ansible_ipa_server import (
Packit Service 0f71a7
    setup_logging, options, paths, read_cache
Packit 8cb997
)
Packit 8cb997
Packit 8cb997
Packit 8cb997
def main():
Packit 8cb997
    ansible_module = AnsibleModule(
Packit 8cb997
        argument_spec=dict(
Packit 8cb997
            # basic
Packit 8cb997
            dm_password=dict(required=True, no_log=True),
Packit 8cb997
        ),
Packit 8cb997
    )
Packit 8cb997
Packit 8cb997
    ansible_module._ansible_debug = True
Packit Service 0f71a7
    setup_logging()
Packit 8cb997
Packit 8cb997
    # set values ############################################################
Packit 8cb997
Packit 8cb997
    # basic
Packit 8cb997
    options.dm_password = ansible_module.params.get('dm_password')
Packit 8cb997
Packit 8cb997
    # restore cache #########################################################
Packit 8cb997
Packit 8cb997
    if os.path.isfile(paths.ROOT_IPA_CACHE):
Packit 8cb997
        if options.dm_password is None:
Packit 8cb997
            ansible_module.fail_json(msg="Directory Manager password required")
Packit 8cb997
        try:
Packit 8cb997
            cache_vars = read_cache(options.dm_password)
Packit 8cb997
            options.__dict__.update(cache_vars)
Packit 8cb997
            if cache_vars.get('external_ca', False):
Packit 8cb997
                options.external_ca = False
Packit 8cb997
                options.interactive = False
Packit 8cb997
        except Exception as e:
Packit 8cb997
            ansible_module.fail_json(
Packit 8cb997
                msg="Cannot process the cache file: %s" % str(e))
Packit 8cb997
Packit 8cb997
        kwargs = {"changed": True}
Packit 8cb997
        for name in options.__dict__:
Packit 8cb997
            kwargs[name] = options.__dict__[name]
Packit 8cb997
        ansible_module.exit_json(**kwargs)
Packit 8cb997
Packit 8cb997
    # done ##################################################################
Packit 8cb997
Packit 8cb997
    ansible_module.exit_json(changed=False)
Packit 8cb997
Packit 8cb997
Packit 8cb997
if __name__ == '__main__':
Packit 8cb997
    main()