搜索

4.12. 在断开连接的环境中构建执行环境

download PDF

为 Ansible Automation Platform 创建执行环境是一个常见任务,它在断开连接的环境中工作不同。https://docs.redhat.com/en/documentation/red_hat_ansible_automation_platform/2.4/html/creating_and_consuming_execution_environments/index在构建自定义执行环境时,ansible-builder 工具默认为从互联网上以下位置下载内容:

  • Red Hat Automation hub (console.redhat.com)或 Ansible Galaxy (galaxy.ansible.com)用于添加到执行环境镜像的任何 Ansible 内容集合。
  • PyPI (pypi.org),适用于作为集合依赖项所需的任何 python 软件包。
  • RPM 存储库,如 RHEL 或 UBI 软件仓库(cdn.redhat.com),用于在执行环境镜像中添加或更新 RPM (如果需要)。
  • registry.redhat.io 用于访问基本容器镜像。

在断开连接的环境中构建执行环境镜像需要从这些位置镜像内容。如需有关 将集合从 Ansible Ansible Galaxy 或自动化中心导入到私有自动化中心 的信息,请参阅将集合导入到私有自动化中心。

传送到断开连接的网络后镜像 PyPI 内容可以使用 Web 服务器或 Nexus 等工件存储库。RHEL 和 UBI 存储库内容可以从面向互联网的 Red Hat Satellite 服务器导出,复制到断开连接的环境中,然后导入到断开连接的 Satellite 中,以便构建自定义执行环境。详情请查看 Air-Gapped Scenario 中的 ISS 导出同步

默认基础容器镜像 ee-minimal-rhel8 用于创建自定义执行环境镜像,并包含在捆绑的安装程序中。在安装时,此镜像添加到私有自动化中心。如果需要不同的基础容器镜像,如 ee-minimal-rhel9,则必须将其导入到断开连接的网络中,并添加到私有自动化中心容器 registry 中。

断开连接的网络上所有先决条件后,可以使用 ansible-builder 命令来创建自定义执行环境镜像。

4.12.1. 安装 Ansible Builder RPM

在构建自定义虚拟环境的 RHEL 系统上,您将使用环境中已存在的 Satellite 服务器安装 Ansible Builder RPM。此方法是首选的,因为执行环境镜像可以根据需要使用来自预先存在的 Satellite 的任何 RHEL 内容。

流程

  1. 从 Ansible Automation Platform 存储库安装 Ansible Builder RPM。

    1. 将 RHEL 系统订阅到断开连接的网络上的 Satellite。
    2. 附加 Ansible Automation Platform 订阅并启用 AAP 存储库。存储库名称可以是 ansible-automation-platform-2.4-for-rhel-8-x86_64-rpmsansible-automation-platform-2.4-for-rhel-9-x86_64-rpms,具体取决于底层系统上使用的 RHEL 版本。
    3. 安装 Ansible Builder RPM。Ansible Builder RPM 的版本必须是 3.0.0 或更高版本,才能使以下示例正常工作。
  2. 从 Ansible Automation Platform 安装捆绑包安装 Ansible Builder RPM。如果断开连接的网络中没有 Satellite 服务器,则使用此方法。

    1. 解压缩 Ansible Automation Platform 安装捆绑包。
    2. 从包含的内容安装 Ansible Builder RPM 及其依赖项。
$ tar -xzvf ansible-automation-platform-setup-bundle-2.4-3-x86_64.tar.gz
$ cd ansible-automation-platform-setup-bundle-2.4-3-x86_64/bundle/packages/el8/repos/
$ sudo dnf install ansible-builder-3.0.0-2.el8ap.noarch.rpm \
    python39-requirements-parser-0.2.0-4.el8ap.noarch.rpm \
    python39-bindep-2.10.2-3.el8ap.noarch.rpm \
    python39-jsonschema-4.16.0-1.el8ap.noarch.rpm \
    python39-pbr-5.8.1-2.el8ap.noarch.rpm \
    python39-distro-1.6.0-3.el8pc.noarch.rpm \
    python39-packaging-21.3-2.el8ap.noarch.rpm \
    python39-parsley-1.3-2.el8pc.noarch.rpm \
    python39-attrs-21.4.0-2.el8pc.noarch.rpm \
    python39-pyrsistent-0.18.1-2.el8ap.x86_64.rpm \
    python39-pyparsing-3.0.9-1.el8ap.noarch.rpm
注意

根据所使用的安装捆绑包版本,特定版本可能稍有不同。

其他资源

4.12.2. 创建自定义执行环境定义

安装 Ansible Builder RPM 后,请按照以下步骤创建自定义执行环境。

  1. 为创建自定义执行环境时使用的构建工件创建一个目录。所有使用以下步骤创建的新文件都将在此目录中创建。

    $ mkdir $HOME/custom-ee $HOME/custom-ee/files
    $ cd $HOME/custom-ee/
  2. 创建一个 execution-environment.yml 文件,该文件定义自定义执行环境的要求。

    注意

    需要执行环境定义格式的版本 3,因此请确保 execution-environment.yml 文件包含 version: 3,然后再继续。

    1. 覆盖基础镜像以指向私有自动化中心中可用的最小执行环境。
    2. 定义额外的构建文件,以指向构建过程中要使用的任何断开连接的内容源。您的自定义 execution-environment.yml 文件应类似以下示例:
    $ cat execution-environment.yml
    ---
    version: 3
    
    images:
      base_image:
        name: private-hub.example.com/ee-minimal-rhel8:latest
    
    dependencies:
      python: requirements.txt
      galaxy: requirements.yml
    
    additional_build_files:
      - src: files/ansible.cfg
        dest: configs
      - src: files/pip.conf
        dest: configs
      - src: files/hub-ca.crt
        dest: configs
      # uncomment if custom RPM repositories are required
      #- src: files/custom.repo
      #  dest: configs
    
    additional_build_steps:
      prepend_base:
        # copy a custom pip.conf to override the location of the PyPI content
        - ADD _build/configs/pip.conf /etc/pip.conf
        # remove the default UBI repository definition
        - RUN rm -f /etc/yum.repos.d/ubi.repo
        # copy the hub CA certificate and update the trust store
        - ADD _build/configs/hub-ca.crt /etc/pki/ca-trust/source/anchors
        - RUN update-ca-trust
        # if needed, uncomment to add a custom RPM repository configuration
        #- ADD _build/configs/custom.repo /etc/yum.repos.d/custom.repo
    
      prepend_galaxy:
        - ADD _build/configs/ansible.cfg ~/.ansible.cfg
    
    ...
  3. files/ 子目录下创建一个 ansible.cfg 文件,指向您的私有自动化中心。

    $ cat files/ansible.cfg
    [galaxy]
    server_list = private_hub
    
    [galaxy_server.private_hub]
    url = https://private-hub.example.com/api/galaxy/
  4. files/ 子目录下创建一个 pip.conf 文件,指向内部 PyPI 镜像(Web 服务器或 Nexus):

    $ cat files/pip.conf
    [global]
    index-url = https://<pypi_mirror_fqdn>/
    trusted-host = <pypi_mirror_fqdn>
  5. 可选:如果使用 bindep.txt 文件来添加 RPM 自定义执行环境,请在 files/ 子目录下创建一个 custom.repo 文件,指向断开连接的 Satellite 或托管 RPM 存储库的其他位置。如果此步骤是必需的,请取消与 custom.repo 文件对应的 execution-environment.yml 示例中的步骤。

    以下示例是 UBI 仓库。也可以将其他本地仓库添加到此文件中。URL 路径可能需要根据镜像内容位于 web 服务器上的位置进行更改。

    $ cat files/custom.repo
    [ubi-8-baseos]
    name = Red Hat Universal Base Image 8 (RPMs) - BaseOS
    baseurl = http://<ubi_mirror_fqdn>/repos/ubi-8-baseos
    enabled = 1
    gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
    gpgcheck = 1
    
    [ubi-8-appstream]
    name = Red Hat Universal Base Image 8 (RPMs) - AppStream
    baseurl = http://<ubi_mirror_fqdn>/repos/ubi-8-appstream
    enabled = 1
    gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
    gpgcheck = 1
  6. 添加用于为私有自动化中心 web 服务器证书签名的 CA 证书。如果私有自动化中心使用安装程序提供的自签名证书:

    1. 从您的私有自动化中心中复制 /etc/pulp/certs/pulp_webserver.crt,并将其命名为 hub-ca.crt
    2. hub-ca.crt 文件添加到 files/ 子目录中。
  7. 如果私有自动化中心使用由证书颁发机构签名的用户提供的证书:

    1. 复制该 CA 证书并将其命名为 hub-ca.crt
    2. hub-ca.crt 文件添加到 files/ 子目录中。
  8. 完成预付步骤后,使用自定义执行环境镜像所需的内容创建 python requirements.txt 和 Ansible 集合 requirements.yml 文件。

    注意

    任何所需的集合都必须已上传到您的私有自动化中心。

    custom-ee/ 目录下应存在以下文件,其中 bindep.txtfiles/custom.repo 是可选的:

$ cd $HOME/custom-ee
$ tree .
.
├── bindep.txt
├── execution-environment.yml
├── files
│   ├── ansible.cfg
│   ├── custom.repo
│   ├── hub-ca.crt
│   └── pip.conf
├── requirements.txt
└── requirements.yml

1 directory, 8 files

其他资源

如需有关版本 3 格式和要求的更多信息,请参阅 执行环境定义: 版本 3 格式

4.12.3. 构建自定义执行环境

在创建新的自定义执行环境前,需要来自私有 hub 的 API 令牌来下载内容。

执行以下步骤生成令牌:

  1. 登录到您的私有自动化中心。
  2. 从左侧菜单中选择"集合"。
  3. 在菜单的 "Collections" 部分下选择"API 令牌"。
  4. 具有令牌后,设置以下环境变量以便 Ansible Builder 可以访问令牌:

    $ export ANSIBLE_GALAXY_SERVER_PRIVATE_HUB_TOKEN=<your_token>
  5. 使用以下命令创建自定义执行环境:

    $ cd $HOME/custom-ee
    $ ansible-builder build -f execution-environment.yml -t private-hub.example.com/custom-ee:latest -v 3
    注意

    如果构建失败,并显示私有 hub 证书由未知颁发机构签名的错误,您可以通过运行以下命令将所需的镜像拉取到本地镜像缓存中:

    $ podman pull private-hub.example.com/ee-minimal-rhel8:latest --tls-verify=false

    另外,您可以将私有 hub CA 证书添加到 podman 证书存储中:

    $ sudo mkdir /etc/containers/certs.d/private-hub.example.com
    $ sudo cp $HOME/custom-ee/files/hub-ca.crt /etc/containers/certs.d/private-hub.example.com

4.12.4. 将自定义执行环境上传到私有自动化中心

在新的执行环境镜像可用于自动化作业前,必须将其上传到私有自动化中心。

首先,验证执行环境镜像是否可以在本地 podman 缓存中看到:

$ podman images --format "table {{.ID}} {{.Repository}} {{.Tag}}"
IMAGE ID	    REPOSITORY					              TAG
b38e3299a65e	private-hub.example.com/custom-ee     	  latest
8e38be53b486	private-hub.example.com/ee-minimal-rhel8  latest

然后,登录到私有自动化中心的容器 registry 并推送镜像使其可用于作业模板和工作流:

$ podman login private-hub.example.com -u admin
Password:
Login Succeeded!
$ podman push private-hub.example.com/custom-ee:latest
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.