如何将键值对添加到列表中的每个字典 [英] How to add key-value-pair to each dictionary in a list

查看:96
本文介绍了如何将键值对添加到列表中的每个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一个键和一个值添加到一个列表中,该列表从 csv 文件中读取到一个变量中.

I have to add a key and a value to a list which is read from a csv-file into a variable.

为了更好地解释它.首先,我从 csv 文件加载一个列表:

To explain it a bit better. First I load a list from a csv-file:

- name: Load user list from csv
  community.general.read_csv:
    path: ../files/example-prod.csv
    delimiter: ','
  register: users
  delegate_to: localhost

当我查看用户"时变量我得到以下输出:

When I look at the "users" variable I get the following output:

"list": [
        {
            "firstName": "Max",
            "gid": "2002",
            "homedir": "example",
            "lastName": "Mustermann",
            "uid": "m.example"
        },
        {
            "firstName": "Martin",
            "gid": "2002",
            "homedir": "staff",
            "lastName": "Muster",
            "uid": "martin.muster"
        }
    ]

现在我需要为列表中的所有用户生成密码.所以我想遍历项目并为列表中的每个项目添加一个新的键值对.

Now I need to generate passwords for all users in the list. So I want to loop through the items and add a new key-value-pair to each item in the list.

密码创建循环后我想要的输出是:

The output that I want after the password creation loop is:

"list": [
        {
            "firstName": "Max",
            "gid": "2002",
            "homedir": "example",
            "lastName": "Mustermann",
            "uid": "m.example",
            "password": "examplepassword"
        },
        {
            "firstName": "Martin",
            "gid": "2002",
            "homedir": "staff",
            "lastName": "Muster",
            "uid": "martin.muster",
            "password": "examplepassword"
        }
    ]

...所以我可以稍后读出变量.

...so I can read out the variables later.

如何实现?我已经阅读了很多关于如何使用字典执行此操作的内容,但由于我在这里使用的是列表,因此我找不到它.

How to accomplish this? I've read many things about how to do this with a dictionary, but since I'm using a list here, I could not find it out.

推荐答案

您实际上是从 read_csv 模块获取字典列表.list"的内容是一个列表,但是这个列表中的元素是字典.

You are actually getting a list of dictionaries from the read_csv module. The content of "list" is a list, but the elements in this list are dictionaries.

您要做的是为每个字典添加一个键值对.
这是生成随机密码的示例.有关详细信息,请参阅密码查找文档生成密码:

What you want to do is to add a key-value-pair to each dictionary.
Here is an example generating random passwords. See the password-lookup documentation on details how it generates passwords:

- set_fact:
    users: |
      {{ users.list | map('combine', { "password": "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits length=8') }}"}) }}

# this resolves the template added above and is required to avoid changing passwords on each resolve of users
- set_fact:
    users: "{{ users }}"

- debug:
    msg: "{{ users }}"

说明:

  • users.list 包含您的用户列表
  • map('combine', <dict>) 对输入列表 (users.list) 中的每个字典运行 combine 过滤器在这种情况下)并将 的内容添加到它
  • { "password": "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits length=8') }}"} 是我们的字典将每个输入字典与
  • 结合起来
  • password" 是键,值是生成密码的查找
  • users.list contains your list of users
  • map('combine', <dict>) runs the combine filter on each dictionary in the input list (users.list in this case) and adds the contents of <dict> to it
  • { "password": "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits length=8') }}"} is the dictionary we combine each input-dictionary with
  • "password" is the key, and the value is the lookup that is generating the password

备注:
我使用 yaml 的多行字符串文字来避免引用地狱.

Remark:
I use the multi-line string literal of yaml to avoid the quoting-hell.

这篇关于如何将键值对添加到列表中的每个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆