traversal melalui loop dalam buku pedoman yang memungkinkan

Saya pemula dalam hal mungkin dan mencoba menulis buku pedoman pertama saya.

- name: create volume
  volume:
    state: present
    username: "{{ username }}"
    password: "{{ password }}"
    hostname: "{{ inventory_hostname }}"
    vserver: "{{item[0]}}"
    name:  "{{item[1]}}"
    aggregate_name: "{{output}}"
  with_nested:
    - [ 'vs10' , 'vs11' ]
    - [ 'vol1' , 'vol2', 'vol3' , 'vol4' ,'vol5', ''vol6']
  connection: local

Keluaran sebenarnya:

vs10-vol1 vol2 vol3 vol4
vs11- vol1 vol2 vol3 vol4

Hasil yang diharapkan:

vs10-vol1, vol3 vol5
vs11-vol2, vol4 vol6

person mahesh g o    schedule 17.03.2020    source sumber


Jawaban (1)


Ini mungkin akan berhasil. Saya pada dasarnya mengulang tugas terhadap volume dan menghitung vserver dalam tugas tersebut.

- name: create volume
  volume:
    state: present
    username: "{{ username }}"
    password: "{{ password }}"
    hostname: "{{ inventory_hostname }}"
    # Calculate which vserver to use based on 'current volume index in the volumes list' and 'length of vservers list'. 
    # The logic uses modulus to try and distribute volumes across given vcenters
    vserver: "{{vservers[(current_index % (vservers|length))]}}"
    # Name is item itself because you are looping volumes
    name:  "{{item}}"
    aggregate_name: "{{output}}"
  # Loop the volumes
  loop: [ 'vol1' , 'vol2', 'vol3' , 'vol4' ,'vol5', 'vol6']
  # This is make a loop_control variable available. This will give us 'current_index'
  loop_control:
    index_var: current_index
  # Vservers are defined here
  vars:
    vservers: [ 'vs10' , 'vs11' ]
person Uttam    schedule 19.03.2020