无法根据另一个变量在字典的ansible中提取嵌套变量

Can't extract a nested variable in ansible of a dict based on another variable

我有一个这样的变量文件

apps:
  appOne:
    slug: none
    ports:
      - "8999:8999"
  kong:
    slug: somerepo/ansible.kong.git
    ports:
      - "8000:8000"
      - "8001:8001"
      - "8443:8443"
      - "8445:8445"

在我的 vars/apps_config 文件中

我试图找出基于 {{ app_name }} 的端口,但似乎无法找到深入应用对象的方法。

所以在我正在执行的 yml 文件中我有

vars_files:
   - "vars/app_config"
  vars:
      app_name: kong
      container_ports: "{{ apps [{{ app_name }}]['ports'] }}"

但 ansible 不喜欢嵌套变量 {{ app_name }}。我之前看到有人提到使用 vars['somestring'] 但以下似乎不起作用

vars['apps.{{ app_name }}']

有什么想法吗?

正确的语法是:

container_ports: "{{ apps[app_name]['ports'] }}"

*在 {{}} 中 app_name 被视为一个变量


测试:

- name: test
  hosts: localhost
  vars_files:
   - "vars/app_config"
  vars:
      app_name: kong
      container_ports: "{{ apps[app_name]['ports'] }}"   
  tasks:
    - debug:
        var: container_ports

结果:

ok: [localhost] => 
  container_ports:
  - 8000:8000
  - 8001:8001
  - 8443:8443
  - 8445:8445