首页 > 解决方案 > Ansible Inventory 文件优先级

问题描述

我一直在阅读varsAnsible 中的优先级,但我不确定我得到的行为是否是设计使然。这是我的 Ansible 库存;

---

all:
  children:
    we:
      children:
        workers:
          hosts:
            worker-we-vm1:
              order: 1
            worker-ew-vm2:
              order: 2
          vars:
            size: "Standard_B2s"
        controllers:
          hosts:
            controller-we-vm:
              order: 1
          vars:
            size: "Standard_B1s"
      vars:
        ip_prefix: "10.60"
    scus:
      children:
        workers:
          hosts:
            worker-scus-vm1:
              order: 1
            worker-scus-vm2:
              order: 2
          vars:
            size: "Standard_B2s"
      vars:
        ip_prefix: "10.61"
  vars:
    azure_profile: "test"

现在我所we期望的是,当访问. 但事实并非如此,所有虚拟机都设置为.worker-we-vm1ip_prefix10.60scusworker-scus-vm1ip_prefix10.61hostvarsip_prefix10.60

谁能帮助理解为什么vars优先级没有按预期工作?我已经阅读了https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html,它似乎与如何处理父变量和同级变量相矛盾。

标签: ansibleansible-inventory

解决方案


Ansible 主机组命名空间是扁平的,不是分层的,因此您的示例中只有一个workers组。

当您为该组定义相同的变量两次时,只保留最后一个值。

workers例如,将您的组拆分为workers_weworkers_scus(如果这是定义的原因,则使用主机模式)。


推荐阅读