Reputation: 550
I have the dictionary:
sshd_additional_user_cfg:
- name: root
authorized_keys:
- key2
- key3
- name: user1
authorized_keys:
- key1
How can I select the objects of that dictionary where the name is root, so that I get
- name: root
authorized_keys:
- key2
- key3
Upvotes: 1
Views: 233
Reputation: 68034
Q: "How can I select the objects of that dictionary where the name is root?"
A: Use filter selectattr. For example
- set_fact:
selected_users: "{{ sshd_additional_user_cfg|
selectattr('name', 'eq', 'root')|
list }}"
- debug:
var: selected_users
gives
selected_users:
- authorized_keys:
- key2
- key3
name: root
Upvotes: 2
Reputation: 1954
You can create a new var with all elements that match with name=='root'
tasks:
- set_fact:
my_new_var: "{{ my_new_var | default([]) + [item] }}"
when: item.name == 'root'
with_items: "{{ sshd_additional_user_cfg }}"
- debug: var=my_new_var
Upvotes: 0