Fix for setting values using gconftool-2 (#23713)

Miscallenous fixes added to make gconftool-2 work.

Fixes https://github.com/ansible/ansible/issues/23670

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/23741/head
Abhijeet 8 years ago committed by John R Barker
parent a40450d40a
commit 27f776f97d

@ -1,6 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# (c) 2016, Kenneth D. Evensen <kevensen@redhat.com> # (c) 2016, Kenneth D. Evensen <kevensen@redhat.com>
# (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>
# #
# This file is part of Ansible (sort of) # This file is part of Ansible (sort of)
# #
@ -103,9 +104,10 @@ RETURN = '''
... ...
''' '''
from subprocess import Popen, PIPE
from ansible.module_utils.basic import AnsibleModule, BOOLEANS_TRUE from ansible.module_utils.basic import AnsibleModule, BOOLEANS_TRUE
from ansible.module_utils.pycompat24 import get_exception from ansible.module_utils.pycompat24 import get_exception
import subprocess
class GConf2Preference(object): class GConf2Preference(object):
@ -121,7 +123,8 @@ class GConf2Preference(object):
def value_already_set(self): def value_already_set(self):
return False return False
def call(self, call_type): def call(self, call_type, fail_onerr=True):
""" Helper function to perform gconftool-2 operations """
config_source = '' config_source = ''
direct = '' direct = ''
changed = False changed = False
@ -137,34 +140,41 @@ class GConf2Preference(object):
direct = "--direct" direct = "--direct"
# Execute the call # Execute the call
cmd = "gconftool-2 "
try: try:
# If the call is "get", then we don't need as many parameters and # If the call is "get", then we don't need as many parameters and
# we can ignore some # we can ignore some
if call_type == 'get': if call_type == 'get':
process = subprocess.Popen(["gconftool-2 --get " + self.key], cmd += "--get {0}".format(self.key)
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
# Otherwise, we will use all relevant parameters # Otherwise, we will use all relevant parameters
else: elif call_type == 'set':
process = subprocess.Popen(["gconftool-2 " + direct + " " + cmd += "{0} {1} --type {2} --{3} {4} \"{5}\"".format(direct,
config_source + " --type " + config_source,
self.value_type + " --" + self.value_type,
call_type + " " + self.key + " " + call_type,
self.value], stdout=subprocess.PIPE, self.key,
stderr=subprocess.PIPE, shell=True) self.value)
elif call_type == 'unset':
cmd += "--unset {0}".format(self.key)
# Start external command
process = Popen([cmd], stdout=PIPE, stderr=PIPE, shell=True)
# In either case, we will capture the output # In either case, we will capture the output
out = process.stdout.read() out = process.stdout.read()
err = process.stderr.read() err = process.stderr.read()
if len(err) > 0: if len(err) > 0:
self.ansible.fail_json(msg='gconftool-2 failed with error: %s' if fail_onerr:
% (str(err))) self.ansible.fail_json(msg='gconftool-2 failed with '
'error: %s' % (str(err)))
else: else:
changed = True changed = True
except OSError: except OSError:
self.ansible.fail_json(msg='gconftool-2 failed with and exception') exception = get_exception()
self.ansible.fail_json(msg='gconftool-2 failed with exception: '
'%s' % exception)
return changed, out.rstrip() return changed, out.rstrip()
@ -174,18 +184,15 @@ def main():
argument_spec=dict( argument_spec=dict(
key=dict(required=True, default=None, type='str'), key=dict(required=True, default=None, type='str'),
value_type=dict(required=False, value_type=dict(required=False,
choices=['int', 'bool', choices=['int', 'bool', 'float', 'string'],
'float', 'string'], type='str'),
type='str'), value=dict(required=False, default=None, type='str'),
value=dict(required=False, default=None, state=dict(required=True,
type='str'), default=None,
state=dict(required=True, default=None, choices=['present', 'get', 'absent'],
choices=['present', 'get', 'absent'], type='str'),
type='str'), direct=dict(required=False, default=False, type='bool'),
direct=dict(required=False, config_source=dict(required=False, default=None, type='str')
default=False, type='bool'),
config_source=dict(required=False,
default=None, type='str')
), ),
supports_check_mode=True supports_check_mode=True
) )
@ -230,8 +237,8 @@ def main():
# Create a gconf2 preference # Create a gconf2 preference
gconf_pref = GConf2Preference(module, key, value_type, gconf_pref = GConf2Preference(module, key, value_type,
value, direct, config_source) value, direct, config_source)
# Now we get the current value # Now we get the current value, if not found don't fail
_, current_value = gconf_pref.call("get") _, current_value = gconf_pref.call("get", fail_onerr=False)
# Check if the current value equals the value we want to set. If not, make # Check if the current value equals the value we want to set. If not, make
# a change # a change
@ -250,11 +257,12 @@ def main():
else: else:
new_value = current_value new_value = current_value
facts = {} facts = dict(gconftool2={'changed': change,
facts['gconftool2'] = {'changed': change, 'key': key, 'key': key,
'value_type': value_type, 'new_value': new_value, 'value_type': value_type,
'previous_value': current_value, 'new_value': new_value,
'playbook_value': module.params['value']} 'previous_value': current_value,
'playbook_value': module.params['value']})
module.exit_json(changed=change, ansible_facts=facts) module.exit_json(changed=change, ansible_facts=facts)

Loading…
Cancel
Save