简体   繁体   中英

Using ansible to parse and loop over a json file to post back to a curl

I am trying to parse json using ansible so i can then post it back to an API using a curl command. I think a with_together is probably the best way of doing it but i cant seem to find the right json path for it even though it is pretty simple json. So the json is here -

{
                "deploy-account": [{
                                "role": "roles/iam.serviceAccountTokenCreator",
                                "members": [
                                        "group:group1@google.com",
                                        "group:group2@google.com"
                                ]
                        }
                ],
                "data_account": [{
                                "role": "roles/iam.serviceAccountTokenCreator",
                                "members": [
                                        "group:group1@google.com"
                                ]
                        },
                        {
                                "role": "roles/iam.serviceAccountKeyAdmin",
                                "members": [
                                        "group:group1@google.com"
                                ]
                        }
                ],
                "dataproc_account": [{
                                "role": "roles/iam.serviceAccountTokenCreator",
                                "members": [
                                        "group:group1@google.com"
                                ]
                        },
                        {
                                "role": "roles/iam.serviceAccountKeyAdmin",
                                "members": [
                                        "group:group3@google.com"
                                ]
                        }
                ]
                
        }

The idea is to take the account name as one item, and then take everything under that account name, regardless of how many roles or groups are there, and add that into the second item for pushing to the API. But when i loop over it, it takes only a single role from each account and applies it so my loop must not be right but i dont see how. yml is like this

      tasks:
      - name: access fact
        set_fact:
        access_auths: "{{ lookup('file', 'stack.json') }}"
     - name: Curl
       uri:
         url: "https://google.com/{{ item.0 }}"
         body:
           policy: 
             bindings: "{{ item.1 }}"
         body_format: json
         method: POST
         headers:
           Content-Type: "application/json"
      with_together: 
        - "{{ access_auths }}"
        - "{{ access_auths | json_query ('*') }}"

So the end goal is there will be many json files, with a different amount of accounts and the json will have different amounts of sub-elements and it needs to handle that, The idea in this case is

item.0 - deploy-account
item.1 - [{"role": "roles/iam.serviceAccountTokenCreator","members": [ "group:group1@google.com","group:group2@google.com"]

next loop
item.0 - data_account
item.1 - [{"role": "roles/iam.serviceAccountTokenCreator","members": ["group:group1@google.com"]},{"role": "roles/iam.serviceAccountKeyAdmin","members": ["group:group1@google.com"]

As i said above though, the problem i get when i run this, is that if an account has 2 roles assigned, it loops that one twice and overwrites the first role, is there a better way of accessing the json instead of my query? And is a with_together the best way?

I ended up using with_dict since it just wanted keys and values, and its worked

loop: "{{ lookup('dict', access_auths) }}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM