community.general.git_config 模块 – 更新 git 配置

注意

此模块是 community.general 集合(版本 10.1.0)的一部分。

如果您使用的是 ansible 包,您可能已经安装了这个集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.general。您需要进一步的要求才能使用此模块,请参阅 要求 了解详细信息。

要在 playbook 中使用它,请指定:community.general.git_config

概要

要求

在执行此模块的主机上需要以下要求。

  • git

参数

参数

注释

add_mode

字符串

在 community.general 8.1.0 中添加

指定值应替换现有值,还是应将新值与同名的其他值一起添加。

此选项仅在添加/替换值时相关。如果 state=absent 或仅读取值,则不考虑此选项。

选择

  • "add"

  • "replace-all" ←(默认)

file

路径

在 community.general 2.0.0 中添加

要使用 file 作用域管理的临时 git 配置文件的路径。

list_all

布尔值

列出所有设置(可选地限制为给定的 scope)。

此选项已弃用,将从 community.general 11.0.0 中删除。请改用 community.general.git_config_info

选择

  • false ←(默认)

  • true

name

字符串

设置的名称。如果未提供值,则将从配置中读取该值(如果已设置)。

repo

路径

从特定仓库读取和写入值的 git 仓库的路径。

scope

字符串

指定从中读取/设置值的范围。

设置配置值时,这是必需的。

如果将其设置为 local,则还必须指定 repo 参数。

如果将其设置为 file,则还必须指定 file 参数。

默认情况下,仅当不使用 list_all=true 时才为 system。

选择

  • "file"

  • "local"

  • "global"

  • “系统”

状态

字符串

指示应该设置/取消设置的配置。此参数的优先级高于 value 参数:当 state=absent 且定义了 value 时,value 将被忽略。

选择

  • "present" ← (默认)

  • “absent”

字符串

当指定单个设置的名称时,提供一个值以将该设置设置为给定值。

从 community.general 11.0.0 版本开始,如果 state=present,则 value 将是必需的。要读取值,请改用 community.general.git_config_info 模块。

属性

属性

支持

描述

check_mode

支持: 完全

可以在 check_mode 中运行,并返回更改状态预测,而无需修改目标。

diff_mode

支持:

当处于差异模式时,将返回已更改的内容的详细信息(或者可能需要在 check_mode 中更改的内容)。

示例

- name: Add a setting to ~/.gitconfig
  community.general.git_config:
    name: alias.ci
    scope: global
    value: commit

- name: Add a setting to ~/.gitconfig
  community.general.git_config:
    name: alias.st
    scope: global
    value: status

- name: Remove a setting from ~/.gitconfig
  community.general.git_config:
    name: alias.ci
    scope: global
    state: absent

- name: Add a setting to ~/.gitconfig
  community.general.git_config:
    name: core.editor
    scope: global
    value: vim

- name: Add a setting system-wide
  community.general.git_config:
    name: alias.remotev
    scope: system
    value: remote -v

- name: Add a setting to a system scope (default)
  community.general.git_config:
    name: alias.diffc
    value: diff --cached

- name: Add a setting to a system scope (default)
  community.general.git_config:
    name: color.ui
    value: auto

- 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

- 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 }}'

返回值

常见的返回值已在此处记录 here,以下是此模块独有的字段

描述

config_value

字符串

list_all=false 且未设置值时,包含名称中设置的值的字符串

返回: 成功

示例: "vim"

config_values

字典

list_all=true 时,一个包含多个配置设置的键/值对的字典

返回: 成功

示例: {"alias.diffc": "diff --cached", "alias.remotev": "remote -v", "color.ui": "auto", "core.editor": "vim"}

作者

  • Matthew Gamble (@djmattyg007)

  • Marius Gedminas (@mgedmin)