ansible-proxmox について
ansible-proxmox は、Ansible で Proxmox を自動化するスキルで、仮想化環境のデプロイや運用用 Playbook を作成する人を対象とします。`community.proxmox` のネイティブモジュールを優先し、コマンドラインが必要な操作では冪等性を重視します。
主な機能
- 01community.proxmox のネイティブモジュールを優先
- 02VM の作成とクローンを自動化
- 03ユーザー、グループ、プール、ACL の管理
- 04クラスタと CEPH の CLI 操作に冪等性を追加
- 05VLAN 対応ネットワークブリッジの設定
活用シーン
- 01テンプレートから Proxmox VM を作成または複製
- 02API 経由でユーザー、ストレージ、権限を管理
- 03クラスタを作成しノードとクォーラムを確認
- 04CEPH を初期化して OSD を作成
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ basher83/ lunar-claude --skill ansible-proxmoxインストール後、Claude Code で/ansible-proxmoxを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
references/ceph-automation.mdcluster-automation.mdcommunity-proxmox-plugin-index.mdnetwork-automation.md詳細
Ansible Proxmox 統合
community.proxmox コレクションを使用した Proxmox の高度な自動化。CLI の使用は最小限に抑えます。
モジュールの選択
ネイティブモジュールを優先する
利用可能な場合は、community.proxmox モジュールを使用します。
| 操作 | 使用するモジュール | CLI は使用しない |
|---|---|---|
| VM の作成 | community.proxmox.proxmox_kvm | qm create |
| VM のクローン | community.proxmox.proxmox_kvm | qm clone |
| ユーザーの管理 | community.proxmox.proxmox_user | pveum user |
| グループの管理 | community.proxmox.proxmox_group | pveum group |
| プールの管理 | community.proxmox.proxmox_pool | pveum pool |
| ACL の管理 | community.proxmox.proxmox_acl | pveum acl |
| ストレージ | community.proxmox.proxmox_storage | pvesm |
CLI が必要な場合
一部の操作には、ネイティブモジュールがありません。
| 操作 | CLI が必要 | 理由 |
|---|---|---|
| クラスターの作成 | pvecm create | モジュールが存在しません |
| クラスターへの参加 | pvecm add | モジュールが存在しません |
| CEPH の初期化 | pveceph init | 複雑なワークフローです |
| CEPH OSD | pveceph osd create | 複雑なワークフローです |
ネイティブモジュールの例
テンプレートから VM を作成する
- name: Create VM from template
community.proxmox.proxmox_kvm:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_token_id }}"
api_token_secret: "{{ proxmox_token_secret }}"
node: "{{ proxmox_node }}"
vmid: "{{ vm_id }}"
name: "{{ vm_name }}"
clone: "{{ template_name }}"
full: true
storage: local-lvm
memory: "{{ vm_memory | default(4096) }}"
cores: "{{ vm_cores | default(2) }}"
state: present
delegate_to: localhost
Proxmox ユーザーを管理する
- name: Create Terraform user
community.proxmox.proxmox_user:
api_host: "{{ proxmox_api_host }}"
api_user: "root@pam"
api_password: "{{ proxmox_root_password }}"
userid: "terraform@pve"
comment: "Terraform automation user"
groups:
- automation
state: present
no_log: true
delegate_to: localhost
ACL を設定する
- name: Grant terraform permissions
community.proxmox.proxmox_acl:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_token_id }}"
api_token_secret: "{{ proxmox_token_secret }}"
path: "/"
users:
- terraform@pve
roles:
- Administrator
state: present
delegate_to: localhost
冪等性を確保した CLI の使用
CLI が必要な場合は、適切な冪等性制御を追加します。
クラスターの構築
- name: Check existing cluster status
ansible.builtin.command: pvecm status
register: cluster_status
failed_when: false
changed_when: false
- name: Set cluster facts
ansible.builtin.set_fact:
is_cluster_member: "{{ cluster_status.rc == 0 }}"
in_target_cluster: "{{ cluster_name in cluster_status.stdout }}"
- name: Create cluster on primary node
ansible.builtin.command: pvecm create {{ cluster_name }}
when:
- inventory_hostname == groups['proxmox'][0]
- not in_target_cluster
register: cluster_create
changed_when: cluster_create.rc == 0
- name: Join cluster on secondary nodes
ansible.builtin.command: pvecm add {{ hostvars[groups['proxmox'][0]].ansible_host }}
when:
- inventory_hostname != groups['proxmox'][0]
- not is_cluster_member
register: cluster_join
changed_when: cluster_join.rc == 0
API トークンの作成
- name: Create API token
ansible.builtin.command: >
pveum user token add {{ username }}@pam {{ token_name }}
--privsep 0
register: token_result
changed_when: "'already exists' not in token_result.stderr"
failed_when:
- token_result.rc != 0
- "'already exists' not in token_result.stderr"
no_log: true
認証パターン
API トークン(推奨)
# Store token in variables or Infisical
proxmox_api_host: "192.168.1.10"
proxmox_api_user: "terraform@pve"
proxmox_token_id: "automation"
proxmox_token_secret: "{{ lookup('infisical', 'PROXMOX_TOKEN_SECRET') }}"
- name: Create VM
community.proxmox.proxmox_kvm:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_token_id }}"
api_token_secret: "{{ proxmox_token_secret }}"
# ... rest of config
root パスワード(ローカル操作)
Proxmox ノード自体に対する操作の場合:
- name: Retrieve root password
ansible.builtin.include_tasks: tasks/infisical-secret-lookup.yml
vars:
secret_name: 'PROXMOX_ROOT_PASSWORD'
secret_var_name: 'proxmox_root_password'
- name: Configure via API
community.proxmox.proxmox_user:
api_host: "{{ inventory_hostname }}"
api_user: "root@pam"
api_password: "{{ proxmox_root_password }}"
# ... config
no_log: true
クラスター操作
冪等性を確保したクラスター状態の確認
- name: Get cluster status
ansible.builtin.command: pvecm status
register: cluster_status
changed_when: false
failed_when: false
- name: Get cluster nodes
ansible.builtin.command: pvecm nodes
register: cluster_nodes
changed_when: false
failed_when: false
when: cluster_status.rc == 0
- name: Set cluster facts
ansible.builtin.set_fact:
cluster_exists: "{{ cluster_status.rc == 0 }}"
cluster_node_count: "{{ cluster_nodes.stdout_lines | length | default(0) }}"
is_quorate: "{{ 'Quorate: Yes' in cluster_status.stdout | default('') }}"
クラスターの健全性を確認する
- name: Verify cluster quorum
ansible.builtin.command: pvecm status
register: cluster_health
changed_when: false
failed_when: "'Quorate: Yes' not in cluster_health.stdout"
- name: Verify expected node count
ansible.builtin.command: pvecm nodes
register: nodes_check
changed_when: false
failed_when: nodes_check.stdout_lines | length != groups['proxmox'] | length
CEPH の統合
CEPH を初期化する
- name: Check if CEPH is initialized
ansible.builtin.command: pveceph status
register: ceph_status
changed_when: false
failed_when: false
- name: Initialize CEPH
ansible.builtin.command: >
pveceph init --network {{ ceph_network }}
when:
- inventory_hostname == groups['proxmox'][0]
- ceph_status.rc != 0
register: ceph_init
changed_when: ceph_init.rc == 0
OSD を作成する
- name: Check if OSD exists on device
ansible.builtin.command: >
pveceph osd list
register: osd_list
changed_when: false
- name: Create OSD
ansible.builtin.command: >
pveceph osd create {{ item }}
loop: "{{ ceph_osd_devices }}"
when: item not in osd_list.stdout
register: osd_create
changed_when: osd_create.rc == 0
ネットワーク設定
ネットワーク設定には community.general.interfaces_file を使用します。
- name: Configure VLAN-aware bridge
community.general.interfaces_file:
iface: vmbr1
option: bridge-vlan-aware
value: "yes"
backup: true
state: present
notify: reload network
- name: Set bridge ports
community.general.interfaces_file:
iface: vmbr1
option: bridge-ports
value: "bond0"
backup: true
state: present
notify: reload network
アンチパターン
モジュールがあるのに CLI を使用する
# BAD - Module exists for this
- name: Create user
ansible.builtin.command: pveum user add terraform@pve
# GOOD
- name: Create user
community.proxmox.proxmox_user:
api_host: "{{ proxmox_api_host }}"
api_user: "root@pam"
api_password: "{{ password }}"
userid: "terraform@pve"
state: present
CLI の冪等性が不足している
# BAD - Will fail on second run
- name: Create cluster
ansible.builtin.command: pvecm create MyCluster
# GOOD
- name: Check cluster status
ansible.builtin.command: pvecm status
register: cluster_check
changed_when: false
failed_when: false
- name: Create cluster
ansible.builtin.command: pvecm create MyCluster
when: cluster_check.rc != 0
間違ったホストで実行する
# BAD - API calls from managed node
- name: Create VM
community.proxmox.proxmox_kvm:
api_host: "{{ inventory_hostname }}" # Calling itself
# ...
# GOOD - Delegate API calls to localhost
- name: Create VM
community.proxmox.proxmox_kvm:
api_host: "{{ proxmox_api_host }}"
# ...
delegate_to: localhost
コレクションのインストール
# Install community.proxmox
uv run ansible-galaxy collection install community.proxmox
# Or via requirements.yml
cd ansible
uv run ansible-galaxy collection install -r requirements.yml
追加リソース
Proxmox の自動化パターンの詳細については、以下を参照してください。
references/ceph-automation.md- CEPH ストレージのデプロイと OSD の管理references/cluster-automation.md- Proxmox クラスターの作成とノードの参加references/network-automation.md- VLAN 対応ブリッジとネットワーク設定references/community-proxmox-plugin-index.md- community.proxmox モジュールの完全なリファレンス
関連スキル
- ansible-fundamentals - モジュール選択の基本パターン
- ansible-idempotency - CLI コマンドの冪等性を確保する方法
- ansible-error-handling - クラスター操作でのエラー復旧
コメント
Markdown 対応