galaxy-importerについて
これは Ansible Advent Calendar 2023 の16日目の記事となります。
毎年この時期にしかブログ書いてないです、来年は沢山ポストしたいな。
はじめに
今年は夏位に2ヵ月程Ansibleの作業を行いまして、その際に galaxy-importer
というツールを利用する機会がありました。当時(本記事作成している 2024/12月時点も)このツールの使い方は公式情報(GitHub)しかなくて色々と苦労したので使い方や用途を少し共有しようと思います。
AnsibleGalaxyやAutomationHubにモジュールをアップロードする方は覚えておくとよいツールだと思います。
※公式ドキュメントもそこまで詳細にないので、認識の誤りなどあったらごめんなさい。
galaxy-importer概要
- AnsibleGalaxyやAutomationHubにモジュールをアップロードする際に内部で実行されるテストの実体
ansible-test sanity
とかansible-lint
を実行
- コマンド単体+コンテナ(podman, docker)環境で動かすことができる
- コンフィグファイルに指定する事で、AutomatinHubにアップロードする際のテストと同等の内容も行う事が出来る
概要はこんな感じと理解しています。
モジュールを作成してAnsibleGalaxyにアップロードしたいけど、アップロード時に行われるテスト結果をまずは手元で確認したい。
という用途で使えるツールです。
普通に ansible-test
などのコマンドを実行するより便利なところ
- 後述のテストオプションや結果を見てわかる通り
- 様々なPython環境でテストしてくれる
ansible-test
,ansible-lint
,ansible-doc
など個別のコマンドをまとめて実行してくれる- 実施にAnsibleGalaxyやAutomationHubにアップロードする際のテストが行える
が非常に良い所だと思っています。
使い方
環境構築
- venv環境で
galaxy-importer
を動かす環境を用意
(py39_galaxy-importer) [nakayama@test]$ pip install galaxy-importer
- テスト実行環境として、Docker or podman を用意する。
usage
(py39_galaxy-importer) [nakayama@test]$ python -m galaxy_importer.main --help
usage: main.py [-h] [--git-clone-path GIT_CLONE_PATH] [--output-path OUTPUT_PATH] [--print-result] [--legacy-role] [--namespace NAMESPACE] [file]
Run importer on collection and save result to disk.
positional arguments:
file artifact to import
optional arguments:
-h, --help show this help message and exit
--git-clone-path GIT_CLONE_PATH
git directory with collection that will get built
--output-path OUTPUT_PATH
path where built collection will be stored
--print-result print importer result to console
--legacy-role import a legacy role rather than collection
--namespace NAMESPACE
namespace of the legacy role to import
(py39_galaxy-importer) [nakayama@test]$
configファイル
export GALAXY_IMPORTER_CONFIG=/xxxx/xxx/xxxx//galaxy-importer.cfg
コンフィグ例
[galaxy-importer] LOG_LEVEL_MAIN = INFO RUN_ANSIBLE_TEST = True ANSIBLE_TEST_LOCAL_IMAGE = True
default値
公式ドキュメントにもあるように、オプション未指定時はデフォルトで動く。
デフォルト値は こちらの、DEFAULTS =
の部分
設定値 | DEFAULT | 内容 (分かるものだけ) |
---|---|---|
ansible_local_tmp | ~/.ansible/tmp | テスト実行時の一時ファイル置き場 |
ansible_test_local_image | False | |
check_required_tags | False | |
infra_osd | False | |
local_image_docker | False | テスト環境のコンテナ指定 |
log_level_main | INFO | ログレベル指定 |
require_v1_or_greater | False | |
run_ansible_doc | True | ansible-doc実行の有無 |
run_ansible_lint | True | ansible-lint実行の有無 |
offline_ansible_lint | True | |
run_ansible_test | False | ansible-test sanity実行の有無(※) |
run_flake8 | False | |
tmp_root_dir | None |
run_ansible_test
- デフォルト Falseになっている、GalaxyやAutomationHubにアップする際のテストを行う場合はここは
True
に True
にすると、ansible-test sanity
が実行されるので、Docker or Podman 環境が必要となる
- デフォルト Falseになっている、GalaxyやAutomationHubにアップする際のテストを行う場合はここは
local_image_docker
- False(Default)
- podman環境
ansible-test sanity
を実行
- podman環境
- True
- docker環境で
ansible-test sanity
を実行
- docker環境で
- False(Default)
実行例(cisco.ios)
(py39_galaxy-importer) [nakayama@test]$ python -m galaxy_importer.main cisco-ios-5.0.0.tar.gz Importing with galaxy-importer 0.4.13 Getting doc strings via ansible-doc Finding content inside collection Loading module ios_service Loading module ios_l3_interfaces : (省略) : Collection loading complete Building ContainerFile Building image... Running image... Extracting archive... Running 'ansible --version'... ansible [core 2.15.0] config file = None configured module search path = ['/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.9/dist-packages/ansible ansible collection location = /.ansible/collections:/usr/share/ansible/collections executable location = /usr/local/bin/ansible python version = 3.9.5 (default, Nov 23 2021, 15:27:38) [GCC 9.3.0] (/usr/bin/python3.9) jinja version = 3.1.2 libyaml = True Running ansible-test sanity on cisco-ios-5.0.0 ... Running sanity test "action-plugin-docs" Running sanity test "changelog" Running sanity test "compile" on Python 2.7 Running sanity test "compile" on Python 3.5 Running sanity test "compile" on Python 3.6 Running sanity test "compile" on Python 3.7 Running sanity test "compile" on Python 3.8 Running sanity test "compile" on Python 3.9 Running sanity test "compile" on Python 3.10 Running sanity test "compile" on Python 3.11 Running sanity test "empty-init" Running sanity test "future-import-boilerplate" Running sanity test "ignores" Running sanity test "line-endings" Running sanity test "metaclass-boilerplate" Running sanity test "no-assert" Running sanity test "no-basestring" Running sanity test "no-dict-iteritems" Running sanity test "no-dict-iterkeys" Running sanity test "no-dict-itervalues" Running sanity test "no-get-exception" Running sanity test "no-illegal-filenames" Running sanity test "no-main-display" Running sanity test "no-smart-quotes" Running sanity test "no-unicode-literals" Running sanity test "pep8" Running sanity test "pylint" Running sanity test "replace-urlopen" Running sanity test "runtime-metadata" Running sanity test "shebang" Running sanity test "shellcheck" Running sanity test "symlinks" Running sanity test "use-argspec-type-path" Running sanity test "use-compat-six" Running sanity test "yamllint" ansible-test sanity complete. : (省略) : Importer processing completed successfully (py39_galaxy-importer) [nakayama@test]$
こんな感じですね。 AnsibleGalaxyサイトにモジュールアップロードした方だと、アップロード時に実行されるテストと同じだなーと思うかと思います。
- podman images(テスト実行後)
[nakayama@test]$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> d5ddca2777a0 53 seconds ago 2.06 GB ★テスト実行してるコンテナ(終わったら消える)
quay.io/ansible/default-test-container 7.14.0 a47d0ef8edf2 7 months ago 1.6 GB
quay.io/ansible/base-test-container 4.1.0 e0fc2e4eb2fd 8 months ago 1.29 GB
quay.io/ansible/ansible-test-utility-container 2.0.0 846c88fea76d 12 months ago 7.7 MB
[nakayama@test]$
- podman ps(ansible-test 実行中)
[nakayama@test]$ podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4b95f37c1fc d5ddca2777a016ed1f5619e348b24f53c3bfbbf9d352908b13dbe6f025d8355d LOCAL_IMAGE_RUNNE... 54 seconds ago Up 54 seconds focused_chebyshev [nakayama@test]$ podman ps
参考:CI環境(GitHubActions)
GitHubにモジュールをアップしている場合、GitHubActionsでこのテストを実行する事も可能です。
教えてもらったリポジトリ
参考に組んでみたGitHubActions例(Ansibleモジュールはなんでもいいです)
まとめ
AnsibleGalaxyやAutomationHubにモジュールをアップする際には是非お試し下さい。 あまり情報がない中試行錯誤した情報が多いので、誤っている部分などあったらご容赦を💦 今年はちゃんと技術ネタ書けて良かった。
ではでは
Happy Automation♪