ansible:如何将变量${item}from与notify中的_items一起使用?
我是Ansible的新手,我正在尝试创建几个虚拟环境(每个项目一个,在变量中定义项目列表) 该任务运行良好,我获得了所有文件夹,但是处理程序不工作,它没有使用虚拟环境初始化每个文件夹。处理程序中的${item}变量不起作用。 当我与_项一起使用时,如何使用处理程序ansible:如何将变量${item}from与notify中的_items一起使用?,ansible,Ansible,我是Ansible的新手,我正在尝试创建几个虚拟环境(每个项目一个,在变量中定义项目列表) 该任务运行良好,我获得了所有文件夹,但是处理程序不工作,它没有使用虚拟环境初始化每个文件夹。处理程序中的${item}变量不起作用。 当我与_项一起使用时,如何使用处理程序 tasks: - name: create virtual env for all projects ${projects} file: state=directory path=${virtualen
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}
处理程序只是在任何(逐项列出的子)任务请求它时被“标记”以执行(如果结果发生更改:yes)。 那时,处理程序就像下一个常规任务一样,不知道逐项循环 可能的解决方案不是使用处理程序,而是使用extratask+条件 差不多
- 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
总结前人的论述,并对现代翻译进行调整
- hosts: localhost,
gather_facts: false
tasks:
- action: shell echo {{item}} && exit {{item}}
with_items:
- 1
- 2
- 3
- 4
- 5
register: task
changed_when: task.rc == 3
failed_when: no
notify: update service
handlers:
- name: update service
debug: msg="updated {{item}}"
with_items: >
{{
task.results
| selectattr('changed')
| map(attribute='item')
| list
}}
这很有效!但是迭代dicts列表会产生丑陋的输出。太糟糕了,with_items不支持python表达式您还可以创建一个通知处理程序,并将“with_items:task.results”放在那里。如果您关心丑陋的输出,您可以控制传递给“with_items:”子句的内容,如:“with_items:task.results | selectattr('changed')| map(attribute='item')| list”(不要忘记在“debug:msg=…”中将“item.item”更改为“item”)