LoginSignup
3
2

More than 1 year has passed since last update.

GitHub Enterpriseなど社内リポジトリにあるansible roleリポジトリをinstallして利用する

Last updated at Posted at 2022-07-17

会社でホストしているGitHub Enterpriseにある独自のRoleを複数のPlaybookのあるリポジトリから参照して利用したい場合のTips。

GitHub Enterprise以外にもGitLabだけでなく、他のgit-baseのSCMでも同じ方法でいけると思います(未確認)。

TL;DR

installしたいRoleのリスト requirements.yml を作成

- src: git@github.example.com:yassan/ansible-role-ping.git
  scm: git
  version: v1.0  # master でもOK
  name: ping

以下を使ってinstall

ansible-galaxy install -v -r requirements.yml

install先を変更したい場合は、ansible.cfg に設定を追加。

[defaults]
roles_path=.ansible/roles

.ansible/roles を作成しておく事。

環境

ansible: 2.9.27
python: 3.8
OS: Ubuntu 20.04 LTS

前提条件

Roleだけのリポジトリを作成しておくこと

こんな感じ。

参考:Roleの作成方法

Roleの作成は以下の通り、ansible-galaxyコマンドを使う方が楽です。

Roleの利用方法

しくみ

ansible-galaxyコマンドを使って、Roleリポジトリ指定してinstallする。

❯ ansible-galaxy role install --help
usage: ansible-galaxy role install [-h] [-s API_SERVER] [--api-key API_KEY] [-c] [-v] [-f] [-p ROLES_PATH] [-i] [-n | --force-with-deps]
                                   [-r ROLE_FILE] [-g]
                                   [role_name [role_name ...]]

positional arguments:
  role_name             Role name, URL or tar file

optional arguments:
  -h, --help            show this help message and exit
  -s API_SERVER, --server API_SERVER
                        The Galaxy API server URL
  --api-key API_KEY     The Ansible Galaxy API key which can be found at https://galaxy.ansible.com/me/preferences. You can also set the token
                        for the GALAXY_SERVER_LIST entry.
  -c, --ignore-certs    Ignore SSL certificate validation errors.
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable connection debugging)
  -f, --force           Force overwriting an existing role or collection
  -p ROLES_PATH, --roles-path ROLES_PATH
                        The path to the directory containing your roles. The default is the first writable one configured via
                        DEFAULT_ROLES_PATH: ~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
  -i, --ignore-errors   Ignore errors and continue with the next specified role.
  -n, --no-deps         Don't download roles listed as dependencies.
  --force-with-deps     Force overwriting an existing role and its dependencies.
  -r ROLE_FILE, --role-file ROLE_FILE
                        A file containing a list of roles to be imported.
  -g, --keep-scm-meta   Use tar instead of the scm archive option when packaging the role.

詳細

install先の指定

ansible-galaxyコマンドでinstallする際、install先を指定しないと ~/.ansible/roles 以下にinstallされます。

JenkinsなどCIを利用する場合、~/.ansible/roles のままだと、CIジョブの環境が一意にならず、都合悪そうなので、Playbookを利用するリポジトリ直下の .ansible/role にインストールするよう ansible.cfg に設定を追加(不要なら指定しなくてもOK)。

[defaults]
roles_path=.ansible/roles

.ansible/roles を作成しておく事。

cf. Ansible Configuration Settings — Ansible Documentation

installしたいRoleのリストを作成

installしたいロールのリストrequirements.yml を以下のフォーマットで作成する。

- src: git@github.example.com:yassan/ansible-role-ping.git
  scm: git
  version: v1.0  # master でもOK
  name: ping

Versionを指定する場合は、事前にリポジトリにReleaseタグを作成しておいてをそれを指定する事。

個人的にはいつの状態のものが使われているかを明確にした方がトラブル時に便利なので指定をオススメ。

リストのフォーマットについては、他にもパターンがあります。
Installing Multiple Roles From a File | Installing content — Ansible Documentation

Roleのinstall

以下のようにしてinstallします。

ansible-galaxy install -v -r requirements.yml

ただ、同じversionをinstallすると、以下のようにskipされます。

❯ ansible-galaxy install -v -r requirements.yml 
Using /home/yassan/repo/example/test-ext-role-play/ansible.cfg as config file
- ping (v1.0) is already installed, skipping.

どうしてもinstallをやり直したい場合は、-f を追加して実行するとOK.

❯ ansible-galaxy install -v -f -r requirements.yml 
Using /home/yassan/repo/example/test-ext-role-play/ansible.cfg as config file
- changing role ping from v1.0 to v1.0
- extracting ping to /home/yassan/repo/example/test-ext-role-play/.ansible/roles/ping
- ping (v1.0) was installed successfully

また、installすると roles_path で指定したパスの配下にRoleが保存されていることが分かります。

❯ tree .ansible -d | head
.ansible
└── roles
    └── ping
        ├── defaults
        ├── handlers
        ├── meta
        ├── molecule
        │   └── default
        ├── tasks
        ├── tests

最後に

plyabookのあるリポジトリにroleを沢山入れてしまうと、testの管理が複雑になってしまうので、ある程度Roleが増えてきたらリポジトリを分離して利用する事をおすすめします。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2