Reputation: 3414
I'm trying to get a list of grants based on database role to re-use in one of the modules. For that the result needs to be a list
name: adding permissions
module:
role: database_role
permissions:
- "schema:USAGE/table1:SELECT/table2:SELECT,UPDATE"
- "another_schema:USAGE/ALL:ALL"
My permissions are defined as variables as follows:
db_roles:
- name: role1
grants:
- schema: schema
permissions:
- table1:SELECT
- table2:SELECT,UPDATE
- schema: another_schema:
permissions:
- ALL:ALL
I have more roles defined as well. This definition of roles means I can add a new permission per row, making it more readable.
Now I'm trying to format this variable to receive something like this:
permissions:
- role1:
- "schema:USAGE/table1:SELECT/table2:SELECT,UPDATE"
- "another_schema:USAGE/ALL:ALL"
- role2:
- "schema:USAGE/ALL:ALL"
But I have no idea how to get to this result.
What I've tried
So far the furthest I got is this but I'm not sure if it's possible to retrieve the values from the dictionary somehow
ok: [localhost] => {
"permissions": {
"role1": {
"schema": "schema:USAGE/table1:SELECT/table2:SELECT",
"another_schema": "another_schema:USAGE/ALL:ALL"
}
}
}
The code that got me there is:
- name: Create privs for users
set_fact:
permissions: "{{ permissions|default( {item.0.name:{}} ) | combine( {item.0.name:{item.1.schema: item.1.schema ~ ':USAGE/' ~ item.1.permissions | join('/')}}, recursive=True) }}"
with_subelements:
- "{{ db_roles }}"
- grants
Upvotes: 1
Views: 59
Reputation: 6685
tough one :) hope i got it right.
picking up where you left off, i added this task, to further process the variable you prepared:
- name: Create privs for users - step 2
set_fact:
permissions_final: "{{ permissions_final|default([]) + [{ item : permissions[item] | dict2items | map(attribute='value') | list }] }}"
with_items:
- "{{ permissions.keys() | list }}"
full code and sample output:
---
- hosts: localhost
gather_facts: false
vars:
db_roles:
- name: role1
grants:
- schema: schema
permissions:
- table1:SELECT
- table2:SELECT,UPDATE
- schema: another_schema
permissions:
- ALL:ALL
- name: role2
grants:
- schema: schema3
permissions:
- table1:SELECT
- table2:SELECT,UPDATE
- schema: another_schema4
permissions:
- ALL:ALL
tasks:
- name: Create privs for users
set_fact:
permissions: "{{ permissions|default( {item.0.name:{}} ) | combine( {item.0.name:{item.1.schema: item.1.schema ~ ':USAGE/' ~ item.1.permissions | join('/')}}, recursive=True) }}"
with_subelements:
- "{{ db_roles }}"
- grants
# - name: print results
# debug:
# var: permissions
- name: Create privs for users - step 2
set_fact:
permissions_final: "{{ permissions_final|default([]) + [{ item : permissions[item] | dict2items | map(attribute='value') | list }] }}"
with_items:
- "{{ permissions.keys() | list }}"
- name: print results
debug:
var: permissions_final
variable produced:
TASK [print results] ***************************************************************************************************************************************************************************************************
ok: [localhost] => {
"permissions_final": [
{
"role1": [
"schema:USAGE/table1:SELECT/table2:SELECT,UPDATE",
"another_schema:USAGE/ALL:ALL"
]
},
{
"role2": [
"schema3:USAGE/table1:SELECT/table2:SELECT,UPDATE",
"another_schema4:USAGE/ALL:ALL"
]
}
]
}
hope this helps!
Upvotes: 1