- 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 var1 using the loop keyword.
  • The when condition 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_found with a value of success.
  • The until keyword is used to exit the loop when abc_found is success.
  • The second task loops through var2 using the loop keyword.
  • The when condition checks if abc_found is not success, meaning that no item in var1 was found to be "abc".
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

原文地址: http://www.cveoy.top/t/topic/gkt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录