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

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

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

问题

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

目标:

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:

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"}]
---
name: "Username/Password version 1"
set_fact:
  dict1: {"a_username": "a1", "a_password" : "apass1", "b_username": "b1", "b_password" : "bpass1", "c_username": "c1", "c_password" : "cpass1"}

name: "Username/Password version 2"
set_fact:
  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:

给定以下字典

dict1:
  a_username: a1
  a_password: apass1
  b_username: b1
  b_password: bpass1
  c_username: c1
  c_password: cpass1

dict2:
  a_username: a2
  a_password: apass2
  b_username: b1
  b_password: bpass1
  c_username: c2
  c_password: cpass1

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

答案:以下模板

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

生成

list1:
  - {password: apass1, username: a1}
  - {password: apass2, username: a2}
  - {password: bpass1, username: b1}
  - {password: cpass1, username: c1}
  - {password: cpass1, username: c2}

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

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

生成

dict3:
  a1: apass1
  a2: apass2
  b1: bpass1
  c1: cpass1
  c2: cpass1

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

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

Given the dictionaries

    dict1:
      a_username: a1
      a_password: apass1
      b_username: b1
      b_password: bpass1
      c_username: c1
      c_password: cpass1
      
    dict2:
      a_username: a2
      a_password: apass2
      b_username: b1
      b_password: bpass1
      c_username: c2
      c_password: cpass1

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

A: The template below

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

gives

    list1:
      - {password: apass1, username: a1}
      - {password: apass2, username: a2}
      - {password: bpass1, username: b1}
      - {password: cpass1, username: c1}
      - {password: cpass1, username: c2}

<hr>

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

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

gives

    dict3:
      a1: apass1
      a2: apass2
      b1: bpass1
      c1: cpass1
      c2: cpass1

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

    list1: &quot;{{ dict3|
               dict2items(key_name=&#39;username&#39;, value_name=&#39;password&#39;)|
               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:

确定