如何将 Ansible with_items 循环应用于包含的任务? [英] How do I apply an Ansible with_items loop to the included tasks?

查看:28
本文介绍了如何将 Ansible with_items 循环应用于包含的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import_tasks 的文档提到><块引用>

任何循环、条件和大多数其他关键字都将应用于包含的任务,而不是应用于此语句本身.

这正是我想要的.不幸的是,当我尝试使 import_tasks 与循环一起工作时

- import_tasks: msg.ymlwith_items:- 1- 2- 3

我收到消息

<块引用>

错误!您不能在import_tasks"语句上使用循环.您应该改用include_tasks".

我不想要 include_tasks 行为,因为这会将循环应用于包含的文件,并复制任务.我特别想为每个循环变量运行第一个任务(作为一个带有标准 with_items 输出的任务),然后是第二个,依此类推.我怎样才能得到这种行为?

<小时>

具体来说,请考虑以下事项:

假设我有以下文件:

playbook.yml

---- 主机:192.168.33.100收集事实:没有任务:- include_tasks: msg.ymlwith_items:- 1- 2

msg.yml

---- 名称:消息 1调试:msg: "消息 1: {{ item }}"- 名称:消息 2调试:msg: "消息 2: {{ item }}"

我想要打印的消息

<块引用>

消息 1:1
消息 1:2
消息 2:1
消息 2:2

但是,使用 import_tasks 时出现错误,使用 include_tasks 时出现错误

<块引用>

消息 1:1
消息 2:1
消息 1:2
消息 2:2

解决方案

您可以添加一个 with_items 循环,为导入文件中的每个任务添加一个列表,并调用 import_tasks 带有您传递给内部 with_items 循环的变量.这会将循环的处理移至导入的文件,并且需要在所有任务上重复循环.

<小时>

以您的示例为例,这会将文件更改为:

playbook.yml

---- 主机:192.168.33.100收集事实:没有任务:- 导入任务:msg.yml变量:消息:- 1- 2

msg.yml

---- 名称:消息 1调试:msg: "消息 1: {{ item }}"with_items:- {{消息}}"- 名称:消息 2调试:msg: "消息 2: {{ item }}"with_items:- {{消息}}"

The documentation for import_tasks mentions

Any loops, conditionals and most other keywords will be applied to the included tasks, not to this statement itself.

This is exactly what I want. Unfortunately, when I attempt to make import_tasks work with a loop

- import_tasks: msg.yml
  with_items:
    - 1
    - 2
    - 3

I get the message

ERROR! You cannot use loops on 'import_tasks' statements. You should use 'include_tasks' instead.

I don't want the include_tasks behaviour, as this applies the loop to the included file, and duplicates the tasks. I specifically want to run the first task for each loop variable (as one task with the standard with_items output), then the second, and so on. How can I get this behaviour?


Specifically, consider the following:

Suppose I have the following files:

playbook.yml

---                       

- hosts: 192.168.33.100
  gather_facts: no
  tasks:
  - include_tasks: msg.yml
    with_items:
      - 1
      - 2

msg.yml

---

- name: Message 1
  debug:
    msg: "Message 1: {{ item }}"

- name: Message 2
  debug:
    msg: "Message 2: {{ item }}"

I would like the printed messages to be

Message 1: 1
Message 1: 2
Message 2: 1
Message 2: 2

However, with import_tasks I get an error, and with include_tasks I get

Message 1: 1
Message 2: 1
Message 1: 2
Message 2: 2

解决方案

You can add a with_items loop taking a list to every task in the imported file, and call import_tasks with a variable which you pass to the inner with_items loop. This moves the handling of the loops to the imported file, and requires duplication of the loop on all tasks.


Given your example, this would change the files to:

playbook.yml

---

- hosts: 192.168.33.100
  gather_facts: no
  tasks:
  - import_tasks: msg.yml
    vars:
      messages:
        - 1
        - 2

msg.yml

---

- name: Message 1
  debug:
    msg: "Message 1: {{ item }}"
  with_items:
    - "{{ messages }}"

- name: Message 2
  debug:
    msg: "Message 2: {{ item }}"
  with_items:
    - "{{ messages }}"

这篇关于如何将 Ansible with_items 循环应用于包含的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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