简体   繁体   English

Ansible include_vars进入字典

[英]Ansible include_vars into dictionary

In my ansible playbook, I read a list of directories into a list. 在我的ansible剧本中,我将目录列表读入列表。 I then want to read a "config.yml" file from each of these directories and put their content into dictionary, so that I can reference the config-data via the directory name from that dictionary. 然后,我想从每个目录中读取一个“ config.yml”文件并将其内容放入字典中,以便可以通过该字典中的目录名称引用config-data。 The first part is no problem, but I cannot get the second part to work: 第一部分没有问题,但是我无法使第二部分工作:

Step 1, load directories: 步骤1,加载目录:

- name: Include directories
  include_vars:
  file: /main-config.yml
  name: config

Step 2, load configs from directories: 步骤2,从目录加载配置:

- name: load deploymentset configurations
  include_vars:
    file: /path/{{ item }}/config.yml
    name: "allconfs.{{ item }}"   ## << This is the problematic part 
  with_items:
    - "{{ config.dirs }}"

I tried different things like "allconfs['{{ item }}'] , but none seemed to work. The playbook completed successfully, but the data was not in the dictionary. I also tried defining the outer dictionary beforehand, but that did not work either. 我尝试了诸如"allconfs['{{ item }}'] ,但似乎都无法正常工作。剧本已成功完成,但数据不在词典中。我也尝试过预先定义外部词典,但没有要么工作。

The config files themselves are very simple: 配置文件本身非常简单:

/main-config.yml: /main-config.yml:

dirs:
- dir1
- dir2
- dir3

/path/dir1/config.yml: /path/dir1/config.yml:

some_var: "some_val"
another_var: "another val"

I want to be able to then access the values of the config.yml files like this: 我希望能够然后像这样访问config.yml文件的值:

{{ allconfs.dir1.some_var }}

UPDATE to try Konstantins approach: 尝试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]')) }}"

Error message: 错误信息:

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)"}

Here is a piece of code from one of my projects with similar functionality: 这是我的一个项目中具有类似功能的一段代码:

- name: Load config defaults
  include_vars:
    file: "{{ item }}.yml"
    name: "default_config"
  with_items: "{{ config_files }}"
  register: default_configs
  tags:
    - configuration

- name: Combine configs into one dict
  # Здесь мы делаем словарь вида
  # default_configs:
  #   config_name_1: { default_config_object }
  #   config_name_2: { default_config_object }
  #   config_name_3: { default_config_object }
  #   ...
  set_fact:
    default_configs: "{{ dict(default_configs.results | json_query('[].[item, ansible_facts.default_config]')) }}"
  tags:
    - configuration

default_config is a dummy var to temporary load var data. default_config是用于临时加载var数据的虚拟var。
The trick is to use register: default_configs with include_vars and parse it with the following task stripping out unnecessary fields. 诀窍是使用register: default_configsinclude_vars并通过以下任务将其解析以除去不必要的字段。

AFAIK it isn't possible to create a single dictionary that encompasses multiple include_vars . AFAIK无法创建包含多个include_vars的单个字典。 From my testing it would create separate dictionaries for each included directory. 根据我的测试,它将为每个包含的目录创建单独的字典。 Here's what you can do instead. 您可以改用以下方法。

Remove allconfs. 删除所有allconfs. from your variable name. 从您的变量名。

- name: load deploymentset configurations
  include_vars:
    file: /path/{{ item }}/config.yml
    name: "{{ item }}"
  with_items:
    - "{{ config.dirs }}"

You can then either access variables directly with 然后,您可以直接使用

debug:
  msg: "{{ dir1.some_var }}"
with_items: "{{ config.dirs }}"

Or if you need to loop through all variables in your included directories use this (hoisted from Ansible: how to construct a variable from another variable and then fetch it's value ). 或者,如果您需要遍历包含目录中的所有变量,请使用此方法(从Ansible提出:如何从另一个变量构造一个变量,然后获取其值 )。

debug:
  msg: "{{ hostvars[inventory_hostname][item].some_var }}"
with_items: "{{ config.dirs }}"

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

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