Ansible include_vars 到字典中

feo*_*eob 5 dictionary yaml ansible

在我的 ansible playbook 中,我将目录列表读入列表。然后我想从这些目录中的每一个读取“config.yml”文件并将它们的内容放入字典中,以便我可以通过该字典中的目录名称引用配置数据。第一部分没问题,但我无法让第二部分工作:

第一步,加载目录:

- name: Include directories
  include_vars:
  file: /main-config.yml
  name: config
Run Code Online (Sandbox Code Playgroud)

第 2 步,从目录加载配置:

- name: load deploymentset configurations
  include_vars:
    file: /path/{{ item }}/config.yml
    name: "allconfs.{{ item }}"   ## << This is the problematic part 
  with_items:
    - "{{ config.dirs }}"
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的东西,例如"allconfs['{{ item }}'],但似乎都不起作用。剧本成功完成,但数据不在字典中。我也尝试过预先定义外部字典,但这也不起作用。

配置文件本身非常简单:

/main-config.yml:

dirs:
- dir1
- dir2
- dir3
Run Code Online (Sandbox Code Playgroud)

/path/dir1/config.yml:

some_var: "some_val"
another_var: "another val"
Run Code Online (Sandbox Code Playgroud)

我希望能够像这样访问 config.yml 文件的值:

{{ allconfs.dir1.some_var }}

更新以尝试 Konstantins 方法:

  - name: load deploymentset configurations
    include_vars:
      file: /repo/deploymentsets/{{ item }}/config.yml
      name: "default_config"
    with_items:
    - "{{ config.deploymentsets }}"
    register: default_configs


  - name: combine configs
    set_fact:
      default_configs: "{{ dict(default_configs.results | json_query('[].[item, ansible_facts.default_config]')) }}"
Run Code Online (Sandbox Code Playgroud)

错误信息:

fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "Unexpected templating type error occurred on ({{ dict(default_configs.results | json_query('[].[item, ansible_facts.default_config]')) }}): <lambda>() takes exactly 0 arguments (1 given)"}

Kon*_*rov 5

这是我的一个项目中的一段具有类似功能的代码:

\n\n
- name: Load config defaults\n  include_vars:\n    file: "{{ item }}.yml"\n    name: "default_config"\n  with_items: "{{ config_files }}"\n  register: default_configs\n  tags:\n    - configuration\n\n- name: Combine configs into one dict\n  # \xd0\x97\xd0\xb4\xd0\xb5\xd1\x81\xd1\x8c \xd0\xbc\xd1\x8b \xd0\xb4\xd0\xb5\xd0\xbb\xd0\xb0\xd0\xb5\xd0\xbc \xd1\x81\xd0\xbb\xd0\xbe\xd0\xb2\xd0\xb0\xd1\x80\xd1\x8c \xd0\xb2\xd0\xb8\xd0\xb4\xd0\xb0\n  # default_configs:\n  #   config_name_1: { default_config_object }\n  #   config_name_2: { default_config_object }\n  #   config_name_3: { default_config_object }\n  #   ...\n  set_fact:\n    default_configs: "{{ dict(default_configs.results | json_query(\'[].[item, ansible_facts.default_config]\')) }}"\n  tags:\n    - configuration\n
Run Code Online (Sandbox Code Playgroud)\n\n

default_config是一个临时加载 var 数据的虚拟 var。
\n技巧是使用register: default_configswithinclude_vars并通过以下任务解析它,删除不必要的字段。

\n


kfr*_*ezy 1

AFAIK 不可能创建包含多个include_vars. 根据我的测试,它将为每个包含的目录创建单独的字典。您可以执行以下操作。

allconfs.从变量名中删除。

- name: load deploymentset configurations
  include_vars:
    file: /path/{{ item }}/config.yml
    name: "{{ item }}"
  with_items:
    - "{{ config.dirs }}"
Run Code Online (Sandbox Code Playgroud)

然后您可以直接使用访问变量

debug:
  msg: "{{ dir1.some_var }}"
with_items: "{{ config.dirs }}"
Run Code Online (Sandbox Code Playgroud)

或者,如果您需要循环遍历包含目录中的所有变量,请使用此(从Ansible 提升:如何从另一个变量构造变量,然后获取其值)。

debug:
  msg: "{{ hostvars[inventory_hostname][item].some_var }}"
with_items: "{{ config.dirs }}"
Run Code Online (Sandbox Code Playgroud)