我有一些词典:
users_to_add:
- username: Harley Quinn
login: 90987264
group: mininform
group_desc: "Some random data for 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 for description"
personal_data:
- first_name: Vasya
last_name: Pupkin
mail: vasya@gmail.com
telnum: +7970000000
jobtitle: minister
我的ansible模块只适用于列表:
- 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 }}"
此模块需要“user”参数下的用户列表,创建组并向其添加用户。我想像这样映射字典“users_to_add”:
(使用users_to_add dict)
{group_name: ["user1", "user2"...]}
并将此新dict提供给模块。
如何使用ansible
完成此操作?
我知道我应该首先使用
set_fact:
new_dict:
with_items: {{ users_to_add }}
...
然后将这个新的dict提供给模块,其中key是要创建的组名和值 - 要添加到组的用户列表,但我无法理解如何使用ansible
完成此操作...
答案 0 :(得分:0)
试试这个:
- 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 }}"