прохождение по пьесе l oop in ansible - PullRequest
0 голосов
/ 17 марта 2020

Я новичок ie до ansible и пытаюсь написать свою первую пьесу.

- 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

Фактический результат:

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

Ожидаемый результат:

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

1 Ответ

0 голосов
/ 19 марта 2020

Это, вероятно, будет работать. Я в основном зацикливаю задачу на объемы и вычисляю vserver в задаче.

- 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' ]
...