运行 基于 ansible_distribution 的剧本

To run playbook based on ansible_distribution

我也为 tomcat 在 Ubuntu 和 Linux 上的部署编写了单独的剧本,而不是提及 **

when: ansible_distribution == 'Ubuntu'

**在剧本的每一行中,我只想 运行 仅当满足此条件时才使用整个剧本。

这是我的代码

 - hosts: all
   tasks:
      - name: including the playbook for ubuntu deployment
        include: tomcat_ubuntu_new.yaml
        when: ansible_distribution == 'Ubuntu'

      - name: including the playbook for ubuntu deployment
        include: tomcat_standalone.yaml
        when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'

错误:

ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>

The error appears to be in '/etc/ansible/tomcat_ubuntu_new.yaml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- hosts: all
  ^ here

我想 运行 剧本只在主机上基于 ansible_distribution

我尝试了很多方法,但没有人能行得通post一个明确的答案和解释

Q: "I want to run the playbook only on the hosts based on the ansible_distribution."

A:包含剧本是不可能的。这将 运行 剧本递归。

只有 导入 剧本可用。此外 import_playbook 不是任务。它只是一个将具有多个剧本的大型剧本模块化的工具。

Ansible conditionals 不适用于 import_playbook 就像它们不适用于剧本一样。

相反,可以创建一个将在剧本中使用的组。

$cat tomcat_ubuntu_new.yaml
---
- hosts: my_dynamic_group
  tasks:
  ...

比如我们建群导入playbook

---
- hosts: all
  tasks:
    - add_host:
        name: "{{ item }}"
        groups: my_dynamic_group
      loop: "{{ groups.all }}"
      when: hostvars[item].ansible_distribution == 'Ubuntu'
      run_once: true

- import_playbook: tomcat_ubuntu_new.yaml