使用host_group_vars插件从自定义位置加载嵌套文件



我正在尝试创建ansible角色来生成使用ansible的kubernetes部署。我希望所有自定义都存储在 var 文件中,并在group_vars和host_vars加载期间自动加载。我最终得到以下文件的嵌套结构:

group_vars/my_group.yml

applications:
application_foo: "{{ lookup('template', inventory_dir + '/applications/application_foo.yml') | from_yaml }}"

applications/application_foo.yml

deployments:
application_foo:
deployment:
{{ lookup('template', inventory_dir + '/deployment_vars/deployment_foo.yml')  | to_nice_yaml | indent(6)}}
replicas: 1

deployment_vars/deployment_foo.yml

containers:
conteiner_bar:
container:
{{ lookup('template',inventory_dir + '/container_vars/container_bar.yml') | to_nice_yaml | indent(6) }}
container_baz:
container:
{{ lookup('template',inventory_dir + '/container_vars/container_baz.yml') | to_nice_yaml | indent(6) }}

container_vars/container_bar.yml

container_vars/container_baz.yml

image_url: "https://example.com/image_bar"
cpu_requests: "1"
memory_requests: "512Mi"
cpu_limit: "2"
memory_limit: "1024Mi"
readinessProbe:
exec:
command:
- "sh"
- "-c"
- "true"

playbooks/test-playbook.yml

- hosts: my_group
gather_facts: False
tasks:
- debug:
msg: "{{ applications['application_foo']['deployments']['application_foo']['deployment']['containers']['container_bar']['cpu_requests'] }}"
delegate_to: localhost

当我运行剧本时,它抱怨部署变量是str类型,因此不能有子对象:

fatal: [localhost]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'str object' has no attribute 'containers'nnThe error appears to have been in '/tmp/so_question/playbooks/test-playbook.yml': line 4, column 7, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nn  tasks:n    - debug:n      ^ heren"
}

我希望层在另一个应用程序需要容器或部署的情况下是可重用的,所以不想将所有内容放在一个文件中。

如何解决此问题或如何以其他方式实现相同的嵌套加载的任何建议?

UPD:当我在剧本中剪切字符串以在部署结束时(就像这个msg: "{{ applications['application_foo']['deployments']['application_foo']['deployment'] }}"一样(时,它会给出以下行,在换行符替换后看起来不像有效的 yaml:

containers:
conteiner_bar:
container:
"image_url: \"https://example.com/image_bar\"
cpu_requests: \"1\"
memory_requests:\
\ \"512Mi\"
cpu_limit: \"2\"
memory_limit: \"1024Mi\"
readinessProbe:
exec:
\
\   command:
- \"sh\"
- \"-c\"
- \"true\"
"
container_baz:
container:
"image_url: \"https://example.com/image_bar\"
cpu_requests: \"1\"
memory_requests:\
\ \"512Mi\"
cpu_limit: \"2\"
memory_limit: \"1024Mi\"
readinessProbe:
exec:
\
\   command:
- \"sh\"
- \"-c\"
- \"true\"
"

我想知道为什么缩进过滤器不会缩进所有行。我是否正确使用它?

最终得到以下解决方法:将所有包含默认值的文件移动到 group_vars/all 以使它们自动加载并使用 vars 查找进行嵌套:

$ find group_vars/
group_vars/
group_vars/all
group_vars/all/10_container_bar.yml
group_vars/all/20_deployment_foo.yml
group_vars/all/30_application_foo.yml
group_vars/all/10_container_baz.yml
group_vars/my_group.yml

group_vars/my_group.yml

applications:
application_foo: "{{ lookup('vars', 'defaults_application_application_foo') }}"

group_vars/all/30_application_foo.yml

defaults_application_application_foo:
deployments:
application_foo:
deployment: "{{ lookup('vars', 'defaults_deployment_deployment_foo') }}"
replicas: 1

等等...

仍然不清楚为什么模板的东西不起作用。这个问题悬而未决。

最新更新