执行ansible include_tasks直到满足特定条件(直到循环为止) [英] Execute ansible include_tasks until a certain condition is met (kind of while until loop)
问题描述
我想执行一个包含任务列表,直到满足特定条件为止,我没有固定的循环,但是执行取决于条件.
I want to execute an include tasks list until a certain condition is met, I do not have a fixed loop but execution depends upon a condition.
下面的示例游戏
任务列表剧本 tasks.yml
---
- name: "inc test-var {{ test_var }}"
set_fact:
test_var: "{{ test_var | int + 1 }} "
父级剧本 parent.yml
---
- hosts: all
gather_facts: no
tasks:
- set_fact:
test_var: '1'
req_var: '4'
- name: "Test multi run of task"
include_tasks: ./includes/tasks.yml
register: versions_result
until: test_var is version(req_var, '<')
retries: 5
在这里,我希望 parent.yml
任务可以运行多次,但只能运行一次.有人可以指出我做错了什么,以及如何多次运行任务直到满足条件.
here I am expecting parent.yml
tasks to run multiple times but it only run once.
Could some one point out what I am doing wrong and how to run a task multiple times until a condition is met.
干杯
推荐答案
多次遍历 include_tasks
的一种方法是遍历数字范围,直到达到所需的数字.但是,正如您期望的那样,父母"身份该剧本不会多次运行,而任务文件将会运行.
One way to include_tasks
multiple times is to loop over the range of numbers till it reaches the required number. However as you expect the "parent" playbook will not be run multiple times, the tasks file will be.
考虑以下示例:
通过我的主要剧本 parent.yml
,我想多次运行 tasks1.yml
(在 set_fact
中定义)./p>
Through my main playbook parent.yml
, I would like to run tasks1.yml
multiple times (as defined in set_fact
).
tasks:
- set_fact:
num: 1
req_num: 4
- include_tasks: tasks1.yml
loop: "{{ range(num, req_num + 1)|list }}"
在我的 tasks1.yml
中,我有一条简单的 debug
消息:
And in my tasks1.yml
, I have a simple debug
message:
- debug:
msg: "Run {{ item }}"
包含4次 tasks1.yml
,并在运行 ansible-playbook parent.yml
时给出以下输出:
Includes tasks1.yml
4 times and gives below output when I run ansible-playbook parent.yml
:
TASK [include_tasks] ******************************************************************************************************************************************************************
included: /home/user/tasks1.yml for localhost
included: /home/user/tasks1.yml for localhost
included: /home/user/tasks1.yml for localhost
included: /home/user/tasks1.yml for localhost
TASK [debug] **************************************************************************************************************************************************************************
ok: [localhost] =>
msg: Run 1
TASK [debug] **************************************************************************************************************************************************************************
ok: [localhost] =>
msg: Run 2
# ...goes till "Run 4"
这篇关于执行ansible include_tasks直到满足特定条件(直到循环为止)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!