执行 ansible include_tasks 直到满足某个条件(类似于 while 循环) [英] Execute ansible include_tasks until a certain condition is met (kind of while until loop)

查看:20
本文介绍了执行 ansible include_tasks 直到满足某个条件(类似于 while 循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行包含任务列表直到满足某个条件,我没有固定循环但执行取决于条件.

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 中所定义).

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 }}"

包括 tasks1.yml 4 次并在我运行 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 直到满足某个条件(类似于 while 循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆