问题描述
我需要检查 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.
下面是我如何运行剧本
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.
推荐答案
错误消息说明了一切:您不能循环遍历块.
The error message says it all: you cannot loop over a block.
如果您需要遍历一组任务,请将它们放在一个单独的文件中并使用 include_tasks
If you need to loop over a set of tasks, put them in a separate file and use include_tasks
这篇关于针对 Ansible 中的一组任务在块模块上发出循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!