I’m relatively new to using ansible and hoping someone can assist with some insight around this.
I’m trying to create a jinja2 template that generates authentication credentials for a database in json format
I’ve defined the variables using the format below (using dictionaries)
vars:
db_roles:
user1:
{ db: “lab2”, privs: “READ,DELETE” }
user2:
{ db: “lab1”, privs: “INSERT,DELETE” }
{ db: “lab2”, privs: “UPDATE” }
The idea is to loop through the users (user1 and user2 in this example) and assign the “privs” on the “db” for each user across multiple databases as needed
To do this, I created a jinja2 template that looks like the below:
{% for item in db_roles %}
{% for dict_item in db_roles[item] %}
The example you gave came pretty close (see result below for user2) but it kind of pointed me in the right direction and I think I’ve fixed it
{ “db” : “admin”, “userName” : “user2”,
“roles” : [
{
“db” : “lab1”,
“role” : “INSERT”
},
{
“db” : “lab1”,
“role” : “DELETE”
}
“roles” : [
{
“db” : “lab2”,
“role” : “UPDATE”
}
}
From the above, I still get 2 separate roles array for a single user but in addition to the above, If I move the dictionary items lookup into the "roles: [ " section, I seem to achieve the desired result i.e.