Combining two dictionaries and creating a list of dictionaries with updated credentials

huangapple go评论92阅读模式
英文:

Combining two dictionaries and creating a list of dictionaries with updated credentials

问题

我想要一个帮助手创建一个新的数据结构,用来比较这两个字典,然后创建一个包含字典的列表。思路是,在发生更改时保留旧版本并添加修改后的用户名/密码。

目标:

  1. list: [{"username": "a1", "password": "apass1"}, {"username": "a2", "password": "apass2"}, {"username": "b1", "password": "bpass1"}, {"username": "c1", "password": "cpass1"}, {"c_username": "c1", "c_password": "cpass1"}, {"c_username": "c2", "c2_password": "cpass1"}]

我希望这对于管理我的PostgreSQL数据库中的服务中断很有用。我还从保险库中检索用户名/密码数据。

英文:

I would like a helping hand to create a new data structure, which compares the two dictionaries,

then create a list with dictionaries. The idea being that in case of change to keep the old versions and add the modified usernames/password.

Goal:

  1. list: [{"username": "a1", "password": "apass1"}, {"username": "a2", "password": "apass2"}, {"username": "b1", "password": "bpass1"}, {"username": "c1", "password": "cpass1"}, {"c_username": "c1", "c_password" : "cpass1"} , {"c_username": "c2", "c2_password" : "cpass1"}]
  1. ---
  2. name: "Username/Password version 1"
  3. set_fact:
  4. dict1: {"a_username": "a1", "a_password" : "apass1", "b_username": "b1", "b_password" : "bpass1", "c_username": "c1", "c_password" : "cpass1"}
  5. name: "Username/Password version 2"
  6. set_fact:
  7. dict2: {"a_username": "a2", "a_password" : "apass2", "b_username": "b1", "b_password" : "bpass1", "c_username": "c2", "c2_password" : "cpass1"}

I would like this to be useful for managing service interruptions in my PostgreSQL database.
I also retrieve the username/password data from vault

答案1

得分: 0

Here's the translated content:

给定以下字典

  1. dict1:
  2. a_username: a1
  3. a_password: apass1
  4. b_username: b1
  5. b_password: bpass1
  6. c_username: c1
  7. c_password: cpass1
  8. dict2:
  9. a_username: a2
  10. a_password: apass2
  11. b_username: b1
  12. b_password: bpass1
  13. c_username: c2
  14. c_password: cpass1

问题:"保留旧版本并添加修改后的用户名/密码。"

答案:以下模板

  1. list1: |
  2. {% filter from_yaml %}
  3. {% for i in dict1.keys()|batch(2) %}
  4. - {username: {{ dict1[i.0] }}, password: {{ dict1[i.1] }}}
  5. {% if dict1[i.0] != dict2[i.0] or dict1[i.1] != dict2[i.1] %}
  6. - {username: {{ dict2[i.0] }}, password: {{ dict2[i.1] }}}
  7. {% endif %}
  8. {% endfor %}
  9. {% endfilter %}

生成

  1. list1:
  2. - {password: apass1, username: a1}
  3. - {password: apass2, username: a2}
  4. - {password: bpass1, username: b1}
  5. - {password: cpass1, username: c1}
  6. - {password: cpass1, username: c2}

下一个选项是获取唯一的用户名/密码对并创建一个字典

  1. dict3: "{{ dict((dict1.values()|list + dict2.values()|list)|
  2. batch(2)|unique) }}"

生成

  1. dict3:
  2. a1: apass1
  3. a2: apass2
  4. b1: bpass1
  5. c1: cpass1
  6. c2: cpass1

将字典转换为列表,您将得到相同的结果

  1. list1: "{{ dict3|
  2. dict2items(key_name='username', value_name='password')|
  3. sort(attribute='username') }}"
英文:

Given the dictionaries

  1. dict1:
  2. a_username: a1
  3. a_password: apass1
  4. b_username: b1
  5. b_password: bpass1
  6. c_username: c1
  7. c_password: cpass1
  8. dict2:
  9. a_username: a2
  10. a_password: apass2
  11. b_username: b1
  12. b_password: bpass1
  13. c_username: c2
  14. c_password: cpass1

Q: "Keep the old versions and add the modified usernames/password."

A: The template below

  1. list1: |
  2. {% filter from_yaml %}
  3. {% for i in dict1.keys()|batch(2) %}
  4. - {username: {{ dict1[i.0] }}, password: {{ dict1[i.1] }}}
  5. {% if dict1[i.0] != dict2[i.0] or dict1[i.1] != dict2[i.1] %}
  6. - {username: {{ dict2[i.0] }}, password: {{ dict2[i.1] }}}
  7. {% endif %}
  8. {% endfor %}
  9. {% endfilter %}

gives

  1. list1:
  2. - {password: apass1, username: a1}
  3. - {password: apass2, username: a2}
  4. - {password: bpass1, username: b1}
  5. - {password: cpass1, username: c1}
  6. - {password: cpass1, username: c2}

<hr>

The next option is getting the unique username/password pairs and creating a dictionary

  1. dict3: &quot;{{ dict((dict1.values()|list + dict2.values()|list)|
  2. batch(2)|unique) }}&quot;

gives

  1. dict3:
  2. a1: apass1
  3. a2: apass2
  4. b1: bpass1
  5. c1: cpass1
  6. c2: cpass1

Convert the dictionary to a list and you get the same result

  1. list1: &quot;{{ dict3|
  2. dict2items(key_name=&#39;username&#39;, value_name=&#39;password&#39;)|
  3. sort(attribute=&#39;username&#39;) }}&quot;

huangapple
  • 本文由 发表于 2023年5月26日 00:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定