community.general.git_config (8.5.0) — module

Read and write git configuration

Authors: Matthew Gamble (@djmattyg007), Marius Gedminas (@mgedmin)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

Description

The M(community.general.git_config) module changes git configuration by invoking C(git config). 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 /etc/.git/config). Solutions involving M(ansible.builtin.command) are cumbersome or do not work correctly in check mode.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add a setting to ~/.gitconfig
  community.general.git_config:
    name: alias.ci
    scope: global
    value: commit
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add a setting to ~/.gitconfig
  community.general.git_config:
    name: alias.st
    scope: global
    value: status
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove a setting from ~/.gitconfig
  community.general.git_config:
    name: alias.ci
    scope: global
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add a setting to ~/.gitconfig
  community.general.git_config:
    name: core.editor
    scope: global
    value: vim
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add a setting system-wide
  community.general.git_config:
    name: alias.remotev
    scope: system
    value: remote -v
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add a setting to a system scope (default)
  community.general.git_config:
    name: alias.diffc
    value: diff --cached
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add a setting to a system scope (default)
  community.general.git_config:
    name: color.ui
    value: auto
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add several options for the same name
  community.general.git_config:
    name: push.pushoption
    value: "{{ item }}"
    add_mode: add
  loop:
    - merge_request.create
    - merge_request.draft
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Make etckeeper not complaining when it is invoked by cron
  community.general.git_config:
    name: user.email
    repo: /etc
    scope: local
    value: 'root@{{ ansible_fqdn }}'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Read individual values from git config
  community.general.git_config:
    name: alias.ci
    scope: global
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Scope system is also assumed when reading values, unless list_all=true
  community.general.git_config:
    name: alias.diffc
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Read all values from git config
  community.general.git_config:
    list_all: true
    scope: global
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: When list_all is yes and no scope is specified, you get configuration from all scopes
  community.general.git_config:
    list_all: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Specify a repository to include local settings
  community.general.git_config:
    list_all: true
    repo: /path/to/repo.git

Inputs

    
file:
    description:
    - Path to an adhoc git configuration file to be managed using the V(file) scope.
    type: path
    version_added: 2.0.0
    version_added_collection: community.general

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.
    type: str

repo:
    description:
    - Path to a git repository for reading and writing values from a specific repo.
    type: path

scope:
    choices:
    - file
    - local
    - global
    - system
    description:
    - Specify which scope to read/set values from.
    - This is required when setting config values.
    - 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).
    type: str

state:
    choices:
    - present
    - absent
    default: present
    description:
    - 'Indicates the setting should be set/unset. This parameter has higher precedence
      than O(value) parameter: when O(state=absent) and O(value) is defined, O(value)
      is discarded.'
    type: str

value:
    description:
    - When specifying the name of a single setting, supply a value to set that setting
      to the given value.
    type: str

add_mode:
    choices:
    - add
    - replace-all
    default: replace-all
    description:
    - Specify if a value should replace the existing value(s) or if the new value should
      be added alongside other values with the same name.
    - This option is only relevant when adding/replacing values. If O(state=absent) or
      values are just read out, this option is not considered.
    type: str
    version_added: 8.1.0
    version_added_collection: community.general

list_all:
    default: false
    description:
    - List all settings (optionally limited to a given O(scope)).
    type: bool

Outputs

config_value:
  description: When O(list_all=false) and value is not set, a string containing the
    value of the setting in name
  returned: success
  sample: vim
  type: str
config_values:
  description: When O(list_all=true), a dict containing key/value pairs of multiple
    configuration settings
  returned: success
  sample:
    alias.diffc: diff --cached
    alias.remotev: remote -v
    color.ui: auto
    core.editor: vim
  type: dict