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

관련 정보