简体   繁体   English

Ansible dict:如何按键分组并设置事实以供以后重复使用

[英]Ansible dict: How to group by a key and set fact to be re-used later

I've a CSV which I converted into a dict using read_csv module.我有一个 CSV,我使用read_csv模块将其转换为字典。 My aim is to group the data by a field within the dictionary.我的目标是按字典中的字段对数据进行分组。 For example, in below data I want to use firstname & secondname group by departmentGroup , so that I get a dict array to be re-used later例如,在下面的数据中,我想按departmentGroup使用firstname & secondname组,这样我就可以得到一个 dict 数组,以便稍后重新使用

Below is the input dict created from csv下面是从 csv 创建的输入字典

      {
        {
            "ID02": {
                "department": "IT",
                "departmentGroup": "IT-development",
                "firstname": "first02",
                "id": "ID02",
                "salary": "40000",
                "secondname": "surnam2",
                "subDepartment": "development"
            }
        },
        {
            "ID03": {
                "department": "IT",
                "departmentGroup": "IT-development",
                "firstname": "first03",
                "id": "ID03",
                "salary": "42000",
                "secondname": "surnam3",
                "subDepartment": "development"
            }
        },
        {
            "ID04": {
                "department": "IT",
                "departmentGroup": "IT-operations",
                "firstname": "first04",
                "id": "ID04",
                "salary": "46000",
                "secondname": "surnam4",
                "subDepartment": "operations"
            }
        },
        {
            "ID05": {
                "department": "IT",
                "departmentGroup": "IT-operations",
                "firstname": "first05",
                "id": "ID05",
                "salary": "42000",
                "secondname": "surnam5",
                "subDepartment": "operations"
            }
        }

I'm looking for output of values grouped by departmentGroup , so i can loop it for future tasks to build-up templates我正在寻找 output 按departmentGroup分组的值,所以我可以循环它以用于将来的任务以构建模板

{ 
  "IT-development": 
    [
      {"id": "ID02", "firstname": "first02", "secondname": "surnam2"},
      {"id": "ID03", "firstname": "first03", "secondname": "surnam3"}    
    ],
  "IT-operations": 
    [
      {"id": "ID04", "firstname": "first04", "secondname": "surnam4"},
      {"id": "ID05", "firstname": "first05", "secondname": "surnam5"}
    ]
}

I've tried using map but couldn't reach much.我试过使用map但没用多少。 Any ideas on how to group in such a way in ansible?关于如何在 ansible 中以这种方式分组的任何想法?

I confirmed that I could do it by using the filters only.我确认我可以只使用过滤器来做到这一点。
The below is simpler than using Jinja2 code.下面比使用 Jinja2 代码更简单。

---
- name: Example Playbook for 65447303
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Read a csv file and converts to dict
      read_csv:
        path: test.csv
        key: name
      register: csv_result

    - debug: msg="{{ csv_result }}"

    - name: Set combined data variable
      set_fact:
        combined_data: >-
          {{ combined_data | default({})
            | combine({item.value.departmentGroup: []
            + [{
                'id': item.value.id,
                'firstname': item.value.firstname,
                'secondname': item.value.secondname
              }]
            + combined_data[item.value.departmentGroup] | default([]) })
          }}
      with_dict: "{{ csv_result.dict }}"

    - debug: msg="{{ combined_data }}"

I could do it by using Jinja2.我可以使用 Jinja2 来完成。
It wasn't easy to do it using the Ansible or Jinja2 filters only.仅使用 Ansible 或 Jinja2 过滤器并不容易。
So I implemented the code using Jinja2.所以我使用 Jinja2 实现了代码。
How about this like?这个怎么样?

csv file csv 档案

(venv) $ vi test.csv
name,department,departmentGroup,firstname,id,salary,secondname,subDepartment
ID02,IT,IT-development,first02,ID02,40000,surnam2,development
ID03,IT,IT-development,first03,ID03,42000,surnam3,development
ID04,IT,IT-operations,first04,ID04,46000,surnam4,operations
ID05,IT,IT-operations,first05,ID05,42000,surnam5,operations

Playbook剧本

(venv) $ vi main.yml
---
- name: Example Playbook for 65447303
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Read a csv file and converts to dict
      read_csv:
        path: test.csv
        key: name
      register: csv_result

    - debug: msg="{{ csv_result }}"

    - name: Set combined data variable
      set_fact:
        combined_data: |
          {% set data = {} %}
          {% set tmp = {} %}
          {# initialize data variable #}
          {% for key in csv_result.dict.keys() %}
          {%     set _ = tmp.update(csv_result.dict[key]) %}
          {%     set _ = data.update({tmp['departmentGroup']: []}) %}
          {# Append the data to each department key #}
          {% endfor %}
          {% for key in csv_result.dict.keys() %}
          {%     set _ = tmp.update(csv_result.dict[key]) %}
          {%     set _ = data[tmp['departmentGroup']].append({
                           'id': tmp['id'],
                           'firstname': tmp['firstname'],
                           'secondname': tmp['secondname']
                         }) %}
          {% endfor %}
          {{ data }}

    - debug: msg="{{ combined_data | type_debug }}"

    - debug: msg="{{ combined_data }}"

Execute and Result执行和结果

(venv) $ ansible-playbook main.yml
(snip)
TASK [debug] ***************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "IT-development": [
            {
                "firstname": "first02",
                "id": "ID02",
                "secondname": "surnam2"
            },
            {
                "firstname": "first03",
                "id": "ID03",
                "secondname": "surnam3"
            }
        ],
        "IT-operations": [
            {
                "firstname": "first04",
                "id": "ID04",
                "secondname": "surnam4"
            },
            {
                "firstname": "first05",
                "id": "ID05",
                "secondname": "surnam5"
            }
        ]
    }
}
(snip)

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

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