Custom Execution Environmentを作成する

Ansible Builderを利用して、任意のZバージョン(X.Y.Z)のAnsible Coreを含むExecution Environmentイメージを作成する方法をメモしておく。

環境

Execution Environmentを、Fedora35の上でpodmanを利用してビルドする:

$ sudo lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: Fedora
Description:    Fedora release 35 (Thirty Five)
Release:    35
Codename:   ThirtyFive

$ podman -v
podman version 3.4.4

Ansible Builderのインストール

virtualenvとpipを利用して、ビルド環境を構築する:

$ mkdir work && cd work
$ python3.9 -m venv venv
$ source venv/bin/activate

virtualenvをアクティベートしてAnsible Builderをインストールする:

(venv) $ pip install -U pip
(venv) $ pip install ansible-builder
(venv) $ ansible-builder --version
1.0.1

作業用ディレクトリ(ee)を作成する:

(venv) $ mkdir ee && cd ee

ビルドファイルの作成

Ansible Builderを利用して、Execution Environmentのビルドするために必要な設定ファイル群を、公式サイトにしたがって作成する。

ディレクトリ構成
(venv) $ tree .

.
├── ansible.cfg
├── bindep.txt
├── ee.yml
├── requirements.txt
└── requirements.yml

ee.yml

(venv) $ cat ee.yml

---
version: 1

build_arg_defaults:
  EE_BASE_IMAGE: 'quay.io/ansible/ansible-runner:stable-2.12-devel'  #[1]

ansible_config: 'ansible.cfg'  #[2]

dependencies:
  galaxy: requirements.yml  #[3]
  python: requirements.txt  #[4]
  system: bindep.txt        #[5]

additional_build_steps:     #[6]
  prepend: |
    RUN whoami
    RUN cat /etc/os-release
  append:
    - RUN echo This is a post-install command!
    - RUN ls -la /etc
[1] quay.ioにあるベースイメージ(ansible-runner)のパスを指定する
  • Ansible Core 2.11の場合はquay.io/ansible/ansible-runner:stable-2.12-devel
  • Ansible Core 2.12の場合はquay.io/ansible/ansible-runner:stable-2.11-devel
[2] ansible.cfgのパスを指定する

[3]のrequirements.ymlで指定したcollectionsをansible-galaxyコマンドでダウンロードする際に利用する。Community Galaxy経由ではなく、独自にPrivate Automation HubGalaxy NGからCollectionsのコンテンツを提供している場合は、URLやTOKENなどの接続情報を指定することができる。

(venv) $ cat ansible.cfg

[galaxy]
#server_list = automation_hub

[galaxy_server.automation_hub]
#url=https://cloud.redhat.com/api/automation-hub/
#auth_url=https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token
#token=my_ah_token
[3] requirements.ymlのパスを指定する

Execution Environmentビルド時にダウンロードしておくCollectionsを、requirements.ymlに指定する。

ansible.posixやcommunity.generalなどのAnsible Releaseに含まれるCollectionsは、[4]でインストールするAnsibleに含まれるので、ここでは除外している。

$ cat requirements.yml

collections:
  - awx.awx
[4] requirements.ymlのパスを指定する
  • Execution Environmentにダウンロードして利用するAnsible CoreとAnsibleのバージョンを指定する。
  • [3]でダウンロードするCollectionsが必要とするPythonモジュール(botoなど)は、requirements.txtを設定する。
(venv) $ cat requirements.txt

ansible-core == 2.12.1  # Playbookの実行に利用したいAnsible Coreのバージョンを指定する
ansible == 5.1.0
awxkit  # for awx.awx collections
python-dateutil >= 2.7.0  # for awx.awx.collections
pytz  # for awx.awx collections
[5] bindep.txtのパスを指定する

Execution EnvironmentとなるコンテナイメージにインストールしたいOSパッケージの情報を指定する。

$ cat bindep.txt

tree [platform:rpm]
[6] ビルドプロセスの前後で実行するスクリプトを指定する
  • additional_build_steps.prepend - ビルドプロセス前に実行するスクリプト
  • additional_build_steps.append - ビルドプロセス後に実行するスクリプト

Execution Environmentのビルド

ansible-builderコマンドでExecution Environmentをビルドする:

(venv) $ ansible-builder build --tag=ee-ansible-v5:5.1.0 --file=./ee.yml

Running command:
  podman build -f context/Containerfile -t ee-ansible-v5:5.1.0 context
Complete! The build context can be found at: /home/hsaito/tmp/work/ee/context

できあがり:

(venv) $ podman images

REPOSITORY                       TAG                IMAGE ID      CREATED        SIZE
localhost/ee-ansible-v5          5.1.0              7792250bce14  2 minutes ago  1.33 GB
<none>                           <none>             1244d8886eff  3 minutes ago  1.24 GB
<none>                           <none>             7930ca783be2  6 minutes ago  764 MB
quay.io/ansible/ansible-runner   stable-2.12-devel  38fd0ce19a5c  12 hours ago   763 MB
quay.io/ansible/ansible-builder  latest             3e9c9e0198dc  12 hours ago   655 MB