Ansible-从列表创建字典 [英] Ansible - Creating dictionary from a list
问题描述
我正在尝试遵循此线程,但是我的输出不是预期的.每个先前的项目都将被添加的新项目覆盖.
i am trying to follow this thread but my output is not what was expected. every previous item is getting overwritten with the new item being added.
我的输入是我正在加载到我的account_list变量中的列表,如下所示:
my input is a list that i am loading into my accounts_list variables is as follows:
account:
- PR_user1
- PR_user2
输入文件中没有密码.我需要为每个用户帐户创建随机密码,在设置各种服务时使用它们,然后将它们转储到文本文件中供人使用.
There are no passwords in the input file. i need to create random passwords for each of the user account, use them in setting up various services, and then dump them into a text file for human use.
我遇到的第一个任务是,一旦将它们读入列表中,我想遍历它们,为每个帐户创建密码,然后将其作为键值对存储在字典中.
my first task on which i am stuck is that once i have read them into a list, i want to iterate over them, create password for each account, and then store it inside a dictionary as key value pairs.
我已经尝试过使用合并和'+'两种提到的将项目添加到现有字典中的技术.
i have tried both of the techniques mentioned to add an item to an existing dictionary, using combine as well as '+'.
我的输入是一个名为帐户"的简单列表.
my input is a simple list called 'accounts'.
- set_fact:
# domain_accounts: "{{ domain_accounts|default({}) | combine({item|trim: lookup(...)} ) }}"
domain_accounts: "{{ domain_accounts|default([]) + [{item|trim:lookup('...)}] }}"
with_items: "{{account_list.accounts}}"
我的输出如下:
TASK [set account passwords]
******************************************************************
ok: [localhost] => (item=PR_user1) => {"ansible_facts": {"domain_accounts": [{"PR_user1": "u]oT,cU{"}]}, "changed": false, "item": "PR_user1"}
ok: [localhost] => (item=PR_user2) => {"ansible_facts": {"domain_accounts": [{"PR_user2": "b>npKZdi"}]}, "changed": false, "item": "PR_user2"}
推荐答案
假设变量列表如下(我想这不是您的情况):
Supposing that the variable lists is as follows (That I guess it's not your case):
accounts:
- user: PR_user1
password: "u]oT,cU{"
- user: PR_user2
password: "b>npKZdi"
使用:
- name: debug
debug:
var: accounts
和
- name: Populate dict
set_fact:
domain_accounts: "{{ domain_accounts|default({}) | combine( {item.user: item.password} ) }}"
with_items:
- "{{ accounts }}"
加号:
- name: debug
debug:
var: domain_accounts
您将获得:
ok: [localhost] => {
"domain_accounts": {
"PR_user1": "u]oT,cU{",
"PR_user2": "b>npKZdi"
}
}
但是我猜你有类似的东西:
But I guess you have something like:
accounts:
- PR_user1: "u]oT,cU{"
- PR_user2: "b>npKZdi"
所以:
- name: Create dict
set_fact:
domain_accounts: "{{ domain_accounts|default({}) | combine(item.1) }}"
with_indexed_items: "{{accounts}}"
- name: debug
debug:
var: domain_accounts
会得到:
ok: [localhost] => {
"domain_accounts": {
"PR_user1": "u]oT,cU{",
"PR_user2": "b>npKZdi"
}
}
这是所有内容供您参考:
This is the whole play for your reference:
---
- hosts: localhost
gather_facts: False
vars:
accounts:
- user: PR_user1
password: "u]oT,cU{"
- user: PR_user2
password: "b>npKZdi"
accountslist:
- PR_user1: "u]oT,cU{"
- PR_user2: "b>npKZdi"
tasks:
- name: Debug Accounts
debug:
var: accounts
- name: Debug Accounts List
debug:
var: accountslist
- name: Populate dict
set_fact:
domain_accounts: "{{ domain_accounts|default({}) | combine( {item.user: item.password} ) }}"
with_items:
- "{{ accounts }}"
- name: Debug Domain Accounts
debug:
var: domain_accounts
- name: indexed loop demo
debug:
msg: "{{ item.1 }}"
with_indexed_items: "{{accountslist}}"
- name: Create Local Dict
set_fact:
local_accounts: "{{ local_accounts|default({}) | combine(item.1) }}"
with_indexed_items: "{{accountslist}}"
- name: Debug Local Accounts
debug:
var: local_accounts
结果:
PLAY [localhost] *********************************************************************************************************************
TASK [Debug Accounts] ****************************************************************************************************************
ok: [localhost] => {
"accounts": [
{
"password": "u]oT,cU{",
"user": "PR_user1"
},
{
"password": "b>npKZdi",
"user": "PR_user2"
}
]
}
TASK [Debug Accounts List] ***********************************************************************************************************
ok: [localhost] => {
"accountslist": [
{
"PR_user1": "u]oT,cU{"
},
{
"PR_user2": "b>npKZdi"
}
]
}
TASK [Populate dict] *****************************************************************************************************************
ok: [localhost] => (item={u'password': u'u]oT,cU{', u'user': u'PR_user1'})
ok: [localhost] => (item={u'password': u'b>npKZdi', u'user': u'PR_user2'})
TASK [Debug Domain Accounts] *********************************************************************************************************
ok: [localhost] => {
"domain_accounts": {
"PR_user1": "u]oT,cU{",
"PR_user2": "b>npKZdi"
}
}
TASK [indexed loop demo] *************************************************************************************************************
ok: [localhost] => (item=None) => {
"msg": {
"PR_user1": "u]oT,cU{"
}
}
ok: [localhost] => (item=None) => {
"msg": {
"PR_user2": "b>npKZdi"
}
}
TASK [Create Local Dict] *************************************************************************************************************
ok: [localhost] => (item=(0, {u'PR_user1': u'u]oT,cU{'}))
ok: [localhost] => (item=(1, {u'PR_user2': u'b>npKZdi'}))
TASK [Debug Local Accounts] **********************************************************************************************************
ok: [localhost] => {
"local_accounts": {
"PR_user1": "u]oT,cU{",
"PR_user2": "b>npKZdi"
}
}
PLAY RECAP ***************************************************************************************************************************
localhost : ok=7 changed=0 unreachable=0 failed=0
这篇关于Ansible-从列表创建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!