Specifying multiple default groups as hosts in an Ansible playbook

情到浓时终转凉″ 提交于 2021-01-28 01:50:55

问题


I'm looking for a way to specify multiple default groups as hosts in an Ansible playbook. I've been using this method:

- name:  Do things on hosts
  hosts: "{{ specific_hosts | default('development') }}"
  tasks:  # do things on hosts

However I'm trying to avoid specifying hosts manually (it is error-prone), and the default hosts are inadequate if I want to run the same tasks against multiple groups of servers (for instance, development and QA).

I don't know if this is possible in a playbook:

- name:  Do things on hosts
  hosts: "{{ specific_hosts | default('development') && default('qa') }}"

I don't know if this is possible in an inventory:

[development]
1.2.3.4

[qa]
2.3.4.5

[dev_plus_qa]
development
qa

Creating multiple redundant tasks is undesirable as well - I would like to avoid forcing users to specify specific_qa_hosts (for instance) and I would like to avoid code repitition:

- name: Do things on DEV
  hosts: "{{ specific_hosts | default('development') }}"
- name: Do things on QA
  hosts: "{{ specific_hosts | default('qa') }}"

Is there any elegant way of accomplishing this?


回答1:


This is indeed possible and is described in the common patterns targeting hosts and groups documentation of Ansible.

So targeting two groups is as simple as hosts: group1:group2, and, so, your playbook becomes:

- name:  Do things on hosts
  hosts: "{{ specific_hosts | default('development:qa') }}"

And if you rather want to achieve this via the inventory, this is also possible, as described in the documentation examples:

So in your case, it would be:

[development]
1.2.3.4

[qa]
2.3.4.5

[dev_plus_qa:children]
development
qa

And then, as a playbook:

- name:  Do things on hosts
  hosts: "{{ specific_hosts | default('dev_plus_qa') }}"


来源:https://stackoverflow.com/questions/65294947/specifying-multiple-default-groups-as-hosts-in-an-ansible-playbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!