gpt4 book ai didi

ansible : how to use the variable ${item} from with_items in notify?

转载 作者:行者123 更新时间:2023-12-03 20:18:21 25 4
gpt4 key购买 nike

我是 Ansible 的新手,我正在尝试创建多个虚拟环境(每个项目一个,项目列表在变量中定义)。

任务运行良好,我获得了所有文件夹,但是处理程序不起作用,它没有使用虚拟环境初始化每个文件夹。处理程序中的 ${item} 变量不起作用。
使用 with_items 时如何使用处理程序?

  tasks:    
- name: create virtual env for all projects ${projects}
file: state=directory path=${virtualenvs_dir}/${item}
with_items: ${projects}
notify: deploy virtual env

handlers:
- name: deploy virtual env
command: virtualenv ${virtualenvs_dir}/${item}

最佳答案

一旦任何(逐项子)任务请求它(已更改:结果中为是),处理程序就会被“标记”以供执行。
那时处理程序就像下一个常规任务,不知道逐项循环。

可能的解决方案不是使用处理程序,而是使用额外任务 + 条件

就像是

- hosts: all 
gather_facts: false
tasks:
- action: shell echo {{item}}
with_items:
- 1
- 2
- 3
- 4
- 5
register: task
- debug: msg="{{item.item}}"
with_items: task.results
when: item.changed == True

关于ansible : how to use the variable ${item} from with_items in notify?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18368427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com