ansible 2 \ include other yml 不起作用,尽管直接调用有效

ansible 2 \ include other yml doesn't work, though direct call is working

我的 all.yml 里有这个 不能 运行 tagger.yml(虽然当 运行ning 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.\n\nThe error appears to have been in '.Build/tagger.yml': line 2, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n  - name: tagger - build docker\n    ^ here\n\n\nThe error appears to have been in '.Build/tagger.yml': line 2, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n  - name: tagger - build docker\n    ^ here\n"}

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

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

希望对你有帮助

Ansible有两种"levels",一种是playbook级别,可以提供plays,一种是task级别,可以提供tasks。包含在两个级别上都有效,但如果您已经处于任务级别,您将无法包含新游戏。

例如这样就可以了:

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 中,您仍处于剧本级别,因此您可以包含文件,这些文件本身也是剧本。这意味着您也可以随时 运行 play1.ymlansible-playbook 分开。

但是,一旦处于任务级别,就只能包含仅包含任务的文件:

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,那么剧本将失败,因为您已经处于 "tasks" 级别,从那里您无法再导入任何剧本。