Ansible:如何从另一个剧本中调用剧本?

Ansible:如何从另一个剧本中调用剧本?,ansible,Ansible,我已经编写了一个简单的剧本来打印java进程ID和该PID的其他信息 [root@server thebigone]# cat check_java_pid.yaml --- - hosts: all gather_facts: no tasks: - name: Check PID of existing Java process shell: "ps -ef | grep [j]ava" register: java_status - de

我已经编写了一个简单的剧本来打印java进程ID和该PID的其他信息

[root@server thebigone]# cat check_java_pid.yaml
---
- hosts: all
  gather_facts: no

  tasks:
    - name: Check PID of existing Java process
      shell: "ps -ef | grep [j]ava"
      register: java_status

    - debug: var=java_status.stdout
当我用ansible playbook check\u java\u pid.yaml调用它时,它工作得很好

现在我试着从另一个剧本中调用上面的剧本,但只针对特定的主持人。所以我写了第二个剧本如下

    [root@server thebigone]# cat instance_restart.yaml
    ---
    - hosts: instance_1
      gather_facts: no

      tasks:
        - include: check_java_pid.yaml
但是,在执行ansible playbook实例\u restart.yaml时,我发现了以下错误

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

    The error appears to have been in 
    '/home/root/ansible/thebigone/check_java_pid.yaml': line 2, column 3, but 
    may be elsewhere in the file depending on the exact syntax problem.

    The offending line appears to be:

     ---
     - hosts: all
       ^ here


      The error appears to have been in 
      '/home/root/ansible/thebigone/check_java_pid.yaml': line 2, column 3, 
      but may be elsewhere in the file depending on the exact syntax problem.

     The offending line appears to be:

      ---
      - hosts: all
        ^ here
它说的是语法错误,但没有一个真正的错误,因为我已经毫无问题地执行了Playbook
check\u java\u pid.yaml

请求您帮助理解此问题。

With
include
Ansible需要的文件仅包含任务,而不是完整的剧本。然而,你提供了一个完整的剧本作为论据

你可以这样做(包括),但它不会让你实现你想要的

使用
hosts:all
定义的游戏将始终针对所有目标运行(除非您在命令调用或资源清册中对其进行限制)

此外,从另一个剧本(如果这是您的目标)中访问
java\u状态
值时会遇到问题


您需要重新考虑您的结构,例如,您可以从两个重头戏中提取任务并将其包括在内:

my_tasks.yml

- name: Check PID of existing Java process
  shell: "ps -ef | grep [j]ava"
  register: java_status

  - debug: var=java_status.stdout
---
- hosts: all
  gather_facts: no

  tasks:
    - include my_tasks.yml
---
- hosts: instance_1
  gather_facts: no

  tasks:
    - include: my_tasks.yml
检查java\u pid.yml

- name: Check PID of existing Java process
  shell: "ps -ef | grep [j]ava"
  register: java_status

  - debug: var=java_status.stdout
---
- hosts: all
  gather_facts: no

  tasks:
    - include my_tasks.yml
---
- hosts: instance_1
  gather_facts: no

  tasks:
    - include: my_tasks.yml
instance\u restart.yml

- name: Check PID of existing Java process
  shell: "ps -ef | grep [j]ava"
  register: java_status

  - debug: var=java_status.stdout
---
- hosts: all
  gather_facts: no

  tasks:
    - include my_tasks.yml
---
- hosts: instance_1
  gather_facts: no

  tasks:
    - include: my_tasks.yml

这里有官方文档中的示例

在应用了经批准的答案后,我犯了与你相同的错误。我通过创建这样的主剧本解决了这个问题:

---
- import_playbook: master-okd.yml
- import_playbook: infra-okd.yml
- import_playbook: compute-okd.yml

谢谢你再看一次。现在有意义了。一旦我尝试过,我会更新你。正在工作,现在我明白了,再次感谢你。这不是OP的答案。