#data-structures #ansible
#структуры данных #ansible
Вопрос:
Как я могу записывать строки из двух списков с помощью Ansible?
Файл Var: bottle.yml
Bottle:
- wine:
- 1951
- 1960
- country_to_export:
- belgium-1
- belgium-2
main.yml файл:
debug:
msg: "I send the bottle {{ item.0 }} to country {{ item.1 }}"
with_items:
- "{{ Bottle.wine }}"
- "{{ Bottle.country_to_export}}"
Результат:
"I send the bottle [ u1951, u1960 ] to country [ ubelgium-1,ubelgium-2 ]"
Желаемый результат:
I send the bottle 1951 to country Belgium-1
I send the bottle 1960 to country Belgium-2
Ответ №1:
Вы просто используете неправильный тип цикла, вы должны использовать цикл with_together
вместо цикла with_items
.
Учитывая сборник пьес:
- hosts: all
gather_facts: yes
tasks:
- debug:
msg: "I send the bottle {{ item.0 }} to country {{ item.1 }}"
with_together:
- "{{ Bottle.wine }}"
- "{{ Bottle.country_to_export}}"
vars:
Bottle:
wine:
- 1951
- 1960
country_to_export:
- belgium-1
- belgium-2
Это приводит к повторению:
PLAY [all] **********************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [localhost]
TASK [debug] ********************************************************************************************************
ok: [localhost] => (item=[1951, 'belgium-1']) => {
"msg": "I send the bottle 1951 to country belgium-1"
}
ok: [localhost] => (item=[1960, 'belgium-2']) => {
"msg": "I send the bottle 1960 to country belgium-2"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Также имейте в виду, что в вашем bottle.yml ваш словарь неверен по сравнению с тем, что, по вашим словам, он вам дает.
Скорее это должно быть:
Bottle:
wine:
- 1951
- 1960
country_to_export:
- belgium-1
- belgium-2
Лучший синтаксис, если вы хотите быть совместимым с новыми версиями Ansible, — это отказаться от with_*
структуры и начать использовать их loop
замену.
Таким образом, вышеприведенный сценарий в конечном итоге будет:
- hosts: all
gather_facts: yes
tasks:
- debug:
msg: "I send the bottle {{ item.0 }} to country {{ item.1 }}"
loop: "{{ Bottle.wine | zip(Bottle.country_to_export) | list }}"
vars:
Bottle:
wine:
- 1951
- 1960
country_to_export:
- belgium-1
- belgium-2
Выдача того же резюме.