1

I am at a bit of a loss on how to transform a list of dicts into a json structure that is needed to be sent as a variable into another module

To start with, I have a list of dict that describes all the information about the 'teams' I am creating (in awx / Tower). This list details a bunch of information about my teams. Not all need to be transformed into the JSON structure for this next update I need to do.

awxTeamsDefinition:
  - { teamname: Team1, ldapgroupname: tower-team-1, description: This is tower team 1, organization: orgA }
  - { teamname: Team2, ldapgroupname: tower-team-2, description: This is tower team 2, organization: orgA }
  - { teamname: Team3, ldapgroupname: tower-team-3, description: This is tower team 3, organization: orgB }  
  - { teamname: Team4, ldapgroupname: tower-team-4, description: This is tower team 4, organization: orgB }

Next, I need to extract some information and stick it in a JSON structure that looks like this:

{
 "Team1": {
  "remove": true,
  "organization": "orgA",
  "users": "cn=tower-team-1,cn=groups,cn=accounts,dc=domain,dc=com"
 },
 "Team2": {
  "remove": true,
  "organization": "orgA",
  "users": "cn=tower-team-2,cn=groups,cn=accounts,dc=domain,dc=com"
 },
 "Team3": {
  "remove": true,
  "organization": "orgB",
  "users": "cn=tower-team-3,cn=groups,cn=accounts,dc=domain,dc=com"
 },
 "Team4": {
  "remove": true,
  "organization": "orgB",
  "users": "cn=tower-team-4,cn=groups,cn=accounts,dc=domain,dc=com"
 }
}

I need this JSON structure in a variable that I can pass into another module.

This is simply beyond my ansible / jinja trickery ability. I don't know how to extract from a dict and promote that the the index of each json entry (in this case, 'teamname'), get rid of the 'description', and use 'ldapgroupname' to replace into the cn structure. Any help would be appreciated. Thanks!

1 Answer 1

1

The tasks below

   - set_fact:
        data1: "{{ data1|default({})|
                   combine({item.teamname:
                             {'remove': true,
                              'organization': item.organization,
                              'users': 'cn=' ~ item.ldapgroupname ~ users_append}}) }}"
      loop: "{{ awxTeamsDefinition }}"
      vars:
        users_append: ",cn=groups,cn=accounts,dc=domain,dc=com"
    - debug:
        var: data1|to_nice_json

gives

  data1|to_nice_json: |-
    {
        "Team1": {
            "organization": "orgA",
            "remove": true,
            "users": "cn=tower-team-1,cn=groups,cn=accounts,dc=domain,dc=com"
        },
        "Team2": {
            "organization": "orgA",
            "remove": true,
            "users": "cn=tower-team-2,cn=groups,cn=accounts,dc=domain,dc=com"
        },
        "Team3": {
            "organization": "orgB",
            "remove": true,
            "users": "cn=tower-team-3,cn=groups,cn=accounts,dc=domain,dc=com"
        },
        "Team4": {
            "organization": "orgB",
            "remove": true,
            "users": "cn=tower-team-4,cn=groups,cn=accounts,dc=domain,dc=com"
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.