サマリ
- Lighthouse を使えば、Shared Image Gallary を簡単に異なるテナントに共有できる
- 2019年10月現在、Ansible で Shared Image Gallary を利用して VM を作る場合は devel ブランチが必要
Shared Image Gallary を異なるテナントに共有する
公式ドキュメントでは、Service Principle を利用して Shared Image Gallary を異なるテナントに共有する方法が公開されています。
Azure テナント間でギャラリー VM イメージを共有する
ですが、イメージを共有するためだけに共有する側と共有される側の両方に Azure Active Direcroty の Service Principle を作るのは手間がかかりすぎです。実際に運用で利用する場合、AWS の AMI を異なるアカウントに共有するときのように、共有するテナント側の作業だけで共有が完了する手法が望ましいです。
そこで、Lighthouse を利用します。Shared Image Gallary だけを含むリソースグループを用意したうえで、そのリソースグループに対する参照権限を異なるテナントのユーザに委任します。イメージを共有したいユーザからユーザID と Azure AD のテナント ID をヒアリングしたうえで、Lighthouse を設定します。
参考:Azure の委任されたリソース管理に顧客をオンボードする
設定が完了すると、イメージを共有する側のテナントの Service Provider に、イメージを共有される側のテナントが表示されます。

共有する側の Lighthouse の画面
イメージを共有される側のアカウントで Azure ポータルにログインすると、イメージを共有される側のテナントの Customer にイメージを共有する側のテナントが表示されます。準備万端です。

共有される側の Lighthouse の画面
Shared Image Gallary を利用して Ansible で Virtual Machine を作成する
サブスクリプションフィルターで Shared Image Gallary を共有したテナントとサブスクリプションを選択すれば、共有された側の Azure ポータルに Shared Image Gallary が表示されます。表示されてしまえば、あとは自分のテナントの Shared Image Gallary と同じように Virtual Machine を作れます。

共有された Shared Image Gallary の画面
ポータルから Virtual Machine を作っても面白くないので、今回は Anbile を使って Virtual Machine を作ります。
CloudShell にインストールされている Ansible 2.8 の azure_rm_virtualmachine は Shared Image Gallay に対応していません。azure_rm_virtualmachine で Shared Image Gallary を利用するためには、devel ブランチの Ansible を利用する必要があります。
次のコマンドで CloudShell に devel な Ansible をインストールします。
~$ pip install git+https://github.com/ansible/ansible.git@devel --user
~$ pip install ansible[azure] --user
~$ ./.local/bin/ansible --version
ansible 2.10.0.dev0
config file = None
configured module search path = ['/home/vmoperator/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/vmoperator/.local/lib/python3.5/site-packages/ansible
executable location = ./.local/bin/ansible
python version = 3.5.2 (default, Jul 10 2019, 11:58:48) [GCC 5.4.0 20160609]
devel な Ansible は image の部分に id が指定できるようになっています。その id に Shared Image Gallary 上の イメージの ID を指定すれば OK です。

イメージの ID
- name: provision new Azure virtual host
azure_rm_virtualmachine:
admin_username: '{{ vm_user }}'
admin_password: "{{ vm_password }}"
os_type: Windows
image:
id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-c5bd3103e127/resourceGroups/sharedimage/providers/Microsoft.Compute/galleries/customWindows/images/Windows/versions/1.0.0
name: "{{ vm_name }}"
resource_group: "{{ resource_group }}"
vm_size: Standard_D1
storage_account_name: "{{ vm_name }}"
virtual_network_name: "{{ vm_name }}"
subnet_name: "{{ vm_name }}"
managed_disk_type: "Standard_LRS"