write-ansible-code-var1-is-a-list-var2-is-list-loop-var1-when-item-in-var1-is-abc-exit-If-no-item-in-var1-is-found-to-be-abc-continue-to-loop-var2
- name: Loop through var1 and exit when item is "abc"
vars:
var1: [1, 2, 3, "abc", 5]
var2: ["a", "b", "c", "d"]
loop: "{{ var1 }}"
when: item == "abc"
register: abc_found
until: abc_found|success
- name: Loop through var2 when no item in var1 is "abc"
loop: "{{ var2 }}"
when: not abc_found|success
Explanation:
- The first task loops through
var1using theloopkeyword. - The
whencondition checks if the current item in the loop is equal to"abc". - If the condition is true, the task will register a variable called
abc_foundwith a value ofsuccess. - The
untilkeyword is used to exit the loop whenabc_foundissuccess. - The second task loops through
var2using theloopkeyword. - The
whencondition checks ifabc_foundis notsuccess, meaning that no item invar1was found to be"abc".
原文地址: http://www.cveoy.top/t/topic/gkt 著作权归作者所有。请勿转载和采集!