Ansible - 从列表

时间:2018-05-13 08:34:16

标签: ansible

我正在尝试关注this线程,但我的输出不是预期的。每个以前的项目都会被添加的新项目覆盖。

我的输入是我加载到我的accounts_list变量中的列表如下:

account:
  - PR_user1
  - PR_user2

输入文件中没有密码。我需要为每个用户帐户创建随机密码,在设置各种服务时使用它们,然后将它们转储到供人类使用的文本文件中。

我遇到的第一个任务是,一旦我将它们读入列表,我想迭代它们,为每个帐户创建密码,然后将其作为键值对存储在字典中。

我尝试了两种技术,将项目添加到现有字典中,使用了结合以及“+”。

我的输入是一个名为'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"}

2 个答案:

答案 0 :(得分:1)

假设变量列表如下(我猜它不是你的情况):

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"
    }
}

但我猜你有类似的东西:

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 

答案 1 :(得分:1)

最干净的解决方案可能是创建自己的ansible过滤器:

  • 在您的ansible目录中创建一个文件夹filter_plugins
  • 创建一个名为to_dict.py的文件。

添加以下内容。

class FilterModule(object):
    def filters(self):
    return {'to_dict': lambda _list: {key: value for key, value in [value.split('=') for value in _list]}}

Ansible vars

account:
  - PR_user1=value1
  - PR_user2=value2

任务:

"{{ account | to_dict }}"