collectionのモジュールを使ってみた
はじめに
Ansibleにはcollectionという概念があります。正直今までなおざりにしてきましたが、Ansible 2.10でモジュールの扱いがcollectionありきになるため、一度整理しておこうと思います。
せっかくなので2.10で検証しました。
$ ansible --version ansible 2.10.0b1 config file = None configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /opt/ansible2.10.0b1/lib64/python3.6/site-packages/ansible executable location = /opt/ansible2.10.0b1/bin/ansible python version = 3.6.8 (default, Apr 2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
collectionとは
playbook、role、module、plugin等、Ansibleのコンポーネントを任意にまとめた配布形式です。Ansibleをインストールした際、組み込みのmoduleやplugin等も同梱されますが、これらに含まれないmodule、plugin等を使用したいとき、この形式でインストールします。
Ansible 2.10で、今までAnsibleをインストールするだけで使えていた組み込みモジュールの大半がcollection形式に移行するため、Ansibleの利用にあたり、collectionはより重要な要素になります。
collectionのインストール方法
collectionをインストールする方法は3つあります。
- Galaxy Serverからインストール
- tarballからインストール
- gitリポジトリからインストール
gitリポジトリからインストールはリポジトリ側で条件があったり、クライアント側でgitが必要だったりと、現状あまり取り回しが良くなさそうなので、今回は割愛します。
Galaxy Serverからインストール
Ansible GalaxyというAnsibleのコンテンツ共有サービスから取得する方法です。今回は、ios_*
モジュールを含むcollectionをインストールします。
$ ansible-galaxy collection install cisco.ios Starting galaxy collection install process Process install dependency map Starting collection install process Installing 'cisco.ios:1.0.0' to '/home/vagrant/.ansible/collections/ansible_collections/cisco/ios' Installing 'ansible.netcommon:1.0.0' to '/home/vagrant/.ansible/collections/ansible_collections/ansible/netcommon'
上記コマンドは、https://galaxy.ansible.com/cisco/ios
からcollectionを取得しています。これは暗黙的にhttps://galaxy.ansible.com
が補完されており、保管するURLはGALAXY_SERVER
で定義します。
$ ansible-config dump | grep GALAXY_SERVER GALAXY_SERVER(default) = https://galaxy.ansible.com GALAXY_SERVER_LIST(default) = None
tarballからインストールする
ローカルでtarballを指定してインストールすることも可能です。
$ ansible-galaxy collection install cisco-ios-1.0.1-dev3.tar.gz Starting galaxy collection install process Process install dependency map Starting collection install process Installing 'cisco.ios:1.0.1-dev3' to '/home/vagrant/.ansible/collections/ansible_collections/cisco/ios' Skipping 'ansible.netcommon' as it is already installed
インストールパス
インストールパスを指定しない場合、COLLECTIONS_PATHS
に指定されている最初のパスにインストールされます。Playbookからcollectionを呼び出す際も、Ansibleは、COLLECTIONS_PATHS
で定義されたパスにcollectionを探しに行きます。
$ ansible-config dump | grep COLLECTIONS_PATHS COLLECTIONS_PATHS(default) = ['/home/vagrant/.ansible/collections', '/usr/share/ansible/collections']
※COLLECTIONS_PATHS
の指定について、Ansible2.10から、従来のキー名が非推奨となるため、注意が必要です。具体的には、~paths
となっていたのが軒並み~path
になります。下記はansible-config list
の抜粋です。
COLLECTIONS_PATHS: default: ~/.ansible/collections:/usr/share/ansible/collections description: Colon separated paths in which Ansible will search for collections content. env: - deprecated: alternatives: the "ANSIBLE_COLLECTIONS_PATH" environment variable version: '2.14' why: all PATH-type options are singular PATH description: Colon separated paths in which Ansible will search for collections content. env: - deprecated: alternatives: the "ANSIBLE_COLLECTIONS_PATH" environment variable version: '2.14' why: all PATH-type options are singular PATH name: ANSIBLE_COLLECTIONS_PATHS - name: ANSIBLE_COLLECTIONS_PATH version_added: '2.10' ini: - deprecated: alternatives: the "collections_path" ini setting version: '2.14' why: all path-type options are singular path key: collections_paths section: defaults - key: collections_path section: defaults version_added: '2.10' name: ordered list of root paths for loading installed Ansible collections content type: pathspec
モジュールの使い方
Playbookでcollectionを使用するには、名前空間を意識する必要があります。下記は Fully Qualified Collection Name (FQCN)でios_facts
を呼び出す例です。
--- - hosts: ios gather_facts: false tasks: - name: Gather only the config and default facts cisco.ios.ios_facts: gather_subset: - config
Playbook Keywordのcollection
にコレクションの名前空間を指定することで、モジュール呼び出しの際に省略することができます。下記はその例です。
--- - hosts: ios gather_facts: false collections: cisco.ios tasks: - name: Gather only the config and default facts ios_facts: gather_subset: - config
おわりに
取り合えず、ansible2.10でcollectionのモジュールを使ってみる、というところまでできました。2.9以前からの移行が大変そうです…。