ansible.builtin.git 模块 - 从 git 检出中部署软件(或文件)

注意

此模块是 ansible-core 的一部分,包含在所有 Ansible 安装中。在大多数情况下,即使没有指定 集合关键字,也可以使用简短的模块名称 git。但是,我们建议您使用 完全限定的集合名称(FQCN) ansible.builtin.git 以便于链接到模块文档并避免与可能具有相同模块名称的其他集合冲突。

概要

  • 管理git 存储库的检出以部署文件或软件。

要求

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

  • git>=1.7.1(命令行工具)

参数

参数

注释

accept_hostkey

布尔值

将确保或不确保 -o StrictHostKeyChecking=no 作为 ssh 选项存在。

请注意,这会禁用对 MITM 攻击的保护。

使用 OpenSSH >= 7.5 的用户可能希望使用 accept_newhostkey 或将 ssh_opts 设置为 StrictHostKeyChecking=accept-new,它不会消除 MITM 问题,但它确实会将其限制在第一次尝试。

选择

  • false ← (默认)

  • true

accept_newhostkey

布尔值

在 ansible-core 2.12 中添加

从 OpenSSH 7.5 开始,可以使用 -o StrictHostKeyChecking=accept-new,它更安全,只会接受不存在或相同的主机密钥。如果为 true,确保 -o StrictHostKeyChecking=accept-new 作为 ssh 选项存在。

选择

  • false ← (默认)

  • true

archive

路径

指定带有扩展名的存档文件路径。如果指定,则创建包含源树的树结构的指定格式的存档文件。允许的存档格式 [“zip”,“tar.gz”,“tar”,“tgz”]。

这将克隆并从本地目录执行 git 存档,因为并非所有 git 服务器都支持 git 存档。

archive_prefix

字符串

在 ansible-base 2.10 中添加

指定要添加到存档中每个文件路径的前缀。需要 archive 被指定。

bare

布尔值

如果为 true,存储库将被创建为裸存储库,否则它将是一个带工作区的标准存储库。

选择

  • false ← (默认)

  • true

clone

布尔值

如果为 false,即使存储库在本地不存在,也不要克隆存储库。

选择

  • false

  • true ← (默认)

depth

整数

创建一个浅克隆,其历史记录被截断到指定数量的修订版。最小可能值为 1,否则忽略。需要git>=1.9.1才能正常工作。

dest

路径 / 必需

存储库应被检出的路径。这等效于 git clone [repo_url] [directory]。在 repo 中命名的存储库不会附加到此路径,目标目录必须为空。此参数是必需的,除非 clone 设置为 false

executable

路径

要使用的 git 可执行文件的路径。如果未提供,将使用解析二进制路径的正常机制。

force

布尔值

如果为 true,工作存储库中的任何修改过的文件都将被丢弃。在 0.7 之前,这始终为 true,无法禁用。在 1.9 之前,默认值为 true

选择

  • false ← (默认)

  • true

gpg_allowlist

别名:gpg_whitelist

列表 / 元素=字符串

在 Ansible 2.9 中添加

要与 GPG 签名的提交的指纹进行比较的可信 GPG 指纹列表。

仅在 verify_commit=yes 时使用。

由于依赖于 git 的 --raw 标志到 verify-commitverify-tag,因此使用此功能需要 Git 2.6+。

别名 gpg_allowlist 在版本 2.17 中添加。

别名 gpg_whitelist 已弃用,将在版本 2.21 中删除。

默认: []

key_file

路径

指定目标主机上的可选私钥文件路径,用于检出。

这将确保 IdentitiesOnly=yes 存在于 ssh_opts 中。

recursive

布尔值

如果为 false,存储库将被克隆而不会使用 --recursive 选项,跳过子模块。

选择

  • false

  • true ← (默认)

reference

字符串

参考存储库(参见 git clone --reference ...)。

refspec

字符串

添加要获取的额外 refspec。如果版本设置为SHA-1,该SHA-1无法从任何分支或标签中访问,则可能需要此选项来指定包含SHA-1的 ref。使用与 git fetch 命令相同的语法。一个示例值可能是“refs/meta/config”。

remote

字符串

远程的名称。

默认: "origin"

repo

别名:name

字符串 / 必需

git、SSH 或 HTTP(S) 协议地址的 git 存储库。

separate_git_dir

路径

在 Ansible 2.7 中添加

放置克隆存储库的路径。如果指定,Git 存储库可以与工作区分离。

single_branch

布尔值

在 ansible-core 2.11 中添加

仅克隆通向指定修订版顶端的历史记录。

选择

  • false ← (默认)

  • true

ssh_opts

字符串

当用作协议时,git 将传递给 ssh 的选项,它通过 gitGIT_SSH/GIT_SSH_COMMAND 环境变量工作。

对于旧版本,它将 GIT_SSH_OPTS(特定于此模块)附加到上述变量,或通过包装器脚本附加。

其他选项可以添加到此列表中,例如 key_fileaccept_hostkey

一个示例值可能是 -o StrictHostKeyChecking=no(尽管此特定选项最好由 accept_hostkey 设置)。

该模块确保始终存在 BatchMode=yes 以避免提示。

track_submodules

布尔值

如果为 true,子模块将跟踪其主分支(或 .gitmodules 中指定的其他分支)上的最新提交。如果为 false,子模块将保留在主项目指定的修订版中。这相当于在 git 子模块更新中指定 --remote 标志。

选择

  • false ← (默认)

  • true

umask

any

在执行任何检出或任何其他存储库维护之前要设置的 umask。

update

布尔值

如果为 false,则不会从源存储库中检索新修订版。

像存档这样的操作将在现有的(旧的)存储库上执行,并且可能无法响应对选项版本或远程的更改。

选择

  • false

  • true ← (默认)

verify_commit

布尔值

如果为 true,在克隆或检出 version 时,验证 GPG 签名的提交的签名。这需要安装 git 版本>=2.1.0。提交必须签名,并且公钥必须存在于 GPG 密钥环中。

选择

  • false ← (默认)

  • true

version

字符串

要检出的存储库版本。这可以是文字字符串 HEAD、分支名称、标签名称。它也可以是 *SHA-1* 哈希,在这种情况下,如果给定的修订版尚未可用,则需要指定 refspec

默认值: "HEAD"

属性

属性

支持

描述

check_mode

支持: 完全支持

可以在 check_mode 中运行并返回更改状态预测,而不会修改目标。如果不受支持,则该操作将被跳过。

diff_mode

支持: 完全支持

将在 diff 模式下返回有关更改内容(或可能需要在 check_mode 中更改的内容)的详细信息

platform

平台: posix

可以针对其进行操作的目标操作系统/系列

说明

注意

  • 如果任务似乎挂起,首先验证远程主机是否在 known_hosts 中。SSH 将提示用户授权与远程主机的第一次联系。为了避免此提示,一种解决方案是使用 accept_hostkey 选项。另一种解决方案是在调用 git 模块之前将远程主机公钥添加到 /etc/ssh/ssh_known_hosts 中,使用以下命令:ssh-keyscan -H remote_host.com >> /etc/ssh/ssh_known_hosts

示例

- name: Git checkout
  ansible.builtin.git:
    repo: 'https://github.com/ansible/ansible.git'
    dest: /tmp/checkout
    version: release-0.22

- name: Read-write git checkout from github
  ansible.builtin.git:
    repo: git@github.com:ansible/ansible.git
    dest: /tmp/checkout

- name: Just ensuring the repo checkout exists
  ansible.builtin.git:
    repo: 'https://github.com/ansible/ansible.git'
    dest: /tmp/checkout
    update: no

- name: Just get information about the repository whether or not it has already been cloned locally
  ansible.builtin.git:
    repo: git@github.com:ansible/ansible.git
    dest: /tmp/checkout
    clone: no
    update: no

- name: Checkout a github repo and use refspec to fetch all pull requests
  ansible.builtin.git:
    repo: 'https://github.com/ansible/ansible.git'
    dest: /tmp/checkout
    refspec: '+refs/pull/*:refs/heads/*'

- name: Create git archive from repo
  ansible.builtin.git:
    repo: git@github.com:ansible/ansible.git
    dest: /tmp/checkout
    archive: /tmp/ansible.zip

- name: Clone a repo with separate git directory
  ansible.builtin.git:
    repo: 'https://github.com/ansible/ansible.git'
    dest: /tmp/checkout
    separate_git_dir: /tmp/repo

- name: Example clone of a single branch
  ansible.builtin.git:
    repo: git@github.com:ansible/ansible.git
    dest: /tmp/checkout
    single_branch: yes
    version: master

- name: Avoid hanging when http(s) password is missing
  ansible.builtin.git:
    repo: 'https://github.com/ansible/ansible.git'
    dest: /tmp/checkout
  environment:
    GIT_TERMINAL_PROMPT: 0 # reports "terminal prompts disabled" on missing password
    # or GIT_ASKPASS: /bin/true # for git before version 2.3.0, reports "Authentication failed" on missing password

返回值

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

描述

after

字符串

更新期间检索的存储库的最新提交修订版。

返回:成功

示例: "4c020102a9cd6fe908c9a4a326a38f972f63a903"

before

字符串

存储库更新之前的提交修订版,对于新存储库为“null”。

返回:成功

示例: "67c04ebe40a003bda0efb34eacfb93b0cafdf628"

git_dir_before

字符串

如果 .git 目录已更改,则包含其原始路径。

返回:成功

示例: "/path/to/old/git/dir"

git_dir_now

字符串

如果 .git 目录已更改,则包含其新路径。

返回:成功

示例: "/path/to/new/git/dir"

remote_url_changed

布尔值

包含是否更改了远程 URL(True 或 False)。

返回:成功

示例: true

warnings

字符串

如果由于 git 版本过旧而无法使用请求的功能,则包含警告列表。

返回:错误

示例: "git version is too old to fully support the depth argument. Falling back to full checkouts."

作者

  • Ansible 核心团队

  • Michael DeHaan