ansible:如何在notify中使用with_items中的变量$ {item}? [英] ansible : how to use the variable ${item} from with_items in notify?
问题描述
我是Ansible的新手,我正在尝试创建多个虚拟环境(每个项目一个,在变量中定义项目列表).
I am new to Ansible and I am trying to create several virtual environments (one for each project, the list of projects being defined in a variable).
任务运行良好,我得到了所有文件夹,但是处理程序不起作用,它不会使用虚拟环境初始化每个文件夹.处理程序中的$ {item} varialbe不起作用. 使用with_items时如何使用处理程序?
The task works well, I got all the folders, however the handler does not work, it does not init each folder with the virtual environment. The ${item} varialbe in the handler does not work. How can I use an handler when I use 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}
推荐答案
处理程序只是被标记"为一旦执行(无论是分项的子任务)是否要求执行(已更改:结果为是)即可执行. 那时,处理程序就像接下来的常规任务一样,并且不了解逐项循环.
Handlers are just 'flagged' for execution once whatever (itemized sub-)task requests it (had the changed: yes in its result). At that time handlers are just like a next regular tasks, and don't know about the itemized loop.
可能的解决方案不是使用处理程序,而是使用额外任务+有条件
A possible solution is not with a handler but with an extratask + conditional
类似
- 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:如何在notify中使用with_items中的变量$ {item}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!