Ansible の辞書の count.index

Ansible の辞書の count.index

以下のスクリプトで vm10、vm11、vm12 を自動生成することは可能ですか (terraform で使用される count.index として)? 名前「vm」を渡したり定義したりして、vm10、vm11、vm12 という異なる名前の 3 つの VM をデプロイできるようにしたいと思います。方法を提案してください。ありがとうございます。

---
- hosts: Target                         
  vars:
    machines:                  
      v10:
        mem: 1024
        vcpu: 1
      v11:
        mem: 1024
        vcpu: 1
  tasks:
  - name: img cpy
    copy:
      src: /root/pri.qcow2
      dest: /test/{{ item.key }}.qcow2
      remote_src: yes
    with_dict: "{{ machines }}"
  - name: Import/Load VM
    command: >
             virt-install --name {{ item.key }} --memory {{ item.value.mem }} --vcpus {{ item.value.vcpu }} --disk /test/{{ item.key }}.qcow2,bus=sata --import --os-variant generic --network default --noreboot
    with_dict: "{{ machines }}"

答え1

辞書の代わりにインベントリを使用します。100 個の VM が必要ですか?

vms:
  hosts:
    vm[001:100]:
      mem: 1024
      vcpu: 1

vm001これは、、、vm002...、、vm099として解釈されますvm100委任するタスクの実行時には存在しないので、タスクを実行してlocalhostに作成してください。その後、セットアップモジュール新しく作成された VM 上で直接タスクを実行します。

対応するプレイブックは次のようになります。

---
- hosts: vms
  gather_facts: no
  tasks:
  - name: copy qcow image to target path
    copy:
      src: /root/ovms/pri.qcow2
      dest: /root/ovms/test/{{ inventory_hostname }}.qcow2
      remote_src: yes
    delegate_to: target
  - name: Import/Load VM
    command: >
            virt-install --name {{ inventory_hostname }} --memory {{ mem }} --vcpus {{ vcpu }} --disk /root/ovms/test/{{ inventory_hostname }}.qcow2,bus=sata --import --os-variant generic --network default --noreboot
    delegate_to: target

関連情報