2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2015, Marius Gedminas <marius@pov.lt>
|
|
|
|
# Copyright (c) 2016, Matthew Gamble <git@matthewgamble.net>
|
2020-03-09 10:11:07 +01:00
|
|
|
#
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: git_config
|
|
|
|
author:
|
|
|
|
- Matthew Gamble (@djmattyg007)
|
|
|
|
- Marius Gedminas (@mgedmin)
|
|
|
|
requirements: ['git']
|
|
|
|
short_description: Read and write git configuration
|
|
|
|
description:
|
2023-11-29 08:37:50 +01:00
|
|
|
- The M(community.general.git_config) module changes git configuration by invoking C(git config).
|
2023-02-20 17:26:28 +01:00
|
|
|
This is needed if you do not want to use M(ansible.builtin.template) for the entire git
|
|
|
|
config file (for example because you need to change just C(user.email) in
|
2020-06-29 14:59:15 +02:00
|
|
|
/etc/.git/config). Solutions involving M(ansible.builtin.command) are cumbersome or
|
2023-02-20 17:26:28 +01:00
|
|
|
do not work correctly in check mode.
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
list_all:
|
|
|
|
description:
|
2023-06-15 15:46:44 +02:00
|
|
|
- List all settings (optionally limited to a given O(scope)).
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2022-08-24 19:59:56 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of the setting. If no value is supplied, the value will
|
|
|
|
be read from the config if it has been set.
|
2021-02-25 09:39:48 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
repo:
|
|
|
|
description:
|
|
|
|
- Path to a git repository for reading and writing values from a
|
|
|
|
specific repo.
|
2021-02-25 09:39:48 +01:00
|
|
|
type: path
|
2020-12-22 16:17:24 +01:00
|
|
|
file:
|
|
|
|
description:
|
2023-06-15 15:46:44 +02:00
|
|
|
- Path to an adhoc git configuration file to be managed using the V(file) scope.
|
2020-12-22 16:17:24 +01:00
|
|
|
type: path
|
|
|
|
version_added: 2.0.0
|
2020-03-09 10:11:07 +01:00
|
|
|
scope:
|
|
|
|
description:
|
2020-12-22 16:17:24 +01:00
|
|
|
- Specify which scope to read/set values from.
|
|
|
|
- This is required when setting config values.
|
2023-06-15 15:46:44 +02:00
|
|
|
- If this is set to V(local), you must also specify the O(repo) parameter.
|
|
|
|
- If this is set to V(file), you must also specify the O(file) parameter.
|
|
|
|
- It defaults to system only when not using O(list_all=true).
|
2020-12-22 16:17:24 +01:00
|
|
|
choices: [ "file", "local", "global", "system" ]
|
2021-02-25 09:39:48 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- "Indicates the setting should be set/unset.
|
2023-06-15 15:46:44 +02:00
|
|
|
This parameter has higher precedence than O(value) parameter:
|
|
|
|
when O(state=absent) and O(value) is defined, O(value) is discarded."
|
2020-03-09 10:11:07 +01:00
|
|
|
choices: [ 'present', 'absent' ]
|
|
|
|
default: 'present'
|
2021-02-25 09:39:48 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
value:
|
|
|
|
description:
|
|
|
|
- When specifying the name of a single setting, supply a value to
|
|
|
|
set that setting to the given value.
|
2021-02-25 09:39:48 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Add a setting to ~/.gitconfig
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: alias.ci
|
|
|
|
scope: global
|
|
|
|
value: commit
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Add a setting to ~/.gitconfig
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: alias.st
|
|
|
|
scope: global
|
|
|
|
value: status
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Remove a setting from ~/.gitconfig
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: alias.ci
|
|
|
|
scope: global
|
|
|
|
state: absent
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Add a setting to ~/.gitconfig
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: core.editor
|
|
|
|
scope: global
|
|
|
|
value: vim
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Add a setting system-wide
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-05-15 12:27:06 +02:00
|
|
|
name: alias.remotev
|
|
|
|
scope: system
|
|
|
|
value: remote -v
|
|
|
|
|
|
|
|
- name: Add a setting to a system scope (default)
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: alias.diffc
|
|
|
|
value: diff --cached
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Add a setting to a system scope (default)
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: color.ui
|
|
|
|
value: auto
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Make etckeeper not complaining when it is invoked by cron
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: user.email
|
|
|
|
repo: /etc
|
|
|
|
scope: local
|
|
|
|
value: 'root@{{ ansible_fqdn }}'
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Read individual values from git config
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: alias.ci
|
|
|
|
scope: global
|
|
|
|
|
2022-08-24 19:59:56 +02:00
|
|
|
- name: Scope system is also assumed when reading values, unless list_all=true
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: alias.diffc
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Read all values from git config
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2022-08-24 19:59:56 +02:00
|
|
|
list_all: true
|
2020-03-09 10:11:07 +01:00
|
|
|
scope: global
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: When list_all is yes and no scope is specified, you get configuration from all scopes
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2022-08-24 19:59:56 +02:00
|
|
|
list_all: true
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Specify a repository to include local settings
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.git_config:
|
2022-08-24 19:59:56 +02:00
|
|
|
list_all: true
|
2020-03-09 10:11:07 +01:00
|
|
|
repo: /path/to/repo.git
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
---
|
|
|
|
config_value:
|
2023-06-15 15:46:44 +02:00
|
|
|
description: When O(list_all=false) and value is not set, a string containing the value of the setting in name
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: success
|
|
|
|
type: str
|
|
|
|
sample: "vim"
|
|
|
|
|
|
|
|
config_values:
|
2023-06-15 15:46:44 +02:00
|
|
|
description: When O(list_all=true), a dict containing key/value pairs of multiple configuration settings
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: success
|
|
|
|
type: dict
|
|
|
|
sample:
|
|
|
|
core.editor: "vim"
|
|
|
|
color.ui: "auto"
|
|
|
|
alias.diffc: "diff --cached"
|
|
|
|
alias.remotev: "remote -v"
|
|
|
|
'''
|
2020-12-21 12:08:48 +01:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
list_all=dict(required=False, type='bool', default=False),
|
|
|
|
name=dict(type='str'),
|
|
|
|
repo=dict(type='path'),
|
2020-12-22 16:17:24 +01:00
|
|
|
file=dict(type='path'),
|
|
|
|
scope=dict(required=False, type='str', choices=['file', 'local', 'global', 'system']),
|
2020-03-09 10:11:07 +01:00
|
|
|
state=dict(required=False, type='str', default='present', choices=['present', 'absent']),
|
2020-12-22 16:17:24 +01:00
|
|
|
value=dict(required=False),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
mutually_exclusive=[['list_all', 'name'], ['list_all', 'value'], ['list_all', 'state']],
|
2020-12-22 16:17:24 +01:00
|
|
|
required_if=[
|
|
|
|
('scope', 'local', ['repo']),
|
|
|
|
('scope', 'file', ['file'])
|
|
|
|
],
|
2020-03-09 10:11:07 +01:00
|
|
|
required_one_of=[['list_all', 'name']],
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
git_path = module.get_bin_path('git', True)
|
|
|
|
|
|
|
|
params = module.params
|
|
|
|
# We check error message for a pattern, so we need to make sure the messages appear in the form we're expecting.
|
|
|
|
# Set the locale to C to ensure consistent messages.
|
|
|
|
module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C', LC_CTYPE='C')
|
|
|
|
|
|
|
|
if params['name']:
|
|
|
|
name = params['name']
|
|
|
|
else:
|
|
|
|
name = None
|
|
|
|
|
|
|
|
if params['scope']:
|
|
|
|
scope = params['scope']
|
|
|
|
elif params['list_all']:
|
|
|
|
scope = None
|
|
|
|
else:
|
|
|
|
scope = 'system'
|
|
|
|
|
|
|
|
if params['state'] == 'absent':
|
|
|
|
unset = 'unset'
|
|
|
|
params['value'] = None
|
|
|
|
else:
|
|
|
|
unset = None
|
|
|
|
|
|
|
|
if params['value']:
|
|
|
|
new_value = params['value']
|
|
|
|
else:
|
|
|
|
new_value = None
|
|
|
|
|
|
|
|
args = [git_path, "config", "--includes"]
|
|
|
|
if params['list_all']:
|
|
|
|
args.append('-l')
|
2020-12-22 16:17:24 +01:00
|
|
|
if scope == 'file':
|
|
|
|
args.append('-f')
|
|
|
|
args.append(params['file'])
|
2021-03-30 08:24:08 +02:00
|
|
|
elif scope:
|
|
|
|
args.append("--" + scope)
|
|
|
|
if name:
|
|
|
|
args.append(name)
|
2020-12-22 16:17:24 +01:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
if scope == 'local':
|
|
|
|
dir = params['repo']
|
|
|
|
elif params['list_all'] and params['repo']:
|
|
|
|
# Include local settings from a specific repo when listing all available settings
|
|
|
|
dir = params['repo']
|
|
|
|
else:
|
|
|
|
# Run from root directory to avoid accidentally picking up any local config settings
|
|
|
|
dir = "/"
|
|
|
|
|
2021-02-12 06:13:05 +01:00
|
|
|
(rc, out, err) = module.run_command(args, cwd=dir, expand_user_and_vars=False)
|
2020-03-09 10:11:07 +01:00
|
|
|
if params['list_all'] and scope and rc == 128 and 'unable to read config file' in err:
|
|
|
|
# This just means nothing has been set at the given scope
|
|
|
|
module.exit_json(changed=False, msg='', config_values={})
|
|
|
|
elif rc >= 2:
|
|
|
|
# If the return code is 1, it just means the option hasn't been set yet, which is fine.
|
|
|
|
module.fail_json(rc=rc, msg=err, cmd=' '.join(args))
|
|
|
|
|
|
|
|
if params['list_all']:
|
|
|
|
values = out.rstrip().splitlines()
|
|
|
|
config_values = {}
|
|
|
|
for value in values:
|
|
|
|
k, v = value.split('=', 1)
|
|
|
|
config_values[k] = v
|
|
|
|
module.exit_json(changed=False, msg='', config_values=config_values)
|
|
|
|
elif not new_value and not unset:
|
|
|
|
module.exit_json(changed=False, msg='', config_value=out.rstrip())
|
|
|
|
elif unset and not out:
|
|
|
|
module.exit_json(changed=False, msg='no setting to unset')
|
|
|
|
else:
|
|
|
|
old_value = out.rstrip()
|
|
|
|
if old_value == new_value:
|
|
|
|
module.exit_json(changed=False, msg="")
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
if unset:
|
|
|
|
args.insert(len(args) - 1, "--" + unset)
|
2020-12-22 16:17:24 +01:00
|
|
|
cmd = args
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2021-02-12 06:13:05 +01:00
|
|
|
cmd = args + [new_value]
|
2022-04-26 11:51:01 +02:00
|
|
|
(rc, out, err) = module.run_command(cmd, cwd=dir, ignore_invalid_cwd=False, expand_user_and_vars=False)
|
2020-03-09 10:11:07 +01:00
|
|
|
if err:
|
|
|
|
module.fail_json(rc=rc, msg=err, cmd=cmd)
|
|
|
|
|
|
|
|
module.exit_json(
|
|
|
|
msg='setting changed',
|
|
|
|
diff=dict(
|
|
|
|
before_header=' '.join(args),
|
|
|
|
before=old_value + "\n",
|
|
|
|
after_header=' '.join(args),
|
|
|
|
after=(new_value or '') + "\n"
|
|
|
|
),
|
|
|
|
changed=True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|