Ansible 2.7如何遍历用户列表并运行一组任务

时间:2018-12-01 07:47:58

标签: ansible

我想用一个循环重复执行一组任务,并使用Ansible 2.7循环列出用户列表。我的简单剧本看起来像这样:

---
- hosts: localhost
  gather_facts: no
  vars:
    username: "{{ item }}"

  tasks:
    - debug:
        msg: "running as {{ username }}"

    - name: echo the username
      command: "/bin/echo echoing {{ username }}"

      loop:
        - joe
        - fred

我在这里做什么错了?

1 个答案:

答案 0 :(得分:2)

要执行一组子任务,尤其是在循环结构下,您需要include_tasks:,如下所示:

- hosts: all
  tasks:
  - include_tasks: the-other-tasks.yml
    with_items:
    - alpha
    - beta

(我使用with_items:是因为它在此示例中最简洁,但是AFAIK只是loop:及其loop_var:朋友的合成糖,因此应该可以正常工作)