トキメk…じゃなくてAnsible RunnerやAnsible Builderの話で、Ansibleとコンテナ環境の話題がとても盛り上がってるところに乗り遅れつつある*1ので、マイペースに動作確認してみた作業ログ。
コンテナの中身寄り情報あり。
環境
Fedora 34で確認。
[zaki@fedora-node ~]$ cat /etc/fedora-release
Fedora release 34 (Thirty Four)
Pythonは3.9.4
(runner) [zaki@fedora-node ~]$ python --version
Python 3.9.4
BuilderとRunnerのバージョン
(runner) [zaki@fedora-node ~]$ ansible-builder --version
1.0.1
(runner) [zaki@fedora-node ~]$ ansible-runner --version
2.0.1
ちなみにDockerとPodmanは両方入ってる環境。
周りがみんなDocker使ってるのでPodmanで確認。
(runner) [zaki@fedora-node ~]$ podman --version
podman version 3.2.2
(runner) [zaki@fedora-node ~]$ docker --version
Docker version 20.10.6, build 370c289
Podmanはdnf
でシュッっとインストールできて設定も特に不要。
$ sudo dnf install podman
ちなみに、Ansible RunnerとBuilderはデフォルトではコンテナエンジンにPodmanが使用される。
Ansible Builder
[zaki@fedora-node ~]$ python -m venv python/venv/runner
[zaki@fedora-node ~]$ . python/venv/runner/bin/activate
(runner) [zaki@fedora-node ~]$
(runner) [zaki@fedora-node ~]$ pip install --upgrade pip
インストール
ansible-builder.readthedocs.io
pip install ansible-builder
(runner) [zaki@fedora-node ~]$ pip install --upgrade pip
Requirement already satisfied: pip in ./python/venv/runner/lib/python3.9/site-packages (21.0.1)
Collecting pip
Using cached pip-21.1.3-py3-none-any.whl (1.5 MB)
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 21.0.1
Uninstalling pip-21.0.1:
Successfully uninstalled pip-21.0.1
Successfully installed pip-21.1.3
(runner) [zaki@fedora-node ~]$ pip install ansible-builder
Collecting ansible-builder
Downloading ansible_builder-1.0.1-py3-none-any.whl (16 kB)
Collecting bindep
Downloading bindep-2.9.0-py2.py3-none-any.whl (32 kB)
Collecting PyYAML
Using cached PyYAML-5.4.1-cp39-cp39-manylinux1_x86_64.whl (630 kB)
Collecting requirements-parser
Downloading requirements-parser-0.2.0.tar.gz (6.3 kB)
Collecting Parsley
Downloading Parsley-1.3-py2.py3-none-any.whl (88 kB)
|████████████████████████████████| 88 kB 3.6 MB/s
Collecting distro
Using cached distro-1.5.0-py2.py3-none-any.whl (18 kB)
Collecting pbr>=2.0.0
Downloading pbr-5.6.0-py2.py3-none-any.whl (111 kB)
|████████████████████████████████| 111 kB 10.8 MB/s
Using legacy 'setup.py install' for requirements-parser, since package 'wheel' is not installed.
Installing collected packages: pbr, Parsley, distro, requirements-parser, PyYAML, bindep, ansible-builder
Running setup.py install for requirements-parser ... done
Successfully installed Parsley-1.3 PyYAML-5.4.1 ansible-builder-1.0.1 bindep-2.9.0 distro-1.5.0 pbr-5.6.0 requirements-parser-0.2.0
(runner) [zaki@fedora-node ansible-ee]$ ansible-builder --version
1.0.1
execution environment definition
ansible-builder.readthedocs.io
YAMLファイルを作成するようになってるけど、特にファイル名の指定がなさそう?
と思ったけどこっちか。
ansible-builder.readthedocs.io
デフォルトではカレントのexecution-environment.yml
を見るっぽい。
(runner) [zaki@fedora-node ansible-ee]$ ansible-builder build --help
:
:
optional arguments:
-h, --help show this help message and exit
:
-f FILENAME, --file FILENAME
The definition of the execution environment (default: execution-environment.yml)
なるほど、ファイル名は任意に指定可能みたい。
とりあえずデフォルトのexecution-environment.yml
でやっていく。
以下の定義はExecution Environment Definitionから。
---
version: 1
build_arg_defaults:
EE_BASE_IMAGE: 'quay.io/ansible/ansible-runner:stable-2.10-devel'
ansible_config: 'ansible.cfg'
dependencies:
galaxy: requirements.yml
python: requirements.txt
system: bindep.txt
additional_build_steps:
prepend: |
RUN whoami
RUN cat /etc/os-release
append:
- RUN echo This is a post-install command!
- RUN ls -la /etc
ベースイメージに指定してるのは、サンプル通りでquay.io/ansible/ansible-runner:stable-2.10-devel
で、Ansible 2.10のイメージ。
選択できるベースのイメージのタグは、現時点(2021.07.22)でこの辺りかな?
quay.io
- stable-2.11-devel (latest)
- stable-2.10-devel
- stable-2.9-devel
dependencies
に指定するのは、
- galaxy:
ansible-galaxy collection install
するコレクションの定義
- requirements.txt: 追加で
pip install
するPythonパッケージの定義
- bindep.txt: おそらく、OS(今回はコンテナだけど)で追加インストールするパッケージ一覧っぽい
docs.opendev.org
requirements.yml
お試しでKubernetesとNetboxのコレクションを指定。
---
collections:
- name: kubernetes.core
version: 2.1.1
- name: netbox.netbox
version: 3.1.1
requirements.txt
たとえばjson_query
使う場合などはこんな感じ。
jmespath
bindep.txt
お試しで以下の通り。
git
コントロールノード上に必要なコマンドを追加しないといけないときはこれに指定すれば良さそう。
ビルド
まずはオプション何も指定せずにやってみる。
(イメージビルドでtagしていないといろいろアレだけど)
(runner) [zaki@fedora-node ansible-ee]$ ansible-builder build
Running command:
podman build -f context/Containerfile -t ansible-execution-env:latest context
...showing last 20 lines of output...
--> e34f056f5eb
STEP 3: USER root
--> 9a704c0920c
STEP 4: ADD _build/ansible.cfg ~/.ansible.cfg
--> 17c5ffa6fb5
STEP 5: ADD _build /build
--> 95fe088171b
STEP 6: WORKDIR /build
--> 66c5fcbc890
STEP 7: RUN ansible-galaxy role install -r requirements.yml --roles-path /usr/share/ansible/roles
Skipping install, no requirements found
--> fe014f0f998
STEP 8: RUN ansible-galaxy collection install $ANSIBLE_GALAXY_CLI_COLLECTION_OPTS -r requirements.yml --collections-path /usr/share/ansible/collections
Starting galaxy collection install process
Process install dependency map
ERROR! Invalid collection name 'netbox', name must be in the format <namespace>.<collection>.
Please make sure namespace and collection name contains characters from [a-zA-Z0-9_] only.
STEP 9: FROM quay.io/ansible/ansible-builder:latest AS builder
Trying to pull quay.io/ansible/ansible-builder:latest...
Error: error building at STEP "RUN ansible-galaxy collection install $ANSIBLE_GALAXY_CLI_COLLECTION_OPTS -r requirements.yml --collections-path /usr/share/ansible/collections": error while running runtime: exit status 1
An error occured (rc=125), see output line(s) above for details.
(runner) [zaki@fedora-node ansible-ee]$
ん、netboxのcollectionのFQCN指定ミスりました。(netbox.netbox
にすべきところをnetbox
だけしか書いてなかった)
出力を見た感じ、中身は普通のイメージビルドね。あとdocker
でなくpodman
が使われている。
(runner) [zaki@fedora-node ansible-ee]$ ansible-builder build --help
:
:
--container-runtime {podman,docker}
Specifies which container runtime to use (default: podman)
(runner) [zaki@fedora-node ansible-ee]$ ansible-builder build
File context/_build/requirements.yml had modifications and will be rewritten
Running command:
podman build -f context/Containerfile -t ansible-execution-env:latest context
Complete! The build context can be found at: /home/zaki/ansible-ee/context
できた。(time
つけておけばよかった)
(runner) [zaki@fedora-node ansible-ee]$ podman image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/ansible-execution-env latest 79a18fb0244f 5 minutes ago 800 MB
<none> <none> 0d9f9ebaef03 6 minutes ago 677 MB
<none> <none> 83aa24b33b99 7 minutes ago 729 MB
<none> <none> fe014f0f9986 9 minutes ago 723 MB
quay.io/ansible/ansible-runner stable-2.10-devel 6354f87b1cb2 7 hours ago 723 MB
quay.io/ansible/ansible-builder latest c38a5c4514a4 7 hours ago 630 MB
ベースが723MBでそれなりにあって、ビルド結果は800MBになってる。
イメージの中身をちょっと見てみる
(runner) [zaki@fedora-node ansible-ee]$ podman run -it --rm localhost/ansible-execution-env bash
これでコンテナのbash起動できる。(この辺りはDocker/Podmanの使い方)
bash-4.4# cat /etc/redhat-release
CentOS Linux release 8.4.2105
ディストリビューションは現状ではCentOS 8だった。
ここはたぶんディストリビューションに依存する使い方にはならないと思うので(apt系では無いくらいを覚えておけば)あまり気にしなくていいかな?
bindepの指定
bash-4.4# git --version
git version 2.27.0
入っている。
(runner) [zaki@fedora-node ~]$ podman run -it --rm quay.io/ansible/ansible-runner:stable-2.10-devel bash
bash-4.4# git --version
git version 2.27.0
と思ったけど、ベースイメージの時点で最初から入っていた。(git
をインストールする、という指定は不要かな)
後述したけどgcc
を入れれば、インストールされることを確認。
bash-4.4# pip list
Package Version
----------------- ---------------
ansible-base 2.10.12.post0
ansible-runner 2.0.0.0a4.dev61
asn1crypto 1.2.0
Babel 2.7.0
bcrypt 3.2.0
cachetools 4.2.2
certifi 2021.5.30
cffi 1.13.2
chardet 3.0.4
cryptography 2.8
decorator 5.0.9
docutils 0.17.1
dumb-init 1.2.5
google-auth 1.33.1
gssapi 1.6.14
idna 2.8
Jinja2 2.10.3
jmespath 0.10.0
jsonpatch 1.32
jsonpointer 2.1
kubernetes 17.17.0
lockfile 0.12.2
lxml 4.4.1
MarkupSafe 1.1.1
ncclient 0.6.12
ntlm-auth 1.5.0
oauthlib 3.1.1
packaging 21.0
paramiko 2.7.2
pexpect 4.8.0
pip 21.1.3
ply 3.11
ptyprocess 0.7.0
pyasn1 0.4.8
pyasn1-modules 0.2.8
pycparser 2.19
pykerberos 1.2.1
PyNaCl 1.4.0
pyOpenSSL 19.1.0
pyparsing 2.4.7
pypsrp 0.5.0
PySocks 1.7.1
pyspnego 0.1.6
python-daemon 2.3.0
python-dateutil 2.8.2
pytz 2019.3
pywinrm 0.4.2
PyYAML 5.4.1
requests 2.22.0
requests-credssp 1.2.0
requests-ntlm 1.1.0
requests-oauthlib 1.3.0
rsa 4.7.2
setuptools 41.6.0
six 1.12.0
toml 0.10.2
urllib3 1.25.7
websocket-client 1.1.0
wheel 0.33.6
xmltodict 0.12.0
jmespath
が入っている。
今回の場合の、ベースイメージとの差分はこんな感じ。
[zaki@fedora-node ~]$ diff -u <(podman run --rm quay.io/ansible/ansible-runner:stable-2.10-devel pip freeze | sort) <(podman run --rm localhost/ansible-execution-env pip freeze | sort)
--- /dev/fd/63 2021-07-22 16:57:46.000908246 +0900
+++ /dev/fd/62 2021-07-22 16:57:46.000908246 +0900
@@ -8,24 +8,33 @@
ansible-runner @ file:///output/wheels/ansible_runner-2.0.0.0a4.dev61-py3-none-any.whl
asn1crypto==1.2.0
bcrypt==3.2.0
+cachetools==4.2.2
+certifi==2021.5.30
cffi==1.13.2
chardet==3.0.4
cryptography==2.8
decorator==5.0.9
docutils==0.17.1
dumb-init==1.2.5
+google-auth==1.33.1
gssapi==1.6.14
idna==2.8
+jmespath==0.10.0
+jsonpatch==1.32
+jsonpointer==2.1
+kubernetes==17.17.0
lockfile==0.12.2
lxml==4.4.1
ncclient==0.6.12
ntlm-auth==1.5.0
+oauthlib==3.1.1
packaging==21.0
paramiko==2.7.2
pexpect==4.8.0
ply==3.11
ptyprocess==0.7.0
pyOpenSSL==19.1.0
+pyasn1-modules==0.2.8
pyasn1==0.4.8
pycparser==2.19
pykerberos==1.2.1
@@ -33,12 +42,16 @@
pypsrp==0.5.0
pyspnego==0.1.6
python-daemon==2.3.0
+python-dateutil==2.8.2
pytz==2019.3
pywinrm==0.4.2
requests-credssp==1.2.0
requests-ntlm==1.1.0
+requests-oauthlib==1.3.0
requests==2.22.0
+rsa==4.7.2
six==1.12.0
toml==0.10.2
urllib3==1.25.7
+websocket-client==1.1.0
xmltodict==0.12.0
コレクション
bash-4.4# ansible-galaxy collection list
# /usr/share/ansible/collections/ansible_collections
Collection Version
--------------- -------
kubernetes.core 2.1.1
netbox.netbox 3.1.1
Ansibleバージョン
bash-4.4# ansible --version
ansible 2.10.12.post0
config file = None
configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.8.6 (default, Jan 29 2021, 17:38:16) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
シェル起動直下のファイル
bash-4.4# pwd
/runner
bash-4.4# ls -F
artifacts/ env/ inventory/ project/
bash-4.4# find
.
./artifacts
./env
./inventory
./project
ビルドに使われたファイル
Dockerfile相当のファイルなどは、context/Containerfile
に出力される。
イメージに組み込まれたファイル類はcontext/_build/
以下。
(runner) [zaki@fedora-node ansible-ee]$ find context/
context/
context/_build
context/_build/requirements.yml
context/_build/requirements.txt
context/_build/bindep.txt
context/_build/ansible.cfg
context/Containerfile
Dockerfile
に相当するcontext/Containerfile
の中身は以下の通り。
マルチステージビルドで3段階のビルドを行ってる。
ARG EE_BASE_IMAGE=quay.io/ansible/ansible-runner:stable-2.10-devel
ARG EE_BUILDER_IMAGE=quay.io/ansible/ansible-builder:latest
FROM $EE_BASE_IMAGE as galaxy
ARG ANSIBLE_GALAXY_CLI_COLLECTION_OPTS=
USER root
ADD _build/ansible.cfg ~/.ansible.cfg
ADD _build /build
WORKDIR /build
RUN ansible-galaxy role install -r requirements.yml --roles-path /usr/share/ansible/roles
RUN ansible-galaxy collection install $ANSIBLE_GALAXY_CLI_COLLECTION_OPTS -r requirements.yml --collections-path /usr/share/ansible/collections
FROM $EE_BUILDER_IMAGE as builder
COPY --from=galaxy /usr/share/ansible /usr/share/ansible
ADD _build/requirements.txt requirements.txt
ADD _build/bindep.txt bindep.txt
RUN ansible-builder introspect --sanitize --user-pip=requirements.txt --user-bindep=bindep.txt --write-bindep=/tmp/src/bindep.txt --write-pip=/tmp/src/requirements.txt
RUN assemble
FROM $EE_BASE_IMAGE
USER root
RUN whoami
RUN cat /etc/os-release
COPY --from=galaxy /usr/share/ansible /usr/share/ansible
COPY --from=builder /output/ /output/
RUN /output/install-from-bindep && rm -rf /output/wheels
RUN echo This is a post-install command!
RUN ls -la /etc
リビルドする場合 (bindep.txtにgcc追加)
例えばbindep.txt
を更新した場合でも、再実行すればbuilderがちゃんと検知してくれる。
git
gcc
gcc
を追加して再実行。今度はtag指定あり。
(runner) [zaki@fedora-node ansible-ee]$ ansible-builder build -t devel:latest
File context/_build/bindep.txt had modifications and will be rewritten
Running command:
podman build -f context/Containerfile -t devel:latest context
Complete! The build context can be found at: /home/zaki/ansible-ee/context
(runner) [zaki@fedora-node ansible-ee]$ podman image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/devel latest 131d82f1aa27 45 seconds ago 886 MB
<none> <none> 50d91977e012 2 minutes ago 783 MB
<none> <none> acf4c57bb6f6 3 minutes ago 729 MB
localhost/ansible-execution-env latest 79a18fb0244f 16 hours ago 800 MB
<none> <none> 0d9f9ebaef03 16 hours ago 677 MB
<none> <none> 83aa24b33b99 16 hours ago 729 MB
<none> <none> fe014f0f9986 16 hours ago 723 MB
quay.io/ansible/ansible-runner stable-2.10-devel 6354f87b1cb2 23 hours ago 723 MB
quay.io/ansible/ansible-builder latest c38a5c4514a4 23 hours ago 630 MB
(runner) [zaki@fedora-node ansible-ee]$ podman run -it --rm localhost/devel bash
bash-4.4# gcc --version
gcc (GCC) 8.4.1 20200928 (Red Hat 8.4.1-1)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
ちゃんとパッケージインストールも確認できた。
これはデフォルトでは入っていない。
(runner) [zaki@fedora-node ansible-ee]$ podman run -it --rm quay.io/ansible/ansible-runner:stable-2.10-devel bash
bash-4.4# gcc
bash: gcc: command not found
Ansible Runner
ansible-runner.readthedocs.io
ん-、こっちはGetting StartedとかQuick Startみたいな節がない。
Introduction to Ansible Runner かな?
install
ansible-runner.readthedocs.io
Runnerはインストール方法がpip install
だけでなく、パッケージインストールもできる模様。
builderで作ったイメージの実行方法は環境によっていろいろ合わせられるって感じかな。
とりあえずpip
で。
(runner) [zaki@fedora-node ~]$ pip install ansible-runner
Collecting ansible-runner
Downloading ansible_runner-2.0.1-py3-none-any.whl (77 kB)
|████████████████████████████████| 77 kB 2.6 MB/s
Collecting python-daemon
Downloading python_daemon-2.3.0-py2.py3-none-any.whl (35 kB)
Requirement already satisfied: pyyaml in ./python/venv/runner/lib/python3.9/site-packages (from ansible-runner) (5.4.1)
Collecting pexpect>=4.5
Downloading pexpect-4.8.0-py2.py3-none-any.whl (59 kB)
|████████████████████████████████| 59 kB 1.7 MB/s
Collecting six
Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting ptyprocess>=0.5
Downloading ptyprocess-0.7.0-py2.py3-none-any.whl (13 kB)
Collecting lockfile>=0.10
Downloading lockfile-0.12.2-py2.py3-none-any.whl (13 kB)
Requirement already satisfied: setuptools in ./python/venv/runner/lib/python3.9/site-packages (from python-daemon->ansible-runner) (53.0.0)
Collecting docutils
Downloading docutils-0.17.1-py2.py3-none-any.whl (575 kB)
|████████████████████████████████| 575 kB 12.8 MB/s
Installing collected packages: ptyprocess, lockfile, docutils, six, python-daemon, pexpect, ansible-runner
Successfully installed ansible-runner-2.0.1 docutils-0.17.1 lockfile-0.12.2 pexpect-4.8.0 ptyprocess-0.7.0 python-daemon-2.3.0 six-1.16.0
(runner) [zaki@fedora-node ~]$ ansible-runner --version
2.0.1
adhoc (失敗)
ansible-runner.readthedocs.io
Running Ansible adhocの項。
(runner) [zaki@fedora-node ansible-runner]$ ansible-runner adhoc localhost -m ping
usage: ansible-runner [-h] [--version] [--debug] [--logfile LOGFILE] [-b BINARY]
[-i IDENT] [--rotate-artifacts ROTATE_ARTIFACTS]
[--artifact-dir ARTIFACT_DIR] [--project-dir PROJECT_DIR]
[--inventory INVENTORY] [-j] [--omit-event-data]
[--only-failed-event-data] [-q] [-v]
{run,start,stop,is-alive,transmit,worker,process} ...
ansible-runner: error: argument command: invalid choice: 'adhoc' (choose from 'run', 'start', 'stop', 'is-alive', 'transmit', 'worker', 'process')
サブコマンド無かった。
(runner) [zaki@fedora-node ~]$ ansible-runner --help
[...]
subcommands:
COMMAND PRIVATE_DATA_DIR [ARGS]
{run,start,stop,is-alive,transmit,worker,process}
Command to invoke
run Run ansible-runner in the foreground
start Start an ansible-runner process in the background
stop Stop an ansible-runner process that's running in the background
is-alive Check if a an ansible-runner process in the background is still running.
transmit Send a job to a remote ansible-runner process
worker Execute work streamed from a controlling instance
process Receive the output of remote ansible-runner work and distribute the results
というか、次の項の「Running Ansible ansible-playbook」のplaybook
ってサブコマンドも無いような…見てるドキュメントが違うとかバージョンが違うとかかな。。
ansible-runner.readthedocs.io
こっちかな?
とりあえずadhoc
あたりのサブコマンド周りはスキップ。
Runner Input Directory Hierarchyを見ると、実行の基準のディレクトリから以下のディレクトリを使うようになってる模様
ssh_key
なんかはenv
以下に置くらしい。
設定とplaybook作成
一番何もしてなさそうなplaybookをお試しで。
ディレクトリ構造ベースにファイル作成する。
以下で使ってるrunner-files
というディレクトリ名は任意 (引数で指定するだけ)
(runner) [zaki@fedora-node ~]$ find runner-files/
runner-files/
runner-files/env
runner-files/env/settings
runner-files/inventory
runner-files/project
runner-files/project/playbook.yml
分かりにくかったけど、env/settings
で実行環境としてコンテナ(podman
やdocker
)を使う設定にないと、実行ノードのansible-playbook
を探して実行しようとするため、入れていない場合はエラーになる。
(runner) [zaki@fedora-node ~]$ ansible-runner run runner-files/ -p playbook.yml
The command was not found or was not executable: ansible-playbook.
env/settings
は最低限以下の通り。
process_isolation
をtrue
指定し、process_isolation_executable
にコンテナエンジンを指定。
container_image
に実行するAnsible Runnerのイメージ名を指定。(これがいままでのvenvに相当する)
process_isolation: true
process_isolation_executable: podman
container_image: localhost/ansible-execution-env:latest
project/playbook.yml
は以下の通り。
---
- hosts: localhost
gather_facts: false
tasks:
- name: ping
ping:
実行時のplaybookの指定は-p
で、ノードOS上のplaybookのファイルパスではなく、project
ディレクトリ内のファイル名を指定する。
(runner) [zaki@fedora-node ~]$ ansible-runner run runner-files/ -p playbook.yml
[WARNING]: Unable to parse /runner/inventory/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [ping] ********************************************************************
ok: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
一度実行すると、artifactsディレクトリができる。
(runner) [zaki@fedora-node ~]$ ls runner-files/artifacts/
0e86e736-127b-4f31-ba87-7582fb150861 5f237ac7-eaa6-41de-9ff2-c56cb9529061
35951222-37e1-42ba-b9cd-7886a4f2e674 ba21e63e-5589-450c-85f4-cada04600d40
中を確認すると、Ansibleの実行結果やログなどが格納される。
実行後にコンテナの残骸は残らないのでここを確認すればよさそう。
(runner) [zaki@fedora-node ~]$ podman container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-v
付けてもちゃんと反応する。
(runner) [zaki@fedora-node ~]$ ansible-runner run runner-files/ -p playbook.yml -v
No config file found; using defaults
[WARNING]: Unable to parse /runner/inventory/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [ping] ********************************************************************
ok: [localhost] => {"changed": false, "ping": "pong"}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Ansible Runnerを実行した時のコンテナIDがディレクトリ名に使われてると思うけど、どのIDで実行されてるかが実行時にわからないな。。
ちなみに実行時のlocalhost
はノードOSでなくコンテナ内。
- name: print hostname
debug:
msg: "{{ lookup('pipe', 'hostname') }}"
これを実行すると、
TASK [print hostname] **********************************************************
ok: [localhost] => {
"msg": "949bae49d041"
}
こんな感じ。
コレクション使用 + ssh接続のターゲットノード
せっかくKubernetesコレクションを入れたので、podの状態を拾うplaybookをproject/k8s.yml
に作成。
---
- hosts: k8s_cluster
gather_facts: false
tasks:
- name: k8s get pods
kubernetes.core.k8s_info:
namespace: example
kind: Pod
インベントリはディレクトリ構成通りinventory/hosts
に作成
[k8s_cluster]
192.168.0.44
接続ユーザー
ターゲットノードへの接続情報ないのでエラーになるのはわかってるけど、ひとまずこれで実行。
(runner) [zaki@fedora-node ~]$ ansible-runner run -v runner-files/ -p k8s.yml
No config file found; using defaults
PLAY [k8s_cluster] *************************************************************
TASK [k8s get pods] ************************************************************
fatal: [192.168.0.44]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.0.44' (ECDSA) to the list of known hosts.\r\nroot@192.168.0.44: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}
PLAY RECAP *********************************************************************
192.168.0.44 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
なるほど、root
ユーザーで接続しようとしてる。というよりはユーザー名未指定なので、コンテナ内の実行ユーザーが使われてて、それがroot
ってことだと思われる。
(runner) [zaki@fedora-node ~]$ podman run -it --rm localhost/ansible-execution-env iduid=0(root) gid=0(root) groups=0(root)
ユーザー名を以下のように指定して、
[k8s_cluster]
192.168.0.44 ansible_user=zaki
まだ認証情報の設定してないのでエラーになる想定だけどユーザー名は制御できてるかの確認。
(runner) [zaki@fedora-node ~]$ ansible-runner run -v runner-files/ -p k8s.yml
No config file found; using defaults
PLAY [k8s_cluster] *************************************************************
TASK [k8s get pods] ************************************************************
fatal: [192.168.0.44]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.0.44' (ECDSA) to the list of known hosts.\r\nzaki@192.168.0.44: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}
PLAY RECAP *********************************************************************
192.168.0.44 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
接続先がzaki@192.168.0.44
になってる。大丈夫。
ベストプラクティスとは思えないけど必要な秘密鍵を配置
(runner) [zaki@fedora-node ~]$ cp .ssh/id_rsa runner-files/env/ssh_key
env/ssh_keyのドキュメントを見る限り、現バージョンではsshの秘密鍵は一つしか使用できないようだけど、既に複数ファイルを使用できるような対応が進んでる模様。
実行
(runner) [zaki@fedora-node ~]$ ansible-runner run -v runner-files/ -p k8s.yml
Identity added: /runner/artifacts/d690476a-e0d3-4fcb-98ea-1924e4deeb91/ssh_key_data (/runner/artifacts/d690476a-e0d3-4fcb-98ea-1924e4deeb91/ssh_key_data)
No config file found; using defaults
PLAY [k8s_cluster] *************************************************************
TASK [k8s get pods] ************************************************************
ok: [192.168.0.44] => {"api_found": true, "changed": false, "resources": [{"apiVersion": "v1", "kind": "Pod", "metadata": {"creationTimestamp": "2021-05-16T23:36:35Z", "generateName": "sample-http-6c94f59975-", "labels": {"app": "sample-http", "pod-template-hash": "6c94f59975"}, "managedFields": [{"apiVersion": "v1", "fieldsType": "FieldsV1", "fieldsV1": { ......
動きました。
(Kubernetesモジュールはターゲットにopenshift
パッケージが必要などのモジュール特有の要件は従来通り)
Dockerの場合
指定方法は--help
で確認できる。
実行は未確認。
builder
$ ansible-builder build --container-runtime docker
runner
env/setting
ファイルのprocess_isolation_executable
にdocker
指定するか、
$ ansible-runner run --process-isolation-executable docker
かな?
ざっくりまとめ
- これからのAnsible実行環境はvevnでなくコンテナ
- 実行環境としてのコンテナイメージがAnsible Runner
- 今までvenvなどで作ってた自分用の実行環境はAnsible Runnerのベースイメージでビルドする
- ビルドに使用するのがAnsible Builder
そういえばコンテナ実行になると、Docker Connectionプラグインとかちゃんと使えるのかな?dindな構成になるのかな。
あと、OSパッケージインストール周りでリポジトリ追加したい場合なんかは現状不明。
リポジトリ追加したい場合のビルドについて書きました。
zaki-hmkc.hatenablog.com
参考情報
usage-automate.hatenablog.com
ansible-builder.readthedocs.io
ansible-runner.readthedocs.io
さらばvenv
コレクションのインストールとか、venv使ったPythonのランタイム関係ファイルのみを隔離した環境って、Ansible視点だと事故起こり過ぎで好きじゃなかったのでコンテナ利用は嬉しいな。