本文介绍了ansible:如何在通知中使用来自 with_items 的变量 ${item}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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} 变量不起作用.使用 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}

推荐答案

一旦(逐项子)任务请求它(已更改:在其结果中是),处理程序只是被标记"以执行.那个时候handler就像是下一个常规任务,不知道逐项循环.

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:如何在通知中使用来自 with_items 的变量 ${item}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 19:46