安装集合
注意
如果您按照本段所述手动安装集合,那么在升级 ansible 包或 ansible-core 时,该集合将不会自动升级。
在容器中安装集合
您可以在称为“执行环境 (Execution Environments)”的容器中安装集合及其依赖项。详细信息请参阅 执行环境入门。
使用 ansible-galaxy 安装集合
默认情况下,ansible-galaxy collection install 使用 https://galaxy.ansible.com 作为 Galaxy 服务器(如 ansible.cfg 文件中 GALAXY_SERVER 下所列)。您无需进行任何额外配置。默认情况下,Ansible 会将集合安装在 ~/.ansible/collections 下的 ansible_collections 目录中。
如果您使用其他 Galaxy 服务器(例如 Red Hat Automation Hub),请参阅 配置 ansible-galaxy 客户端。
要安装托管在 Galaxy 中的集合
ansible-galaxy collection install my_namespace.my_collection
要将集合升级到 Galaxy 服务器上提供的最新版本,可以使用 --upgrade 选项
ansible-galaxy collection install my_namespace.my_collection --upgrade
您也可以直接使用构建好的 tarball(压缩包)
ansible-galaxy collection install my_namespace-my_collection-1.0.0.tar.gz -p ./collections
您可以从本地源目录构建并安装集合。ansible-galaxy 工具会使用目录中的 MANIFEST.json 或 galaxy.yml 元数据来构建集合。
ansible-galaxy collection install /path/to/collection -p ./collections
您也可以在命名空间目录中安装多个集合。
ns/
├── collection1/
│ ├── MANIFEST.json
│ └── plugins/
└── collection2/
├── galaxy.yml
└── plugins/
ansible-galaxy collection install /path/to/ns -p ./collections
注意
安装命令会自动将 ansible_collections 路径附加到通过 -p 选项指定的路径后,除非父目录本身已位于名为 ansible_collections 的文件夹中。
使用 -p 选项指定安装路径时,请使用 COLLECTIONS_PATHS 中配置的值之一,因为这是 Ansible 本身期望查找集合的位置。如果您未指定路径,ansible-galaxy collection install 会将集合安装到 COLLECTIONS_PATHS 中定义的第一个路径,默认情况下是 ~/.ansible/collections
通过签名验证安装集合
如果集合已由 分发服务器 签名,服务器将提供 ASCII 装甲的独立签名,以便在验证集合内容之前验证 MANIFEST.json 的真实性。此选项并非在所有分发服务器上都可用。有关支持集合签名的服务器列表,请参阅 分发集合。
要对已签名的集合使用签名验证
为 ansible-galaxy 配置 GnuPG 密钥环,或者在安装已签名集合时使用
--keyring选项提供密钥环路径。将来自分发服务器的公钥导入到该密钥环中。
gpg --import --no-default-keyring --keyring ~/.ansible/pubring.kbx my-public-key.asc
安装集合时验证签名。
ansible-galaxy collection install my_namespace.my_collection --keyring ~/.ansible/pubring.kbx
如果您已经 配置了 GnuPG 密钥环,则不需要
--keyring选项。可选地,可在安装后的任何时候验证签名,以证明集合未被篡改。有关详细信息,请参阅 验证已签名的集合。
您还可以包含分发服务器提供之外的签名。使用 --signature 选项,用这些额外签名验证集合的 MANIFEST.json。补充签名应以 URI 形式提供。
ansible-galaxy collection install my_namespace.my_collection --signature https://examplehost.com/detached_signature.asc --keyring ~/.ansible/pubring.kbx
GnuPG 验证仅针对从分发服务器安装的集合进行。用户提供的签名不用于验证从 Git 仓库、源目录或 tar.gz 文件的 URL/路径安装的集合。
您还可以将额外的签名包含在集合的 requirements.yml 文件中的 signatures 键下。
# requirements.yml
collections:
- name: ns.coll
version: 1.0.0
signatures:
- https://examplehost.com/detached_signature.asc
- file:///path/to/local/detached_signature.asc
有关如何使用此文件安装集合的详细信息,请参阅 集合需求文件。
默认情况下,如果至少有 1 个签名成功验证了集合,则视为验证成功。所需签名的数量可以通过 --required-valid-signature-count 或 GALAXY_REQUIRED_VALID_SIGNATURE_COUNT 进行配置。将该选项设置为 all 可要求所有签名均有效。如果未找到有效的签名即视为签名验证失败,可在值前加上 +,例如 +all 或 +1。
export ANSIBLE_GALAXY_GPG_KEYRING=~/.ansible/pubring.kbx
export ANSIBLE_GALAXY_REQUIRED_VALID_SIGNATURE_COUNT=2
ansible-galaxy collection install my_namespace.my_collection --signature https://examplehost.com/detached_signature.asc --signature file:///path/to/local/detached_signature.asc
某些 GnuPG 错误可以使用 --ignore-signature-status-code 或 GALAXY_REQUIRED_VALID_SIGNATURE_COUNT 忽略。GALAXY_REQUIRED_VALID_SIGNATURE_COUNT 应为一个列表,而 --ignore-signature-status-code 可以多次提供,以忽略多个额外的错误状态码。
此示例要求分发服务器提供的任何签名都能验证集合,除非由于 NO_PUBKEY 而失败
export ANSIBLE_GALAXY_GPG_KEYRING=~/.ansible/pubring.kbx
export ANSIBLE_GALAXY_REQUIRED_VALID_SIGNATURE_COUNT=all
ansible-galaxy collection install my_namespace.my_collection --ignore-signature-status-code NO_PUBKEY
如果上述示例的验证失败,则仅显示除 NO_PUBKEY 以外的错误。
如果验证不成功,集合将不会被安装。GnuPG 签名验证可以通过 --disable-gpg-verify 或通过配置 GALAXY_DISABLE_GPG_VERIFY 来禁用。
安装集合的旧版本
您一次只能安装一个版本的集合。默认情况下,ansible-galaxy 安装可用的最新版本。如果您想安装特定版本,可以添加版本范围标识符。例如,要安装该集合的 1.0.0-beta.1 版本
ansible-galaxy collection install my_namespace.my_collection:==1.0.0-beta.1
您可以通过 , 分隔指定多个范围标识符。请使用单引号,以便 shell 将整个命令(包括 >, ! 和其他运算符)一起传递。例如,安装大于或等于 1.0.0 且小于 2.0.0 的最新版本
ansible-galaxy collection install 'my_namespace.my_collection:>=1.0.0,<2.0.0'
Ansible 将始终安装符合您指定的范围标识符的最新版本。您可以使用以下范围标识符
*: 最新版本。这是默认设置。!=: 不等于指定版本。==: 完全等于指定版本。>=: 大于或等于指定版本。>: 大于指定版本。<=: 小于或等于指定版本。<: 小于指定版本。
注意
默认情况下,ansible-galaxy 会忽略预发布版本。要安装预发布版本,您必须使用 == 范围标识符显式要求它。
使用需求文件安装多个集合
您可以设置一个 requirements.yml 文件来通过一个命令安装多个集合。此文件为 YAML 格式
---
collections:
# With just the collection name
- my_namespace.my_collection
# With the collection name, version, and source options
- name: my_namespace.my_other_collection
version: ">=1.2.0" # Version range identifiers (default: ``*``)
source: ... # The Galaxy URL to pull the collection from (default: ``--api-server`` from cmdline)
您可以为每个集合条目指定以下键
name
version
signatures
source
type
version 键使用 安装集合旧版本 中记录的相同范围标识符格式。
signatures 键接受一个签名源列表,用于在集合安装和 ansible-galaxy collection verify 期间补充 Galaxy 服务器上找到的签名。签名源应为包含独立签名的 URI。如果指定了签名,则必须提供 --keyring CLI 选项。
签名仅用于验证 Galaxy 服务器上的集合。用户提供的签名不用于验证从 git 仓库、源目录或 tar.gz 文件的 URL/路径安装的集合。
collections:
- name: namespace.name
version: 1.0.0
type: galaxy
signatures:
- https://examplehost.com/detached_signature.asc
- file:///path/to/local/detached_signature.asc
type 键可以设置为 file、galaxy、git、url、dir 或 subdirs。如果省略 type,则使用 name 键隐式确定集合的来源。
当您使用 type: git 安装集合时,version 键可以指向分支或 git commit-ish 对象(提交或标签)。例如
collections:
- name: https://github.com/organization/repo_name.git
type: git
version: devel
您也可以在 roles 键下向 requirements.yml 文件添加角色。这些值遵循旧版 Ansible 中使用的需求文件的相同格式。
---
roles:
# Install a role from Ansible Galaxy.
- name: geerlingguy.java
version: "1.9.6" # note that ranges are not supported for roles
collections:
# Install a collection from Ansible Galaxy.
- name: geerlingguy.php_roles
version: ">=0.9.3"
source: https://galaxy.ansible.com
要同时安装角色和集合,请运行以下命令
$ ansible-galaxy install -r requirements.yml
运行 ansible-galaxy collection install -r 或 ansible-galaxy role install -r 将分别只安装集合或角色。
注意
当指定自定义集合或角色安装路径时,从同一个需求文件同时安装角色和集合将无法工作。在这种情况下,集合将被跳过,命令将像执行 ansible-galaxy role install 那样处理每个条目。
为离线使用下载集合
要从 Galaxy 下载集合 tarball 以供离线使用
导航到集合页面。
点击 Download tarball。
您可能还需要手动下载任何依赖的集合。
在 playbook 附近安装集合
您可以在项目中紧邻 playbook 的位置本地安装集合,而不是安装在系统或 AWX 的全局位置。
在 playbook 附近本地安装集合具有一些好处,例如
确保项目的所有用户都使用相同的集合版本。
使用自包含的项目便于在不同环境之间迁移。增加的可移植性也降低了设置新环境的开销。这在云环境中部署 Ansible playbook 时非常有利。
本地管理集合让您可以随 playbook 一起对其进行版本控制。
本地安装集合可以将它们与拥有多个项目的环境中的全局安装隔离开来。
以下是在当前 playbook 附近保留集合的示例,采用 collections/ansible_collections/ 目录结构。
./
├── play.yml
├── collections/
│ └── ansible_collections/
│ └── my_namespace/
│ └── my_collection/<collection structure lives here>
有关集合目录结构的详细信息,请参阅 集合结构。
从源文件安装集合
Ansible 还可以通过多种方式从源目录进行安装
collections:
# directory containing the collection
- name: ./my_namespace/my_collection/
type: dir
# directory containing a namespace, with collections as subdirectories
- name: ./my_namespace/
type: subdirs
Ansible 还可以通过直接指定输出文件,来安装通过 ansible-galaxy collection build 构建的集合,或者从 Galaxy 下载以供离线使用的集合
collections:
- name: /tmp/my_namespace-my_collection-1.0.0.tar.gz
type: file
注意
相对路径是根据当前工作目录(您从该目录调用 ansible-galaxy install -r)进行计算的。它们不是相对于 requirements.yml 文件的。
从 Git 仓库安装集合
您可以从 git 仓库而不是从 Galaxy 或 Automation Hub 安装集合。作为开发者,从 git 仓库安装可以让您在创建 tarball 和发布集合之前审查您的集合。作为用户,从 git 仓库安装可以让您使用尚未出现在 Galaxy 或 Automation Hub 中的集合或版本。此功能旨在作为内容开发者的一种最小捷径,如前所述,git 仓库可能不支持 ansible-galaxy CLI 的全部功能。在复杂情况下,更灵活的选择可能是将仓库 git clone 到集合安装目录的正确文件结构中。
仓库必须包含 galaxy.yml 或 MANIFEST.json 文件。此文件提供元数据,例如集合的版本号和命名空间。
在命令行上从 git 仓库安装集合
要在命令行上从 git 仓库安装集合,请使用仓库的 URI,而不是集合名称或 tar.gz 文件的路径。使用前缀 git+,除非您在使用带有用户 git 的 SSH 认证(例如 git@github.com:ansible-collections/ansible.windows.git)。您可以使用以逗号分隔的 git commit-ish 语法来指定分支、提交或标签。
例如
# Install a collection in a repository using the latest commit on the branch 'devel'
ansible-galaxy collection install git+https://github.com/organization/repo_name.git,devel
# Install a collection from a private GitHub repository
ansible-galaxy collection install git@github.com:organization/repo_name.git
# Install a collection from a local git repository
ansible-galaxy collection install git+file:///home/user/path/to/repo_name.git
警告
将凭据嵌入到 git URI 中是不安全的。使用安全的认证选项,防止您的凭据暴露在日志或其他地方。
使用 SSH 认证
使用 netrc 认证
在 git 配置中使用 http.extraHeader
在 git 配置中使用 url.<base>.pushInsteadOf
在 git 仓库中指定集合位置
当您从 git 仓库安装集合时,Ansible 会使用集合的 galaxy.yml 或 MANIFEST.json 元数据文件来构建集合。默认情况下,Ansible 会搜索两个路径以查找集合的 galaxy.yml 或 MANIFEST.json 元数据文件:
仓库的顶层。
仓库路径中的每个目录(深一层)。
如果仓库顶层存在 galaxy.yml 或 MANIFEST.json 文件,Ansible 会使用该文件中的集合元数据来安装单个集合。
├── galaxy.yml
├── plugins/
│ ├── lookup/
│ ├── modules/
│ └── module_utils/
└─── README.md
如果仓库路径中的一个或多个目录(深一层)中存在 galaxy.yml 或 MANIFEST.json 文件,Ansible 会将每个包含元数据的目录安装为集合。例如,Ansible 默认安装此仓库结构中的 collection1 和 collection2:
├── collection1
│ ├── docs/
│ ├── galaxy.yml
│ └── plugins/
│ ├── inventory/
│ └── modules/
└── collection2
├── docs/
├── galaxy.yml
├── plugins/
| ├── filter/
| └── modules/
└── roles/
如果您有不同的仓库结构,或者只想安装集合的子集,可以在 URI 末尾(在可选的逗号分隔版本之前)添加一个片段,以指示元数据文件的位置。路径应为目录,而不是元数据文件本身。例如,要仅从包含两个集合的示例仓库中安装 collection2:
ansible-galaxy collection install git+https://github.com/organization/repo_name.git#/collection2/
在某些仓库中,主目录对应于命名空间:
namespace/
├── collectionA/
| ├── docs/
| ├── galaxy.yml
| ├── plugins/
| │ ├── README.md
| │ └── modules/
| ├── README.md
| └── roles/
└── collectionB/
├── docs/
├── galaxy.yml
├── plugins/
│ ├── connection/
│ └── modules/
├── README.md
└── roles/
您可以安装此仓库中的所有集合,或从特定提交中安装一个集合:
# Install all collections in the namespace
ansible-galaxy collection install git+https://github.com/organization/repo_name.git#/namespace/
# Install an individual collection using a specific commit
ansible-galaxy collection install git+https://github.com/organization/repo_name.git#/namespace/collectionA/,7b60ddc245bc416b72d8ea6ed7b799885110f5e5
配置 ansible-galaxy 客户端
默认情况下,ansible-galaxy 使用 https://galaxy.ansible.com 作为 Galaxy 服务器(如 ansible.cfg 文件中 GALAXY_SERVER 下所列)。
您可以使用以下任一选项来配置 ansible-galaxy collection 以使用其他服务器(例如自定义 Galaxy 服务器)
在 配置文件 的 GALAXY_SERVER_LIST 配置选项中设置服务器列表。
使用
--server命令行参数将其限制为单个服务器。
要在 ansible.cfg 中配置 Galaxy 服务器列表
在
[galaxy]部分下添加server_list选项,指向一个或多个服务器名称。为每个服务器名称创建一个新部分。
为每个服务器名称设置
url选项。可选地,为每个服务器名称设置 API 令牌。转到 https://galaxy.ansible.com/me/preferences 并点击 Show API key。
注意
每个服务器名称的 url 选项必须以正斜杠 / 结尾。如果您未在 Galaxy 服务器列表中设置 API 令牌,请使用 --api-key 参数将令牌传递给 ansible-galaxy collection publish 命令。
以下示例展示了如何配置多个服务器
[galaxy]
server_list = my_org_hub, release_galaxy, test_galaxy, my_galaxy_ng
[galaxy_server.my_org_hub]
url=https://automation.my_org/
username=my_user
password=my_pass
[galaxy_server.release_galaxy]
url=https://galaxy.ansible.com/
token=my_token
[galaxy_server.test_galaxy]
url=https://galaxy-dev.ansible.com/
token=my_test_token
[galaxy_server.my_galaxy_ng]
url=http://my_galaxy_ng:8000/api/automation-hub/
auth_url=http://my_keycloak:8080/auth/realms/myco/protocol/openid-connect/token
client_id=galaxy-ng
token=my_keycloak_access_token
注意
您可以使用 --server 命令行参数在 server_list 中选择明确的 Galaxy 服务器,且该参数的值应与服务器名称匹配。要使用不在列表中的服务器,请将该值设置为访问该服务器的 URL(列表中的所有服务器都将被忽略)。此外,您不能对任何预定义的服务器使用 --api-key 参数。只有在没有定义服务器列表或者在 --server 参数中指定了 URL 的情况下,才能使用 api_key 参数。
Galaxy 服务器列表配置选项
GALAXY_SERVER_LIST 选项是按优先级排序的服务器标识符列表。在搜索集合时,安装过程将按该顺序搜索,例如先搜索 automation_hub,然后是 my_org_hub、release_galaxy,最后是 test_galaxy,直到找到集合。实际的 Galaxy 实例定义在 [galaxy_server.{{ id }}] 部分下,其中 {{ id }} 是列表中定义的服务器标识符。此部分可以定义以下键
url: 要连接的 Galaxy 实例的 URL。必需。token: 用于向 Galaxy 实例验证身份的 API 令牌密钥。与username互斥。username: 用于向 Galaxy 实例进行基本身份验证的用户名。与token互斥。password: 与username结合使用以进行基本身份验证的密码。auth_url: 如果使用 SSO 身份验证(例如 galaxyNG),则为 Keycloak 服务器的“token_endpoint”的 URL。与username互斥。需要token。validate_certs: 是否验证 Galaxy 服务器的 TLS 证书。默认为 True,除非提供了--ignore-certs选项或将GALAXY_IGNORE_CERTS配置为 True。client_id: 用于身份验证的 Keycloak 令牌的 client_id。需要auth_url和token。默认的client_id为 cloud-services,用于适配 Red Hat SSO。timeout: 等待 Galaxy 服务器响应的最大秒数。
除了在 ansible.cfg 文件中定义这些服务器选项外,您还可以将其定义为环境变量。环境变量的形式为 ANSIBLE_GALAXY_SERVER_{{ id }}_{{ key }},其中 {{ id }} 是服务器标识符的大写形式,{{ key }} 是要定义的键。例如,您可以设置 ANSIBLE_GALAXY_SERVER_RELEASE_GALAXY_TOKEN=secret_token 为 release_galaxy 定义 token。
对于仅使用一个 Galaxy 服务器的操作(例如 publish、info 或 install 命令),ansible-galaxy collection 命令使用 server_list 中的第一个条目,除非您通过 --server 参数传入了明确的服务器。
注意
ansible-galaxy 可以查找其他已配置的 Galaxy 实例上的依赖项,以支持集合依赖于来自另一个 Galaxy 实例的集合的用例。
移除集合
如果您不再需要某个集合,只需从文件系统中移除安装目录即可。路径可能因您的操作系统而异
rm -rf ~/.ansible/collections/ansible_collections/community/general
rm -rf ./venv/lib/python3.9/site-packages/ansible_collections/community/general