如何创建新的键值对并将它们添加到现有列表中?

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

How to create new key: value items and add them to an existing list?

问题

这是一个 Ansible 问题。

这是我的清单:

  userdata.list:
  - ClassType: 全职
    FirstName: Grace
    LastName: Higgins
    Username: g.higgins
  - ClassType: 兼职
    FirstName: Robert
    LastName: Miller
    Username: robmil
  - ClassType: 灵活
    FirstName: Jeffrey
    LastName: Keller
    Username: jeff.k

我想要将以下值附加到现有列表,循环每个用户:

EmailAddress: {{ Username }} 附加到 '@gmail.com'
OtherAddresses: primary:{{ Username }}@gmail.com,secondary:{{ Username }}@outlook.com
Registered: 是

预期输出:

  userdata.list:
  - ClassType: 全职
    FirstName: Grace
    LastName: Higgins
    Username: g.higgins
    EmailAddress: g.higgins@gmail.com
    OtherAddresses: primary:g.higgins@gmail.com,secondary:g.higgins@outlook.com
    Registered: 是
  - ClassType: 兼职
    FirstName: Robert
    LastName: Miller
    Username: robmil
    EmailAddress: robmil@gmail.com
    OtherAddresses: primary:robmil@gmail.com,secondary:robmil@outlook.com
    Registered: 是
  - ClassType: 灵活
    FirstName: Jeffrey
    LastName: Keller
    Username: jeff.k
    EmailAddress: jeff.k@gmail.com
    OtherAddresses: primary:jeff.k@gmail.com,secondary:jeff.k@outlook.com
    Registered: 是

我该如何创建字典以附加三个新键值,并循环每个用户以产生上述输出?

这是我的当前剧本:

- 主机: 本地主机
  任务:
    - read_csv:
        路径:/var/lib/awx/projects/file/creation.csv
        # 键: FirstName 
        字段名:FirstName,LastName,ClassType
        分隔符: '''
      注册:userdata

    - 调试:
        变量:userdata.list
英文:

*This is an Ansible question *

Here is my list:

  userdata.list:
  - ClassType: Full Time
    FirstName: Grace
    LastName: Higgins
    Username: g.higgins
  - ClassType: Part Time
    FirstName: Robert
    LastName: Miller
    Username: robmil
  - ClassType: Flexi
    FirstName: Jeffrey
    LastName: Keller
    Username: jeff.k

I want to append the following values to the existing list, looping for each user:

EmailAddress: {{ Username }} append to '@gmail.com'
OtherAddresses: primary:{{ Username }}@gmail.com,secondary:{{ Username }}@outlook.com
Registered: Yes

Expected output:

  userdata.list:
  - ClassType: Full Time
    FirstName: Grace
    LastName: Higgins
    Username: g.higgins
    EmailAddress: g.higgins@gmail.com
    OtherAddresses: primary:g.higgins@gmail.com,secondary:g.higgins@outlook.com
    Registered: Yes
  - ClassType: Part Time
    FirstName: Robert
    LastName: Miller
    Username: robmil
    EmailAddress: robmil@gmail.com
    OtherAddresses: primary:robmil@gmail.com,secondary:robmil@outlook.com
    Registered: Yes
  - ClassType: Flexi
    FirstName: Jeffrey
    LastName: Keller
    Username: jeff.k
    EmailAddress: jeff.k@gmail.com
    OtherAddresses: primary:jeff.k@gmail.com,secondary:jeff.k@outlook.com
    Registered: Yes

How do I create the dictionary to append the three new key: values, and loop for each user to produce the above output?

This is my current playbook:

- hosts: localhost
  tasks:
    - read_csv:
        path: /var/lib/awx/projects/file/creation.csv
        # key: FirstName 
        fieldnames: FirstName,LastName,ClassType
        delimiter: ','
      register: userdata

    - debug:
        var: userdata.list

答案1

得分: 1

给定mre数据

  userdata:
    - Username: g.higgins
    - Username: robmil
    - Username: jeff.k

使用Jinja并创建字典update

  update: |
      {% filter from_yaml %}
      {% for user in  userdata|map(attribute='Username') %}
      - EmailAddress: {{ user }}@gmail.com
        OtherAddresses: 'primary:{{ user }}@gmail.com,secondary:{{ user }}@outlook.com'
        Registered: 'Yes'
      {% endfor %}
      {% endfilter %}      

结果如下

  update:
  - EmailAddress: g.higgins@gmail.com
    OtherAddresses: primary:g.higgins@gmail.com,secondary:g.higgins@outlook.com
    Registered: 'Yes'
  - EmailAddress: robmil@gmail.com
    OtherAddresses: primary:robmil@gmail.com,secondary:robmil@outlook.com
    Registered: 'Yes'
  - EmailAddress: jeff.k@gmail.com
    OtherAddresses: primary:jeff.k@gmail.com,secondary:jeff.k@outlook.com
    Registered: 'Yes'

zipcombine列表的项

    - set_fact:
        userdata: "{{ userdata|zip(update)|map('combine') }}"

完整的mre测试剧本示例

shell> cat pb.yml
- hosts: localhost

  vars:

    userdata:
      - Username: g.higgins
      - Username: robmil
      - Username: jeff.k

    update: |
      {% filter from_yaml %}
      {% for user in  userdata|map(attribute='Username') %}
      - EmailAddress: {{ user }}@gmail.com
        OtherAddresses: 'primary:{{ user }}@gmail.com,secondary:{{ user }}@outlook.com'
        Registered: 'Yes'
      {% endfor %}
      {% endfilter %}      

  tasks:

    - debug:
        var: update

    - set_fact:
        userdata: "{{ userdata|zip(update)|map('combine') }}"
    - debug:
        var: userdata

结果如下

shell> ansible-playbook pb.yml

PLAY [localhost] ******************************************************************************

TASK [debug] **********************************************************************************
ok: [localhost] => 
  update:
  - EmailAddress: g.higgins@gmail.com
    OtherAddresses: primary:g.higgins@gmail.com,secondary:g.higgins@outlook.com
    Registered: 'Yes'
  - EmailAddress: robmil@gmail.com
    OtherAddresses: primary:robmil@gmail.com,secondary:robmil@outlook.com
    Registered: 'Yes'
  - EmailAddress: jeff.k@gmail.com
    OtherAddresses: primary:jeff.k@gmail.com,secondary:jeff.k@outlook.com
    Registered: 'Yes'

TASK [set_fact] *******************************************************************************
ok: [localhost]

TASK [debug] **********************************************************************************
ok: [localhost] => 
  userdata:
  - EmailAddress: g.higgins@gmail.com
    OtherAddresses: primary:g.higgins@gmail.com,secondary:g.higgins@outlook.com
    Registered: 'Yes'
    Username: g.higgins
  - EmailAddress: robmil@gmail.com
    OtherAddresses: primary:robmil@gmail.com,secondary:robmil@outlook.com
    Registered: 'Yes'
    Username: robmil
  - EmailAddress: jeff.k@gmail.com
    OtherAddresses: primary:jeff.k@gmail.com,secondary:jeff.k@outlook.com
    Registered: 'Yes'
    Username: jeff.k

PLAY RECAP ************************************************************************************
localhost: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

英文:

Given the mre data

  userdata:
    - Username: g.higgins
    - Username: robmil
    - Username: jeff.k

Use Jinja and create the dictionary update

  update: |
      {% filter from_yaml %}
      {% for user in  userdata|map(attribute='Username') %}
      - EmailAddress: {{ user }}@gmail.com
        OtherAddresses: 'primary:{{ user }}@gmail.com,secondary:{{ user }}@outlook.com'
        Registered: 'Yes'
      {% endfor %}
      {% endfilter %}      

gives

  update:
  - EmailAddress: g.higgins@gmail.com
    OtherAddresses: primary:g.higgins@gmail.com,secondary:g.higgins@outlook.com
    Registered: 'Yes'
  - EmailAddress: robmil@gmail.com
    OtherAddresses: primary:robmil@gmail.com,secondary:robmil@outlook.com
    Registered: 'Yes'
  - EmailAddress: jeff.k@gmail.com
    OtherAddresses: primary:jeff.k@gmail.com,secondary:jeff.k@outlook.com
    Registered: 'Yes'

zip and combine the items of the lists

    - set_fact:
        userdata: "{{ userdata|zip(update)|map('combine') }}"

<hr>

<sup>

Example of a complete mre playbook for testing

shell&gt; cat pb.yml
- hosts: localhost

  vars:

    userdata:
      - Username: g.higgins
      - Username: robmil
      - Username: jeff.k

    update: |
      {% filter from_yaml %}
      {% for user in  userdata|map(attribute=&#39;Username&#39;) %}
      - EmailAddress: {{ user }}@gmail.com
        OtherAddresses: &#39;primary:{{ user }}@gmail.com,secondary:{{ user }}@outlook.com&#39;
        Registered: &#39;Yes&#39;
      {% endfor %}
      {% endfilter %}      

  tasks:

    - debug:
        var: update

    - set_fact:
        userdata: &quot;{{ userdata|zip(update)|map(&#39;combine&#39;) }}&quot;
    - debug:
        var: userdata

gives

shell&gt; ansible-playbook pb.yml

PLAY [localhost] ******************************************************************************

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  update:
  - EmailAddress: g.higgins@gmail.com
    OtherAddresses: primary:g.higgins@gmail.com,secondary:g.higgins@outlook.com
    Registered: &#39;Yes&#39;
  - EmailAddress: robmil@gmail.com
    OtherAddresses: primary:robmil@gmail.com,secondary:robmil@outlook.com
    Registered: &#39;Yes&#39;
  - EmailAddress: jeff.k@gmail.com
    OtherAddresses: primary:jeff.k@gmail.com,secondary:jeff.k@outlook.com
    Registered: &#39;Yes&#39;

TASK [set_fact] *******************************************************************************
ok: [localhost]

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  userdata:
  - EmailAddress: g.higgins@gmail.com
    OtherAddresses: primary:g.higgins@gmail.com,secondary:g.higgins@outlook.com
    Registered: &#39;Yes&#39;
    Username: g.higgins
  - EmailAddress: robmil@gmail.com
    OtherAddresses: primary:robmil@gmail.com,secondary:robmil@outlook.com
    Registered: &#39;Yes&#39;
    Username: robmil
  - EmailAddress: jeff.k@gmail.com
    OtherAddresses: primary:jeff.k@gmail.com,secondary:jeff.k@outlook.com
    Registered: &#39;Yes&#39;
    Username: jeff.k

PLAY RECAP ************************************************************************************
localhost: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

</sup>

huangapple
  • 本文由 发表于 2023年6月8日 12:26:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428617.html
匿名

发表评论

匿名网友

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

确定