簡體   English   中英

無法在 ansible yml 腳本中執行塊模塊

[英]not able to execute block module in ansible yml script

我在 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

我認為這里的混亂源於PlayBlock的不匹配。 Ansible 劇本可能包含一個或多個劇本,劇本是劇本中的頂級結構(請記住劇本只是 YAML 所以它實際上都是一個數據結構)。 當您希望將一系列任務有效地組合成一個單元時,塊就會出現,您可以對這些任務采取組操作,例如條件,但也可以用於錯誤捕獲和恢復。 積木是游戲的一部分,它們幾乎可以放在任務可以放置的任何地方。 但是,在語法中,您定義了嵌套在其他不允許的新 Plays 中。 希望這會有所幫助,祝自動化愉快!

這有幾處錯誤,我假設您是 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 != ""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM