Ansible - 从列表创建字典 [英] Ansible - Creating dictionary from a list

查看:23
本文介绍了Ansible - 从列表创建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循this 线程但我的输出不是预期的.之前的每一项都会被添加的新项覆盖.

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.

我的输入是我加载到我的accounts_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"
    }
}

这是全剧供大家参考:

---
- 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆