简体   繁体   中英

To run playbook based on ansible_distribution

I have written separate playbooks for tomcat deployment on both Ubuntu and Linux as well, instead of mentioning **

when: ansible_distribution == 'Ubuntu'

**in every line in the playbook, i want to run the whole playbook only when this condition meets.

This is my code

 - 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:

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

I want to run the playbook only on the hosts based on the ansible_distribution

I tried many ways but no one works can any one post a clear answer with explanation

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

A: It's not possible to include a playbook. This would run the playbooks recursively.

Only import of a playbook is available. Moreover import_playbook is not a task. It's simply a tool to modularize large playbooks with multiple plays.

Ansible conditionals do not apply to import_playbook the same way as they do not apply to playbooks.

Instead, it is possible to create a group that will be used in the playbook.

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

For example, let's create the group and import the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM