Ansible 2 \ 包含其他 YML 不起作用,尽管直接调用有效



我在我的all.ym中有这个无法运行tagger.yml(尽管直接运行tagger.yml时,它正在工作)

---
  - name: run couple of ymls
    hosts: all
    tasks:
      - include: "./tagger.yml"
        #- include: ./fluentd.yml

tagger.yml

---
  - name: tagger - build docker
    hosts: all  
    tags:
      - all
      - tagger
....

错误是

fatal: [localhost]: FAILED! => {"failed": true, "reason": "no action detected in task. This often indicates a misspelled module name, or incorrect module path.nnThe error appears to have been in '.Build/tagger.yml': line 2, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nn---n  - name: tagger - build dockern    ^ herennnThe error appears to have been in '.Build/tagger.yml': line 2, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nn---n  - name: tagger - build dockern    ^ heren"}

Ansible具有两个"级别",一个是剧本级别,您可以在其中提供播放,另一个是任务级别,您可以在其中提供任务。包括这两个级别上的包括的作品,但是如果您已经处于任务级别,则无法包括新戏。

例如,这还可以:

main.yml
---
- include: play1.yml
- include: play2.yml
play1.yml
----
- name: run couple of tasks on all hosts
  hosts: all
  tasks: [{debug: {msg: "Task1"}}]
play2.yml
----
- name: run some more tasks on some hosts
  hosts: some
  tasks: [{debug: {msg: "Task2"}}]

main.yml中的这里一样,您仍处于剧本级别,因此您可以包括文件本身也是剧本本身。这意味着您也可以随时与ansible-playbook分开运行play1.yml

但是,一旦您处于任务级别,就可以包括仅包含任务的文件:

main.yml
---
- name: run couple of ymls
  hosts: all
  tasks:
    - include: "task1.yml"
    - include: "task2.yml"
task1.yml
---
- name: An actual command
  debug: { msg: "Task 1" }
task2.yml
---
- name: An other actual command
  debug: { msg: "Task 2" }

这也可以,因为task1.ymltask2.yml文件都仅包含任务,并且它们不是不是 成熟的剧本。试图用ansible-playbook单独运行它们将不再起作用,因为它们只是一堆任务。

请注意,在此示例中,如果您要包含play1.yml而不是task1.yml,则剧本将失败,因为您已经处于"任务"级别,从您无法导入更多剧本的地方。<<<<<<<<

从您的tagger.yml文件中删除hosts

---
  - name: tagger - build docker
    whatever task here 
    tags:
      - all
      - tagger

希望帮助您