Created
August 26, 2015 05:21
-
-
Save mgedmin/b38c74e2d25cb4f47908 to your computer and use it in GitHub Desktop.
Ansible 1.9.x module for git config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# action_plugins/gitconfig.py | |
# Action plugin for Ansible 1.9.2, since that was the only way I could find to implement --diff support for a module | |
from ansible.runner.action_plugins.normal import ActionModule as _ActionModule | |
class ActionModule(_ActionModule): | |
'''Change git settings''' | |
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): | |
result = super(ActionModule, self).run(conn, tmp, module_name, module_args, inject, complex_args=complex_args, **kwargs) | |
if 'diff' in result.result: | |
result.diff = result.result['diff'] | |
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess | |
DOCUMENTATION = ''' | |
--- | |
module: gitconfig | |
short_description: changes git configuration parameters | |
description: | |
- The M(gitconfig) module changes git configuration by invoking 'git config'. | |
This is needed if you don't want to use M(template) for the entire git | |
config file (e.g. because you need to change just C(user.email) in | |
/etc/.git/config). Solutions involving M(command) are cumbersone or | |
don't work correctly in check mode. | |
options: | |
name: | |
description: | |
- the name of the setting | |
required: true | |
default: null | |
value: | |
description: | |
- the value for that setting | |
required: true | |
default: null | |
repo: | |
description: | |
- path to the git repository for setting local options (if not set, | |
will change global options) | |
required: false | |
default: null | |
author: | |
- Marius Gedminas <[email protected]> | |
''' | |
EXAMPLES = ''' | |
# set some settings in ~/.gitconfig | |
- gitconfig: name=alias.ci value=commit | |
- gitconfig: name=alias.st value=status | |
# make etckeeper not complain when invoked by cron | |
- gitconfig: name=user.email value="root@{{ ansible_fqdn }}" repo=/etc | |
''' | |
def run(args, module, rc_ok=(0, )): | |
try: | |
cmd = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
out, err = cmd.communicate() | |
rc = cmd.returncode | |
except (OSError, IOError) as e: | |
module.fail_json(rc=e.errno, msg=str(e), cmd=args) | |
if rc not in rc_ok or err: | |
module.fail_json(rc=rc, msg=err, cmd=args) | |
return out | |
def main(): | |
module = AnsibleModule( | |
argument_spec=dict( | |
name=dict(required=True), | |
value=dict(required=True), | |
repo=dict(), | |
), | |
supports_check_mode=True, | |
) | |
name = module.params['name'] | |
value = module.params['value'].rstrip() | |
repo = module.params['repo'] | |
args = ['git', 'config'] | |
if repo: | |
try: | |
os.chdir(repo) | |
except OSerror as e: | |
module.fail_json(rc=e.errno, msg=str(e), cmd=['cd', repo]) | |
args += ['--local'] | |
else: | |
args += ['--global'] | |
args += [name] | |
old_value = run(args, module, rc_ok=(0, 1)).rstrip() | |
if value == old_value: | |
module.exit_json( | |
msg="", | |
changed=False, | |
) | |
if not module.check_mode: | |
run(args + [value], module) | |
module.exit_json( | |
msg="setting changed", | |
diff=dict( | |
before_header=' '.join(args), | |
after_header=' '.join(args), | |
before=old_value + '\n', | |
after=value + '\n'), | |
changed=True, | |
) | |
from ansible.module_utils.basic import * # noqa | |
if __name__ == '__main__': | |
main() |
It is the GPL, yes. Version 3 to be exact.
Hi there,
I wanted you to know that I tidied up this module, updated it to work with Ansible 2.0, and submitted a pull request to the ansible-modules-extras repository to have it included within the main Ansible distribution: ansible/ansible-modules-extras#1945
Woohoo, it's been accepted (quite a while back!): https://docs.ansible.com/ansible/latest/modules/git_config_module.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Licence: whatever Ansible is using (GPL, I think).