首页 > 解决方案 > 合并主机所属组中的所有变量

问题描述

为主机运行剧本时,如何访问该主机所属所有组的所有组合变量?

例如,如果有库存:

all:
  hosts:
    somehost.acme.com
    someotherhost.acme.com
  vars:
    organization: Acme Inc.
us:
  hosts:
    somehost.acme.com
  vars:
    country: US
    city: New York

netherlands:
  hosts:
    someotherhost.acme.com
  vars:
    country: NL
    city: Amsterdam

..我为主机运行一个剧本,然后somehost.acme.com我会得到 和 的变量。organizationcountrycity

UPD

这就是我访问剧本中变量的方式:

vars:
  organization: "{{ hostvars[inventory_hostname].vars.organization }}"
  country: "{{ hostvars[inventory_hostname].vars.country }}"
  city: "{{ hostvars[inventory_hostname].vars.city }}"

标签: ansible

解决方案


推荐阅读