【发布时间】:2023-01-27 22:10:33
【问题描述】:
我需要检查一个名为部署.db存在。如果它不存在,我需要执行一组我正在使用块的任务。
以下是我运行剧本的方式
ansible-playbook test.yml \
-e Layer=APP \
-e BASEPATH="/logs" \
-e Filenames="file1,file2,file3"
这是剧本测试.yml:
---
- 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(',') }}"
运行剧本时出现以下错误:
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经过一番研究,我了解到
block不支持with_items和loop,解决方案是包含一个任务文件。但是,我不确定如何让它发挥作用。你能建议我需要进行哪些调整才能使我的剧本发挥作用吗?
考虑到我使用的是最新版本的 Ansible,还有其他解决方案吗?
【问题讨论】:
-
有很多事情不清楚:您使用了无处定义的变量(图层、文件名)。此外,您的 shell-command 是一种比应有的更多的意图。请编辑您的问题
-
抱歉,我正在使用移动设备发帖,所以格式有问题。现在我已经更新了我原来的帖子。请看一看。谢谢
-
早在 2015 年就要求支持 Ansible 中的此功能,并进行了详细讨论,并最终于 2017 年底关闭。请参阅feature request: looping over blocks #13262。
标签: ansible