https://github.com/gh640/ansible-gce-example-ja
🤖 (Japanese) Ansible で GCP Compute Engine インスタンスを操作するサンプル
https://github.com/gh640/ansible-gce-example-ja
ansible compute-engine gce gcp python3
Last synced: about 1 month ago
JSON representation
🤖 (Japanese) Ansible で GCP Compute Engine インスタンスを操作するサンプル
- Host: GitHub
- URL: https://github.com/gh640/ansible-gce-example-ja
- Owner: gh640
- License: mit
- Created: 2021-09-04T12:51:42.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-06T09:07:26.000Z (almost 5 years ago)
- Last Synced: 2025-03-04T10:25:35.700Z (over 1 year ago)
- Topics: ansible, compute-engine, gce, gcp, python3
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ansible で GCP Compute Engine インスタンスを操作するサンプル
Ansible で Compute Engine インスタンスに SSH 接続するサンプルです。
接続には IAP (Identity-Aware Proxy) TCP Forwarding を使用します。
IAP TCP Forwarding だとパブリックな IP を持たないインスタンスにも接続が可能です。
参考:
- [Identity-Aware Proxy (IAP) | Google Cloud](https://cloud.google.com/iap)
- [Using IAP for TCP forwarding | Identity-Aware Proxy | Google Cloud](https://cloud.google.com/iap/docs/using-tcp-forwarding)
## 必須
- Python 3.9
- Poetry 1.x
- Ansible 4.x
## ディレクトリ構成
```text
collections/
group_vars/
playbooks/
secrets/
ansible.cfg
prod.gcp.yml
pyproject.toml
```
## セットアップ
### Python パッケージをインストール
```bash
poetry install
```
### Ansible コレクションをインストール
```bash
poetry run ansible-galaxy collection install -r collections/requirements.yml
```
このコマンドを実行すると `requirements.yml` で定義されている `google.cloud` プラグイン( `gcp_compute` インベントリプラグインを含む)が `collections/ansible_collections` 以下にインストールされます。
### インベントリファイルを作成
インベントリファイルの中身を実際の情報に書き換えます。
`prod.gcp.yml`:
```bash
plugin: gcp_compute
zones:
- asia-northeast1-a
projects:
- my_project
auth_kind: serviceaccount
service_account_file: ./secrets/gce_key_file.json
hostnames:
- name
groups:
prod: "name == 'my_instance'"
strict: yes
```
最低限 `zones` `projects` `groups` を変更する必要があります。
### サービスアカウントファイルを取得
対象のインスタンスの操作ができるサービスアカウントのキーファイルを生成・ダウンロードして `secrets/gce_key_file.json` に保存します。
`secrets/gce_key_file.json`:
```json
{
"type": "...",
"project_id": "...",
"private_key_id": "...",
"private_key": "...",
"client_email": "...",
"client_id": "...",
"auth_uri": "...",
"token_uri": "...",
"auth_provider_x509_cert_url": "...",
"client_x509_cert_url": "..."
}
```
### Compute Engine インスタンスで IAP TCP Forwarding を有効化
対象の Compute Engine インスタンスで IAP TCP Forwarding を利用できるよう設定します。
具体的には、ファイヤウォールルールの設定やサービスアカウントに対するパーミッションの付与等を行います。
参考:
- [Using IAP for TCP forwarding | Identity-Aware Proxy | Google Cloud](https://cloud.google.com/iap/docs/using-tcp-forwarding)
## 利用
インベントリプラグイン `gcp_compute` でインスタンス情報が正しく取得できる確認します。
```bash
# すべて表示
poetry run ansible-inventory --list
# `prod` グループのみ表示
poetry run ansible-inventory --host prod
```
サンプルプレイブックを実行します。
```bash
poetry run ansible-playbook playbooks/pwd.yml
```