无法在Ansible yml脚本中执行块模块

问题描述 投票:0回答:2

我在下面的剧本中使用块模块。基本上,如果files存在,则只有我要执行Play 2和Play3,但是由于某些原因,在Playbook下面执行时会出现错误。

---
- name: Play 1
  hosts: 127.0.0.1
  tasks:
  - name: find the latest file
    find: paths=/var/lib/jenkins/jobs/process/workspace/files
          file_type=file
          age=-1m
          age_stamp=mtime
    register: files

  - name: Play 2 & 3 if Play 1 has a file
    block:
      - name: Play 2
        hosts: all
        serial: 5
        tasks:
          - name: copy latest file
            copy: src=data_init/goldy.init.qa dest=/data01/admin/files/goldy.init.qa

          - name: copy latest file
            copy: src=data_init/goldy.init.qa dest=/data02/admin/files/goldy.init.qa

      - name: Play 3
        hosts: 127.0.0.1
        tasks:
          - name: execute command
            shell: ./data_init --init_file ./goldy.init.qa
    when: files != ""

下面是错误。知道我在这里做什么错吗?

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in '/var/lib/jenkins/jobs/process/workspace/test.yml': line 14, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    block:
      - name: Play 2
        ^ here
linux ansible
2个回答
0
投票

[我认为这里的混乱源于PlayBlock的不匹配。 Ansible剧本可能包含一个或多个剧本,而Play则是Playbook中的顶级结构(请记住,Playbook只是YAML,因此实际上都是数据结构)。当您想将大量任务高效地组合成一个单元时,就会出现块,您可以对单元执行组操作,例如条件操作,还可以用于错误捕获和恢复。积木是游戏的一部分,几乎可以放置在任何任务可以放置的地方。但是,在语法中,您定义了不允许嵌套在其他游戏中的新游戏。希望这会有所帮助,祝您自动化愉快!


0
投票

在这方面有几处错误,我认为您是ansible的新手。您不能在块上放置名称。您的结构也是错误的。文件未定义。尝试:

---
- name: Play 1
  hosts: 127.0.0.1



  tasks:
  - name: find the latest file
    find: paths=/var/lib/jenkins/jobs/process/workspace/files
          file_type=file
          age=-1m
          age_stamp=mtime
    register: files

  - block:
    - name: copy latest file
      copy: src=data_init/goldy.init.qa dest=/data01/admin/files/goldy.init.qa

    - name: copy latest file
      copy: src=data_init/goldy.init.qa dest=/data02/admin/files/goldy.init.qa

    - name: execute command
      shell: ./data_init --init_file ./goldy.init.qa
      when: files != ""
© www.soinside.com 2019 - 2024. All rights reserved.