コンテナ化されたAnsible実行環境の周辺ツールまとめ(インフラ技術基礎勉強会#4)

4.6K Views

September 03, 23

スライド概要

インフラ技術基礎勉強会 #4 で発表させていただいた資料です。
https://infra365.connpass.com/event/286297/

AnsibleはPython製のIT自動化ツールです。
ここ数年で、Ansible の実行環境がコンテナ化され、周辺ツールも充実してきています。ansible-builder や ansible-navigator など、比較的新しいツールの概要を簡単にご紹介します。

動画
https://www.youtube.com/watch?v=xStYySbOxuA&t=4066s

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

コンテナ化されたAnsible実行環境の 周辺ツールまとめ 2023/09/03 インフラ技術基礎勉強会 #4 株式会社エーピーコミュニケーションズ 横地 晃 (@akira6592) 1

2.

はじめに ◼ AnsibleはPython製のIT自動化ツールです ◼ ここ数年で、Ansible の実行環境がコンテナ化され、 周辺ツールも充実してきています ◼ ansible-builder や ansible-navigator など、比較的新しいツールの 概要を簡単にご紹介します [想定対象者] ・Ansibleを知っている人 ・2021年以降のAnsible事情をキャッチアップしたい人 2

3.

自己紹介 横地 晃 @akira6592 てくなべ(ブログ) 所属 (株)エーピーコミュニケーションズ 業務 ネットワーク自動化のご支援 https://tekunabe.hatenablog.jp/ コミュニ Ansible ユーザー会、JANOG ティ 共著 Ansible クックブック Ansible 実践ガイド 第4版[基礎編]、他 https://book.impress.co.jp/books/1120101163 https://book.impress.co.jp/books/1122101189 3

4.

そもそも Ansible とは 4

5.

IT自動化ツール「Ansible」 ◼ ◼ Linux、Windows、クラウド、ネットワーク機器等の操作を自動化 自動化したい処理を Playbook に定義 ↓ Playbook の例 --- hosts: ios gather_facts: false 処理 tasks: - name: config IP address cisco.ios.ios_l3_interfaces: config: - name: GigabitEthernet3 ipv4: - address: 10.1.3.254/24 5

6.

コンテナ化が進んできた背景 (主観含む) 6

7.

venv からコンテナへ ◼ ◼ ◼ 元々は Ansibleの実行環境は venv で隔離することが多かった ポータビリティが低く、環境によってPlaybookが実行できないこともあった コンテナ化することでポータビリティを高くする動きが加速(2021年頃) ◼ Ansible の実行に必要なものをいれたコンテナイメージを Execution Environment (EE、実行環境)と呼ぶ 【venv ベースの環境分離】 Ansible Ansible Python パッケージ Python パッケージ venv venv システムパッケージ ホスト 【コンテナ ベースの環境分離】 コレクション コレクション Ansible Ansible Python パッケージ Python パッケージ システムパッケージ システムパッケージ コンテナ(EE) コンテナ(EE) ホスト 7 ※ コレクションは venv 内外どちらも可のため図では省略

8.

周辺ツールの全体像 EEを作る側 ansible-builder ビルド EEを使う側 ansible-navigator Execution Environment 処理 Automation Controller 8

9.

EEを作る側 ansible-builder Ansible の実行環境コンテナイメージをビルドする 9

10.

ansible-builder の概要 ◼ ◼ EE をビルドするツール ビルド定義ファイル(execution-environment.yml)を用意しておく ◼ ◼ podman/docker における Containerfile/Dockerfile のようなもの コレクションが依存するパッケージも自動インストールしてくれる ansible-builder ビルド Execution Environment 10

11.

定義ファイルの例 --version: 3 images: base_image: # ベースイメージの指定 name: quay.io/centos/centos:stream9 dependencies: ansible_core: # ansible-core の指定 package_pip: ansible-core==2.15.2 ansible_runner: # ansible-runner の指定 package_pip: ansible-runner galaxy: collections: # コレクションの指定 - name: ansible.netcommon - name: azure.azcollection version: 1.17.0 python: # Python パッケージの指定 - paramiko - ansible-pylibssh system: # システムパッケージの指定 - openssl ベースイメージ、ansible-core、 各種パッケージなどを指定する ※ 公式ドキュメント上のサンプル https://ansible.readthedocs.io/projects/builder/en/latest/definition/#version-3-sample-file 11

12.

実行例 ◼ ansible-builder build コマンドでビルドする -v 3 で 詳細ログ表示 $ ansible-builder build -t example.com/my-ee -v 3 Ansible Builder is generating your execution environment build context. File context/_build/requirements.yml will be created. File context/_build/requirements.txt will be created. ...(略)... Ansible Builder is building your execution environment image. Tags: example.com/my-ee Running command: podman build -f context/Containerfile -t example.com/my-ee context ...(略)... Complete! The build context can be found at: /home/admin/git/ansible-ee/infra365/context $ podman images REPOSITORY TAG IMAGE ID CREATED SIZE イメージができた example.com/my-ee latest 60bd6b413f68 33 seconds ago 1.09 GB 12

13.

EEを使う側 ansible-navigator Playbook を EE 経由で実行する 13

14.

ansible-navigator の概要 ◼ ◼ EE 経由での Playbook の実行などができるツール(デフォルトでは TUI) どのEEを使うかなどを指定した設定ファイル(ansible-navigator.yml)を用意しておく ansible-navigator Execution Environment 処理 ※ 他にも EE やモジュールのドキュメント表示、ansible-builder のラッパーなどの機能もある 14

15.

設定ファイルの例 ◼ 利用イメージなどを指定する --ansible-navigator: execution-environment: # 利用イメージの指定 image: example.com/my-ee:latest pull: policy: missing # Playbook 実行ログの保存設定 playbook-artifact: enable: true save-as: "{playbook_dir}/artifacts/{playbook_name}-{time_stamp}.json" # 時刻タイムゾーン time-zone: Japan ※ 公式ドキュメント上のサンプル https://ansible.readthedocs.io/projects/navigator/settings/#the-ansible-navigator-settings-file 15

16.

実行例(TUI) ◼ ansible-navigator run コマンドで Playbook 実行(デフォルトでTUI) $ ansible-navigator run playbooks/test.yml -i inventory/ 実行結果の ドリルダウン表示ができる 16

17.
[beta]
実行例(CLI)
◼

ansible-navigator run の -m stdout オプションでおなじみの出力に

$ ansible-navigator run

playbooks/test.yml -i inventory/ -m stdout

PLAY [Test Play] ***************************************************************
TASK [Test Task] **************************************************************
ok: [localhost] => {
"msg": "Hello Ansible!"
}
PLAY RECAP ********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

17

18.

EEを使う側 Automation Controller Ansible を組織で活用するためのプロダクト 18

19.

Automation Controller の概要 ◼ ◼ Ansible を組織で活用するための Web UI を備えた有償プロダクト 実行履歴の管理、RBAC、REST API などに対応 ※ バージョン3までは Ansible Tower(EE非対応)と呼ばれていた 19

20.

設定と利用 ◼ Automation Controller としてのEEの設定をして、 ジョブテンプレート(Playbookの実行定義)で、どのEEを使うか指定する ジョブテンプレートの設定画面 EEの設定画面 このEEに名前を 付けて設定 使うEEの設定名 を指定 20

21.

まとめ 21

22.

まとめ ◼ ◼ ◼ ◼ Ansible の実行環境は コンテナ化により高いポータビリティを実現 Ansible の実行に必要なものを含めたコンテナイメージを Execution Environment(EE、実行環境)と呼ぶ EEを作る側: ansible-builder EEを使う側: ansible-navigator、Automation Controller EEを作る側 ansible-builder ビルド EEを使う側 ansible-navigator Automation Controller Execution Environment 処理 22

23.

参考資料 ◼ ansible-builder ◼ ◼ ansible-navigator ◼ ◼ https://ansible.readthedocs.io/projects/navigator/ Red Hat Ansible Automation Platform ◼ ◼ https://ansible.readthedocs.io/projects/builder/en/stable/ https://www.redhat.com/ja/technologies/management/ansible Getting started with Execution Environments ◼ https://docs.ansible.com/ansible/devel/getting_started_ee/index.html 23