ansible - Ansible 2.7 如何遍历用户列表并运行一组任务
问题描述
我有一组任务,我想通过循环重复使用 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
我在这里做错了什么?
解决方案
为了执行一组子任务,尤其是在循环结构下,你会想要include_tasks:
,像这样:
- hosts: all
tasks:
- include_tasks: the-other-tasks.yml
with_items:
- alpha
- beta
(我使用它是with_items:
因为它在这个例子中是最简洁的,但是 AFAIK 这只是loop:
它及其loop_var:
朋友的合成糖,所以它应该按预期工作)
推荐阅读
- python - 为什么`DepthwiseConv2D`会比`Conv2D`慢
- java - 如何在android studio中缓慢更改视图的x,y位置
- selenium-webdriver - 测试自动化 Selenium - Chrome 驱动程序 84.xx 以“数据”打开但未启动应用程序 URL
- docker - 在同一网络上的我的 VM 之外使 docker 容器可用?
- javascript - 调整屏幕大小时将一行更改为两个不同的行
- python - 基于嵌套列表的 Hackkerank 问题以打印成绩第二低的任何学生的姓名
- amazon-web-services - 修改 S3 存储桶分区并合并文件,同时将数据从源复制/复制到目标 S3 存储桶
- raytracing - 有没有办法遍历 OptiX 7 加速结构来找到最接近给定点的几何图形?
- typescript - 从打字稿中的可区分联合的一部分中检测类型
- java - 使用 Java、Spring 和 H2 数据库将 Map 属性转换为列