|
|
@ -101,13 +101,6 @@ requirements:
|
|
|
|
- "wipefs"
|
|
|
|
- "wipefs"
|
|
|
|
- "lsblk"
|
|
|
|
- "lsblk"
|
|
|
|
|
|
|
|
|
|
|
|
notes:
|
|
|
|
|
|
|
|
- "This module does not support check mode. The reason being that
|
|
|
|
|
|
|
|
while it is possible to chain several operations together
|
|
|
|
|
|
|
|
(e.g. 'create' and 'open'), the latter usually depends on changes
|
|
|
|
|
|
|
|
to the system done by the previous one. (LUKS cannot be opened,
|
|
|
|
|
|
|
|
when it does not exist.)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
author:
|
|
|
|
author:
|
|
|
|
"Jan Pokorny (@japokorn)"
|
|
|
|
"Jan Pokorny (@japokorn)"
|
|
|
|
'''
|
|
|
|
'''
|
|
|
@ -172,7 +165,9 @@ name:
|
|
|
|
sample: "luks-c1da9a58-2fde-4256-9d9f-6ab008b4dd1b"
|
|
|
|
sample: "luks-c1da9a58-2fde-4256-9d9f-6ab008b4dd1b"
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
|
|
|
|
import stat
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
@ -249,7 +244,7 @@ class CryptHandler(Handler):
|
|
|
|
return device
|
|
|
|
return device
|
|
|
|
|
|
|
|
|
|
|
|
def is_luks(self, device):
|
|
|
|
def is_luks(self, device):
|
|
|
|
''' check if the LUKS device does exist
|
|
|
|
''' check if the LUKS container does exist
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
result = self._run_command([self._cryptsetup_bin, 'isLuks', device])
|
|
|
|
result = self._run_command([self._cryptsetup_bin, 'isLuks', device])
|
|
|
|
return result[RETURN_CODE] == 0
|
|
|
|
return result[RETURN_CODE] == 0
|
|
|
@ -464,7 +459,16 @@ def run_module():
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=module_args,
|
|
|
|
module = AnsibleModule(argument_spec=module_args,
|
|
|
|
supports_check_mode=False)
|
|
|
|
supports_check_mode=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if module.params['device'] is not None:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
statinfo = os.stat(module.params['device'])
|
|
|
|
|
|
|
|
mode = statinfo.st_mode
|
|
|
|
|
|
|
|
if not stat.S_ISBLK(mode) and not stat.S_ISCHR(mode):
|
|
|
|
|
|
|
|
raise Exception('{0} is not a device'.format(module.params['device']))
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
crypt = CryptHandler(module)
|
|
|
|
crypt = CryptHandler(module)
|
|
|
|
conditions = ConditionsHandler(module, crypt)
|
|
|
|
conditions = ConditionsHandler(module, crypt)
|
|
|
@ -474,12 +478,15 @@ def run_module():
|
|
|
|
|
|
|
|
|
|
|
|
# luks create
|
|
|
|
# luks create
|
|
|
|
if conditions.luks_create():
|
|
|
|
if conditions.luks_create():
|
|
|
|
try:
|
|
|
|
if not module.check_mode:
|
|
|
|
crypt.run_luks_create(module.params['device'],
|
|
|
|
try:
|
|
|
|
module.params['keyfile'])
|
|
|
|
crypt.run_luks_create(module.params['device'],
|
|
|
|
except ValueError as e:
|
|
|
|
module.params['keyfile'])
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
result['changed'] = True
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
# luks open
|
|
|
|
# luks open
|
|
|
|
|
|
|
|
|
|
|
@ -494,14 +501,17 @@ def run_module():
|
|
|
|
name = crypt.generate_luks_name(module.params['device'])
|
|
|
|
name = crypt.generate_luks_name(module.params['device'])
|
|
|
|
except ValueError as e:
|
|
|
|
except ValueError as e:
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
try:
|
|
|
|
if not module.check_mode:
|
|
|
|
crypt.run_luks_open(module.params['device'],
|
|
|
|
try:
|
|
|
|
module.params['keyfile'],
|
|
|
|
crypt.run_luks_open(module.params['device'],
|
|
|
|
name)
|
|
|
|
module.params['keyfile'],
|
|
|
|
except ValueError as e:
|
|
|
|
name)
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
result['name'] = name
|
|
|
|
result['name'] = name
|
|
|
|
result['changed'] = True
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
# luks close
|
|
|
|
# luks close
|
|
|
|
if conditions.luks_close():
|
|
|
|
if conditions.luks_close():
|
|
|
@ -513,39 +523,51 @@ def run_module():
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
name = module.params['name']
|
|
|
|
name = module.params['name']
|
|
|
|
try:
|
|
|
|
if not module.check_mode:
|
|
|
|
crypt.run_luks_close(name)
|
|
|
|
try:
|
|
|
|
except ValueError as e:
|
|
|
|
crypt.run_luks_close(name)
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
result['changed'] = True
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
# luks add key
|
|
|
|
# luks add key
|
|
|
|
if conditions.luks_add_key():
|
|
|
|
if conditions.luks_add_key():
|
|
|
|
try:
|
|
|
|
if not module.check_mode:
|
|
|
|
crypt.run_luks_add_key(module.params['device'],
|
|
|
|
try:
|
|
|
|
module.params['keyfile'],
|
|
|
|
crypt.run_luks_add_key(module.params['device'],
|
|
|
|
module.params['new_keyfile'])
|
|
|
|
module.params['keyfile'],
|
|
|
|
except ValueError as e:
|
|
|
|
module.params['new_keyfile'])
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
result['changed'] = True
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
# luks remove key
|
|
|
|
# luks remove key
|
|
|
|
if conditions.luks_remove_key():
|
|
|
|
if conditions.luks_remove_key():
|
|
|
|
try:
|
|
|
|
if not module.check_mode:
|
|
|
|
crypt.run_luks_remove_key(module.params['device'],
|
|
|
|
try:
|
|
|
|
module.params['remove_keyfile'],
|
|
|
|
crypt.run_luks_remove_key(module.params['device'],
|
|
|
|
force_remove_last_key=module.params['force_remove_last_key'])
|
|
|
|
module.params['remove_keyfile'],
|
|
|
|
except ValueError as e:
|
|
|
|
force_remove_last_key=module.params['force_remove_last_key'])
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
result['changed'] = True
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
# luks remove
|
|
|
|
# luks remove
|
|
|
|
if conditions.luks_remove():
|
|
|
|
if conditions.luks_remove():
|
|
|
|
try:
|
|
|
|
if not module.check_mode:
|
|
|
|
crypt.run_luks_remove(module.params['device'])
|
|
|
|
try:
|
|
|
|
except ValueError as e:
|
|
|
|
crypt.run_luks_remove(module.params['device'])
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
|
|
module.fail_json(msg="luks_device error: %s" % e)
|
|
|
|
result['changed'] = True
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
# Success - return result
|
|
|
|
# Success - return result
|
|
|
|
module.exit_json(**result)
|
|
|
|
module.exit_json(**result)
|
|
|
|