简体   繁体   English

Ansible 如何找到库存、group_vars、all.yaml、user_vars 的位置

[英]How does Ansible find the location of inventory, group_vars, all.yaml, user_vars

My ansible.cfg does not setup inventory, so I do not use /etc/ansible/hosts to decide inventory.我的 ansible.cfg 没有设置库存,所以我不使用 /etc/ansible/hosts 来决定库存。

https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#understanding-variable-precedence https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#understanding-variable-precedence

said Ansible determines variables based on precedence. Ansible 根据优先级确定变量。 But to do that, Ansible needs to first find the variable files, and then understand what types the files are, only after that it can determine the precedence right?但是要做到这一点,Ansible 需要先找到变量文件,然后了解文件是什么类型,然后才能确定优先级对吗? so my questions are:所以我的问题是:

1/ if there are multiple sub-folders under the main ansible playbook folder, how does Ansible know where to find the variable files? 1/如果在主要的ansible playbook文件夹下有多个子文件夹,Ansible如何知道在哪里可以找到变量文件? Or maybe it simply collects all files under the main folder?或者它可能只是收集主文件夹下的所有文件?

2/ How does Ansible know the type of a variable file, ie whether they are inventory group_vars/all, playbook group_vars/all, inventory host_vars/*, etc? 2/ Ansible 如何知道变量文件的类型,即它们是否是inventory group_vars/all、playbook group_vars/all、inventory host_vars/* 等? Note Ansible needs to know the types of the files in order to determined the variable precedence.注意 Ansible 需要知道文件的类型才能确定变量优先级。

I have these questions because I work on a Ansible playbook, its current layout is:我有这些问题是因为我在编写 Ansible playbook,它当前的布局是:

main_playbook.yaml
inventories/dev/rds/group_vars/all.yaml

inventories/dev/rds/group_vars/rds_something.yaml

Although Ansible playbook does not mention path, the Ansible magically knows the variables defined in all the variable files.虽然 Ansible playbook 没有提到路径,但 Ansible 神奇地知道所有变量文件中定义的变量。

If Ansible can magically collect all these files, then how do I design the project so that it will know when to collect which files under different environments?如果 Ansible 可以神奇地收集所有这些文件,那么我如何设计项目以便它知道何时收集不同环境下的哪些文件? Currently it only has dev environment but I plan to have test environment as well.目前它只有开发环境,但我计划也有测试环境。 But I do not know where to put variable files about test environment.但是我不知道把关于测试环境的变量文件放在哪里。

This is a good question, ansible does not make it's magic clear enough.这是一个很好的问题,ansible 并没有让它的魔法足够清晰。

1/ if there are multiple sub-folders under the main ansible playbook folder, how does Ansible know where to find the variable files? 1/如果在主要的ansible playbook文件夹下有多个子文件夹,Ansible如何知道在哪里可以找到变量文件? Or maybe it simply collects all files under the main folder?或者它可能只是收集主文件夹下的所有文件?

In ansible there is no real concept of a main folder.在 ansible 中,没有主文件夹的真正概念。 That said, for purposes of variable resolution, you could think about the "main" folder being wherever your inventory/hosts file is.也就是说,出于可变分辨率的目的,您可以将“主”文件夹视为库存/主机文件所在的位置。

Ansible always looks for host_vars/ and group_vars/ relative to your inventory file. Ansible 总是查找与您的清单文件相关的host_vars/group_vars/ "relative to your inventory file" means "in the same folder as your inventory file" “相对于您的库存文件”意味着“与您的库存文件在同一文件夹中”

So the location of your inventory (hosts) file is important.因此,您的清单(主机)文件的位置很重要。

So your project layout should look like this for multiple environments, if you want to keep them 100% separate:所以你的项目布局对于多个环境应该是这样的,如果你想让它们 100% 分开:

project
├── inventories/
│   ├── dev/
│   │   └── rds/
│   │       ├── group_vars/
│   │       │   ├── all.yaml
│   │       │   └── rds_something.yaml
│   │       └── inventory.yml
│   └── test/
│       └── rds/
│           ├── group_vars/
│           │   ├── all.yaml
│           │   └── rds_something.yaml
│           └── inventory.yml
└── main_playbook.yml

2/ How does Ansible know the type of a variable file, ie whether they are inventory group_vars/all, playbook group_vars/all, inventory host_vars/*, etc? 2/ Ansible 如何知道变量文件的类型,即它们是否是inventory group_vars/all、playbook group_vars/all、inventory host_vars/* 等?

Ansible knows based on what folder it is in. Ansible 知道它在哪个文件夹中。

If there is a file named all.yml in the group_vars folder, then ansible knows it contains varaibles that apply to all groups.如果group_vars文件夹中有一个名为all.yml的文件,则 ansible 知道它包含适用于所有组的变量。

If there is a file named rds_something.yml in the group_vars folder, then ansible knows it contains variables for all hosts in the group rds_something .如果group_vars文件夹中有一个名为rds_something.yml的文件,那么 ansible 知道它包含rds_something组中所有主机的变量。

If there is a file named db01.yml in the host_vars folder, then ansible knows it contains variables that apply only to the db01 host.如果host_vars文件夹中有一个名为db01.yml的文件,那么 ansible 知道它包含仅适用于db01主机的变量。 If db01 is not in your inventory file, then ansible just ignores it.如果db01不在您的清单文件中,则 ansible 将忽略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM