安装集合
注意
如果您按照本段描述手动安装集合,则在升级 ansible 包或 ansible-core 时,不会自动升级该集合。
在容器中安装集合
您可以在称为执行环境的容器中安装集合及其依赖项。有关详细信息,请参阅 执行环境入门。
使用 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 仓库、源目录或 URL/tar.gz 文件路径安装的集合。
您也可以在集合 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
可以使用 --ignore-signature-status-code 或 GALAXY_REQUIRED_VALID_SIGNATURE_COUNT 忽略某些 GnuPG 错误。 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 以外的错误。
如果验证不成功,则不会安装该集合。可以使用 --disable-gpg-verify 或配置 GALAXY_DISABLE_GPG_VERIFY 禁用 GnuPG 签名验证。
安装旧版本的集合
您一次只能安装一个版本的集合。默认情况下,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 存储库、源目录或 URL/路径到 tar.gz 文件安装的集合。
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
还可以将角色添加到 requirements.yml 文件,位于 roles 键下。这些值遵循与在较早的 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 以供离线使用
导航到集合页面。
单击 下载 tarball。
可能还需要手动下载任何依赖集合。
在剧本旁边安装集合
可以在项目中剧本旁边本地安装集合,而不是在系统的全局位置或 AWX 上安装。
在剧本旁边使用本地安装的集合有一些好处,例如
确保项目的所有用户都使用相同的集合版本。
使用自包含项目使在不同环境之间移动变得容易。提高可移植性还可以减少设置新环境时的开销。这在云环境中部署 Ansible 剧本时很有益。
在本地管理集合使您可以将它们与剧本一起版本化。
在本地安装集合可以将它们与具有多个项目的环境中的全局安装隔离开来。
以下是在 collections/ansible_collections/ 目录结构下,将集合与当前剧本相邻保存的示例。
./
├── play.yml
├── collections/
│ └── ansible_collections/
│ └── my_namespace/
│ └── my_collection/<collection structure lives here>
有关集合目录结构的详细信息,请参见 集合结构。
从源文件安装集合
Ansible 还可以通过多种方式从源目录安装
collections:
# directory containing the collection
- source: ./my_namespace/my_collection/
type: dir
# directory containing a namespace, with collections as subdirectories
- source: ./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 [email protected]: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 默认情况下会从以下存储库结构中安装集合 1 和集合 2
├── 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 并单击 显示 API 密钥。
注意
每个服务器名称的 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 证书。除非提供了--ignore-certs选项或将GALAXY_IGNORE_CERTS配置为 True,否则此选项默认为 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