问题描述
我正在寻找一种在Ansible剧本中将多个默认组指定为主机的方法.我一直在使用这种方法:
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
也不希望创建多个冗余任务-我想避免强迫用户指定 specific_qa_hosts
(例如),并且我想避免代码重新定位:
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?
推荐答案
这确实是可能的,并且在针对主机和群组的通用模式 Ansible文档.
This is indeed possible and is described in the common patterns targeting hosts and groups documentation of Ansible.
因此,针对两个组就像 hosts:group1:group2
这样简单,因此,您的剧本就变成了:
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') }}"
如果您想通过广告资源实现此目标,也可以使用如文档示例中所述:
因此,您的情况应该是:
So in your case, it would be:
[development]
1.2.3.4
[qa]
2.3.4.5
[dev_plus_qa:children]
development
qa
然后,作为一本剧本:
- name: Do things on hosts
hosts: "{{ specific_hosts | default('dev_plus_qa') }}"
这篇关于在Ansible剧本中指定多个默认组作为主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!