在Ansible中针对一组任务的块模块上的问题循环 [英] Issue looping on block module for a set of tasks in Ansible
问题描述
我需要检查deploy.db文件是否存在.如果它不存在,那么我需要执行我正在使用块的一组任务.
I need to check if deploy.db file exists. If it does not exist, then I need to perform a set of tasks for which I'm using block.
下面是我运行剧本的方式
Below is how i run the playbook
ansible-playbook test.yml -e Layer=APP -e BASEPATH="/logs" -e Filenames="file1,file2,file3"
下面是我完整的剧本:
---
- name: "Play 1"
hosts: localhost
gather_facts: false
tasks:
- name: Construct
debug:
msg: "Run"
- block:
- stat: path="{{ BASEPATH }}/deploy.db"
register: currdb
- file: path="{{ BASEPATH }}/deploy.db" state=touch recurse=no
when: currdb.stat.exists == False
- shell: "echo done>>{{ BASEPATH }}/deploy.db"
when: currdb.stat.exists == False
when: Layer == 'APP'
with_items:
- "{{ Filenames.split(',') }}"
我在运行剧本时遇到以下错误:
I'm getting the below error running the playbook:
ERROR! 'with_items' is not a valid attribute for a Block
The error appears to be in '/app/test.yml': line 9, column 6, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- block:
^ here
经过研究后,我了解到块模块既不支持with_items也不支持loop,解决方案是包含任务文件.
After researching a bit, I understand that neither with_items nor loop is supported by block module and the solution is to include task file.
但是,我不确定如何使它正常工作.您能否建议我需要进行哪些调整才能使我的剧本正常工作?
I'm however, not sure how to get that to work. Can you please suggest what tweaks I need to inorder for my playbook to work ?
考虑到最新版本的Ansible,还有其他解决方案.
Considering i m on the latest version of Ansible are there other solutions.
推荐答案
"with_items"不是块的有效属性
'with_items' is not a valid attribute for a Block
错误消息说明了一切:您无法循环访问一个块.
The error message says it all: you cannot loop over a block.
If you need to loop over a set of tasks, put them in a separate file and use include_tasks
这篇关于在Ansible中针对一组任务的块模块上的问题循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!