Ansible清单:使用主机变量定义组从属关系

时间:2020-01-19 07:28:40

标签: ansible ansible-inventory

我正在设计一种库存结构,以供世界各地的主机使用。 它们按组分组(每个位置有几种类型的主机(设备,而不仅仅是PC \ mac)),并且在众多特征上有所不同,这些特征会影响它们的管理方式(有效地发挥作用)。这意味着天生就有许多组,主机通常是其中几个的成员。

此外,此清单易于更改(由于给定主机的新部署,移动或重新使用)。 由于上述原因,我希望在主机端定义组成员身份,并尽可能使该过程自动化。

我了解了有关add_host和group_by的信息,但不确定:

  1. 如果我正确理解了用法(例如,不确定是否可以将group_by与常规宿主变量一起使用)
  2. 如何最好地利用它们(如果有最佳实践或我缺少的共同考虑-无法找到全面的例子)

以下是属性的一个示例(不是真实的,obvs):

group_vars/host001:
type: "PC"
os: "windows"
os_version: "10"
has_widescreen: True
office_version: "2019"
<more properties>

group_vars/host041:
type: "mac"
os: "macos"
macos_version: "10.13"
<more properties>

group_vars/host053:
type: "mac"
os: "macos"
os_version: "10.13"
office_version: "2007"
<more properties>

然后创建我要使用的组,例如:

# create groups named "windows 10" and "macos 10.13", under the groups "windows" and "macos" accordingly.
- group_by:
    key: {{ os }}-{{ os_version }}
    parents:
      - {{ os }} 

# create groups named "office2007" and office2019"
- group_by:
    key: {{ office_version }}

# create group of hosts with wide screens
add_host: hostname={{ item }} groups=has_widescreen
  with_items: groups['all']
  when: (has_widescreen == True)

这是有效用法吗?推荐吗?有没有一个全面的动态分组示例,可以向许多团体学习?

0 个答案:

没有答案