Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Ansible中一组任务的块模块上的问题循环_Loops_File_Ansible_Runtime Error - Fatal编程技术网

Loops Ansible中一组任务的块模块上的问题循环

Loops Ansible中一组任务的块模块上的问题循环,loops,file,ansible,runtime-error,Loops,File,Ansible,Runtime Error,我需要检查deploy.db文件是否存在。如果它不存在,那么我需要执行一组使用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: Const

我需要检查deploy.db文件是否存在。如果它不存在,那么我需要执行一组使用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(',') }}" 
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
运行playbook时出现以下错误:

---
- 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
经过研究,我了解到块模块不支持with_items和loop,解决方案是包含任务文件

然而,我不知道如何让它发挥作用。你能建议我需要做些什么调整才能让我的剧本发挥作用吗

考虑到我使用的是Ansible的最新版本,还有其他解决方案吗

“with_items”不是块的有效属性

错误消息说明了一切:不能在块上循环


如果需要循环执行一组任务,请将它们放在一个单独的文件中并使用

多个不清楚的东西:您使用的变量在任何地方都没有定义(图层、文件名)。此外,您的shell命令比它应该做的还要多。请编辑您的问题抱歉,我正在使用移动设备发布内容,因此存在格式问题。现在我更新了我原来的帖子。请看一看。感谢Ansible对该功能的支持,该功能于2015年提出请求,经过详细讨论,最终于2017年底关闭。请参阅。此示例代码可在此处找到: