Thanks Kai.

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.

{% for item in db_roles %}
      ,
       { "db" : "admin", "userName" : "{{ item }}",
          "roles" : [
            {% for dict_item in db_roles[item] %}
            {% for dbpriv in dict_item.privs.split(',') %}
           {
            "db" : "{{ dict_item.db }}",
            "role" : "{{ dbpriv }}"
           }
            {%- if not loop.last %},{% endif %}
            {% endfor %}
            {%- if not loop.last %},{% endif %}
          {% endfor %}
                   ]
       }
   {% endfor %}

Which gives me this:

 ,
       { "db" : "admin", "userName" : "user2",
          "roles" : [


           {
            "db" : "lab1",
            "role" : "INSERT"
           },

           {
            "db" : "lab1",
            "role" : "DELETE"
           }
            ,


           {
            "db" : "lab2",
            "role" : "UPDATE"
           }


                   ]
       }

      ,
       { "db" : "admin", "userName" : "user1",
          "roles" : [


           {
            "db" : "lab2",
            "role" : "READ"
           },

           {
            "db" : "lab2",
            "role" : "DELETE"
           }


                   ]
       }

Thank you :)

On Tuesday, February 20, 2018 at 11:34:14 AM UTC, Kai Stian Olstad wrote:
>
> On 20.02.2018 10:46, 'deewon' via Ansible Project wrote: 
> > vars: 
> >     db_roles: 
> >          user1: 
> >            - { db: "lab2", privs: "READ,DELETE" } 
> >          user2: 
> >            - { db: "lab1", privs: "INSERT,DELETE" } 
> >            - { db: "lab2", privs: "UPDATE" } 
> > 
> > 
> > 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] %} 
> >      , 
> >       { "db" : "admin", "userName" : "{{ item }}", 
> >          "roles" : [ 
> >            {% for dbpriv in dict_item.privs.split(',') %} 
> >           { 
> >            "db" : "{{ dict_item.db }}", 
> >            "role" : "{{ dbpriv }}" 
> >           } 
> >            {% if not loop.last %},{% endif %} 
> >            {% endfor %} 
> >                   ] 
> >       } 
> >          {% endfor %} 
> > 
> >   {% endfor %} 
>
> You need to move your { "db" up one level. 
>
> {% for item in db_roles %} 
>       , 
>        { "db" : "admin", "userName" : "{{ item }}", 
>          {% for dict_item in db_roles[item] %} 
>           "roles" : [ 
>             {% for dbpriv in dict_item.privs.split(',') %} 
>            { 
>             "db" : "{{ dict_item.db }}", 
>             "role" : "{{ dbpriv }}" 
>            } 
>             {%- if not loop.last %},{% endif %} 
>             {% endfor %} 
>           {% endfor %} 
>                    ] 
>        } 
>    {% endfor %} 
>
>
> I also added a "-" to the "if" so it comes after "}" and not on a 
> newline. 
>
>
> -- 
> Kai Stian Olstad 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/af09c469-c551-4de2-b7ef-4cc567da39cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to