英文:
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'
zip和combine列表的项
- 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> 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
gives
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
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论