python - Gather dict from another dict in Ansible -


i have dict:

users_to_add:     - username: harley quinn       login: 90987264       group: mininform       group_desc: "some random data description"       personal_data:         - first_name: harley           last_name: quinn           mail: harley@gmail.com           telnum: +7777777777           jobtitle: minister     - username: vasya pupkin       login: 77777777       group: mininform       group_desc: "some random data description"       personal_data:         - first_name: vasya           last_name: pupkin           mail: vasya@gmail.com           telnum: +7970000000           jobtitle: minister 

and have ansible module works lists:

- name: create needed groups     ipa_group:       state: present       name: "{{ item.group }}"       description: "{{ item.group_desc }}"       ipa_host: "{{ global_host }}"       ipa_user: "{{ global_user }}"       ipa_pass: "{{ global_pass }}"       validate_certs: no       user: []     with_items:       - "{{ users_to_add }}" 

this module wants list of users under "user" argument, creating group , adding users it. want map dict "users_to_add" this: (using users_to_add dict) {group_name: ["user1", "user2"...]} , feed new dict module. how can accomplish ansible? know first should use

set_fact:     new_dict:  with_items: {{ users_to_add }} 

...

and feed new dict module, key group name create , value - list of users add group, can't understand how accomplish ansible...

try this:

  - name: create needed groups     ipa_group:       state: present       name: "{{ item }}"       description: "{{ users_to_add | selectattr('group','equalto',item) | map(attribute='group_desc') | list | first }}"       ipa_host: "{{ global_host }}"       ipa_user: "{{ global_user }}"       ipa_pass: "{{ global_pass }}"       validate_certs: no       user: "{{ users_to_add | selectattr('group','equalto',item) | map(attribute='username') | list }}"     with_items: "{{ users_to_add | map(attribute='group') | list | unique }}" 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -