我正在尝试在Ansible中使用变量覆盖。为此,我创建了下面描述的目录结构。注意,在清单下,我创建了两个单独的站点(1 & 2),注意,我在两个不同的级别添加了group_vars/host_vars;在库下面和每个站点下面。
.
├── ansible.cfg
├── inventories
│ ├── group_vars
│ │ └── all.yml
│ ├── host_vars
│ │ └── target2.yml
│ ├── site1
│ │ ├── group_vars
│ │ │ └── all.yml
│ │ ├── host_vars
│ │ │ └── target1.yml
│ │ └── hosts.yml
│ └── site2
│ ├── group_vars
│ │ └── all.yml
│ ├── host_vars
│ │ └── target2.yml
│ └── hosts.yml
├── modules
├── playbooks
│ └── playbook1
│ ├── group_vars
│ │ └── all.yml
│ └── host_vars
└── roles我希望能够在“库存”级别存储组/主机的默认变量,并在/如果需要时在站点/组/主机级别使用目录(而不是hosts.yml)覆盖它们,但我无法这样做。
如果我以基本的“库存”目录为目标来测试库存,我可以看到sites下的group_vars/host_var文件夹被忽略了:
ansible-inventory --vars --graph -i inventories/
@all:
|--@site1:
| |--target1
| | |--{scope = inventories/site1/hosts.yml}
| |--target2
| | |--{scope = inventories/host_vars/target2.yml}
|--@site2:
| |--target2
| | |--{scope = inventories/host_vars/target2.yml}
|--@ungrouped:
|--{scope = inventories/group_vars/all.yml}但是,如果我以特定站点为目标,则使用基础组_vars/host_var文件夹,但当然忽略了基本“库存”中的文件夹:
ansible-inventory --vars --graph -i inventories/site1
@all:
|--@site1:
| |--target1
| | |--{scope = inventories/site1/host_vars/target1.yml}
| |--target2
| | |--{scope = inventories/site1/group_vars/all.yml}
|--@ungrouped:
|--{scope = inventories/site1/group_vars/all.yml}
ansible-inventory --vars --graph -i inventories/site2
@all:
|--@site2:
| |--target2
| | |--{scope = inventories/site2/host_vars/target2.yml}
| |--{scope = inventories/site2/group_vars/all.yml}
|--@ungrouped:是否可以指示ansible在整个目录结构中查找group_vars/host_var文件夹?
谢谢!
发布于 2022-09-07 07:10:22
根据您的描述和示例,您的集合site1和site2已经是set all的子集。你做了这个观察,并在你的问题中描述了它。
如果我以特定站点为目标,则使用基础组_vars/host_var文件夹,但当然忽略了基本“库存”中的文件夹。
此外,使用的命令还提供了一个提示。
ansible-inventory --vars --graph -i inventories/site1 # or 2因为这样,您将把树结构或图的根设置为子树或图的一部分。
是否可以指示Ansible在整个目录结构中查找group_vars/host_var文件夹?
不,因为在定义的子集、子树或图部分之外没有其他目录。
,但当然,在基础“库存”的那个被忽略了。
换句话说,基本“库存”不再存在,因为您已经将基准设置为“库存/站点1”或“2”。
另一种方法可以是为每个站点提供一份清单。
https://stackoverflow.com/questions/73624136
复制相似问题