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

时间:2019-10-09 00:33:07

标签: linux ansible

我在下面的剧本中使用块模块。基本上,如果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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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
  - debug:
      msg: "{{ files }}"
    when: 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 != ""