问题描述
我正在寻找一种在 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 playbook 中指定多个默认组作为主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!