zaki work log

作業ログやら生活ログやらなんやら

[Ansible] Gitリポジトリにあるコレクションのインストール

Ansible Galaxyと疎通がないとか、ローカル内でGit管理されているプライベート開発なコレクションのように、任意のGitリポジトリにあるコレクションをインストールする際のrequirements.ymlの記述例は以下の通り。

---
collections:
  - name: https://github.com/zaki-lknr/esxissh-ansible.git
    version: main
    type: git

指定するキーと値は以下の通り。

キー
name リポジトリURL
version ブランチ名
type gitを指定

この内容のファイルを指定することで、各種環境でコレクションをインストールできる。

(配布側) リポジトリ構成

リポジトリのルートディレクトリにgalaxy.ymlファイルやpluginsディレクトリなどのコレクションを構成するディレクトリとその中身を配置する。
ない場合(サブディレクトリ以下にある等)だとエラーになるので注意。

今回のサンプルコレクションはこちら。

github.com

コレクション作成の参考

qiita.com

(利用側) インストール

ansible-galaxy

任意のファイル名で前述の書式のファイルを作成し、以下コマンドを実行。
お作法的には collections/requirements.yml が好ましい。多分。

$ ansible-galaxy install -r collections/requirements.yml

インストール先ディレクトリはデフォルトでは~/.ansible以下。
変更したい場合はansible.cfgなどでインストール先を指定する。

[defaults]
collections_path = /path/to/ansible/collections

ansible-builder

execution-environment.ymldependenciesに指定するrequirements.ymlでインストール内容を記述して、あとは普通にビルドする。

$ ansible-builder build -t esxi-ssh:latest -v 3
Ansible Builder is building your execution environment image, "esxi-ssh:latest".
File context/_build/requirements.yml is already up-to-date.
File context/_build/requirements.txt is already up-to-date.
File context/_build/bindep.txt is already up-to-date.
File context/_build/ansible.cfg is already up-to-date.
:
:
[1/3] STEP 8/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
Cloning into '/home/runner/.ansible/tmp/ansible-local-116wclers/tmptw5tpno2/esxissh-ansible347k3r33'...
Already on 'main'
Your branch is up to date with 'origin/main'.
Starting collection install process
Installing 'zaki_lknr.esxissh:1.1.0' to '/usr/share/ansible/collections/ansible_collections/zaki_lknr/esxissh'
Created collection for zaki_lknr.esxissh:1.1.0 at /usr/share/ansible/collections/ansible_collections/zaki_lknr/esxissh
zaki_lknr.esxissh:1.1.0 was installed successfully
--> 304eaaf07f6
:
:

AAP / AWX / Tower

(確認はAAP 2.1のみ実施)
リポジトリcollections/requirements.yml にインストールしたいコレクションを記述する。

サンプルとしては以下。

github.com

これでソースコントロールの更新時にコレクションがインストールされる。

参考

docs.ansible.com

ドキュメントは以下、Install multiple collections with a requirements fileより。

また、Git上にあるRoleのインストール関連は以下。

zaki-hmkc.hatenablog.com