Nested loops in ansible while creating k8s secrets

Viewed 61

I am trying to create k8s secrets with the storing username and secret in results module of ansible but i created one loop for the name and namespace section and i am trying to create one more loop from the json results output

variables:

project_namespaces:
  - projectName: helm
    Namespaces: 
      - default
      - core

robot_result.results

    "robot_result.results | json_query('[].json')": [
        {
            "creation_time": "2022-03-21T10:29:18.331Z",
            "expires_at": -1,
            "id": 75,
            "username": "robot@helm-robot",
            "secret": "YzDDEtJcqYoBL2soZHfTqZxvhIfGKURT"
        },
        {
            "creation_time": "2022-03-21T10:29:22.129Z",
            "expires_at": -1,
            "id": 76,
            "username": "robot@proxy-robot",
            "secret": "7imXCVAGHV91AkeN7LAhWxQHabmYDRmg"
        }
    ]
}

Tasks:

- name: Create secrets
  k8s: 
    state: present
    definition:
      apiVersion: v1
      kind: Secret
      metadata:
        name: "{{ item.0.projectName }}"
        namespace: "{{ item.1 }}"
      stringData:
        password: "{{ project.secret }}"
        username: "{{ project.name }}"
      type: Opaque
  vars:
    project: "{{ robot_result.results | json_query('[*].json') | first }}"  
  loop: "{{ project_namespaces | subelements('Namespaces') }}"
  loop_control:
    label: "{{ item.0.projectName }}"

when i tried to execute this tasks, it's creating the secrets but it's creating every secret with the same name and secret, because i am using project: "{{ robot_result.results | json_query('[*].json') | first }}" it's printing only first value of the results every time.

I am not much aware of this ansible to create two loops in this tasks. can anyone please help me on this to achieve this..i am posting the results and output of the robot account.

I am getting the output like

name: "helm" , namespace: "default", username: "robot@helm-robot", secret: "YzDDEtJcqYoBL2soZHfTqZxvhIfGKURT"
name: "helm" , namespace: "core", username: "robot@helm-robot", secret: "YzDDEtJcqYoBL2soZHfTqZxvhIfGKURT" 

but i am looking the output should be

name: "helm" , namespace: "default", username: "robot@helm-robot", secret: "YzDDEtJcqYoBL2soZHfTqZxvhIfGKURT"
name: "helm" , namespace: "core", username: "robot@proxy-robot", secret: "7imXCVAGHV91AkeN7LAhWxQHabmYDRmg" 

I was getting error like

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'secret'\n\nThe error appears to be in '/home/ubuntu/konvoy/ansible/roles/harbor/tasks/main.yml': line 47, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Create secrets\n  ^ here\n"}

Thanks in advance.

robot_result:

ok: [harbor_stg1_dkp1] => {
    "robot_result": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "content_type": "application/json",
                "cookies": {
                    "sid": "557b377ba2bbe3f054d68bd56b0e10ef"
                },
                "invocation": {
                    "module_args": {
                        "attributes": null,
                        "body": {
                            "description": "pull artifacts for helm",
                            "disable": true,
                            "duration": -1,
                            "level": "system",
                            "name": "helm-robot",
                            "permissions": [
                                {
                                    "access": [
                                        {
                                            "action": "pull",
                                            "resource": "repository"
                                        }
                                    ],
                                    "kind": "project",
                                    "namespace": "helm"
                                }
                            ]
                        },
                        "body_format": "json",
                        "headers": {
                            "Accept": "application/json",
                            "Authorization": "Basic ",
                            "Content-Type": "application/json"
                        },
                        "http_agent": "ansible-httpget",
                        "status_code": [
                            201
                        ],
                        "timeout": 30,
                        "unix_socket": null,
                        "validate_certs": false
                    }
                },
                "item": {
                    "name": "helm-robot",
                    "projectName": "helm"
                },
                "json": {
                    "creation_time": "2022-03-21T10:05:49.248Z",
                    "expires_at": -1,
                    "id": 67,
                    "name": "robot@helm-robot",
                    "secret": "Q8mjthgRJFmscjfmqW1QzXEyKjmLEPQm"
                },
                "x_envoy_upstream_service_time": "18",
                "x_request_id": "496faaa6-bdc4-4e83-890a-2c577576f16b"
            },
            {
                "ansible_loop_var": "item",
                "content_type": "application/json",
                "cookies": {
                    "sid": "6275d7bfe74e71db0a3947f4beb1e159"
                },
                "cookies_string": "sid=6275d7bfe74e71db0a3947f4beb1e159",
                "date": "Mon, 21 Mar 2022 10:05:56 GMT",
                "elapsed": 1,
                "failed": false,
                "failed_when_result": false,
                "invocation": {
                    "module_args": {
                        "attributes": null,
                        "body": {
                            "description": "pull artifacts for proxy",
                            "disable": true,
                            "duration": -1,
                            "level": "system",
                            "name": "proxy-robot",
                            "permissions": [
                                {
                                    "access": [
                                        {
                                            "action": "pull",
                                            "resource": "repository"
                                        }
                                    ],
                                    "kind": "project",
                                    "namespace": "proxy"
                                }
                            ]
                        },
                        "body_format": "json",
                        "group": null,
                        "headers": {
                            "Accept": "application/json",
                            "Authorization": "Basic ",
                            "Content-Type": "application/json"
                        },
                        "http_agent": "ansible-httpget",
                        "method": "POST"
                        ],
                        "timeout": 30,
                        "unix_socket": null,
                        "validate_certs": false
                    }
                },
                "item": {
                    "name": "proxy-robot",
                    "projectName": "proxy"
                },
                "json": {
                    "creation_time": "2022-03-21T10:05:56.807Z",
                    "expires_at": -1,
                    "id": 68,
                    "name": "robot@proxy-robot",
                    "secret": "slPnm8Zkp0OGBLec6tTcPuPITgOU2PAn"
                },
                "msg": "OK (144 bytes)",
                "x_envoy_upstream_service_time": "15",
                "x_request_id": "93478b05-897b-4df9-abb4-e07e03723af0"
            }

If i add more than one variable

project_namespaces:
  - projectName: helm
    Namespaces: 
      - default
      - core
  - projectName: proxy
    Namespaces: 
      - default
      - core

based on the variables the expected output should be looks :

ok: [localhost] => (item=helm) => {
    "msg": "name: helm, namespace: default, password: Q8mjthgRJFmscjfmqW1QzXEyKjmLEPQm, username: robot@helm-robot"
}
ok: [localhost] => (item=helm) => {
    "msg": "name: helm, namespace: core, password: Q8mjthgRJFmscjfmqW1QzXEyKjmLEPQm, username: robot@helm-robot"
}
ok: [localhost] => (item=proxy) => {
    "msg": "name: proxy, namespace: default, password: slPnm8Zkp0OGBLec6tTcPuPITgOU2PAn, username: robot@proxy-robot"
}
ok: [localhost] => (item=proxy) => {
    "msg": "name: proxy, namespace: core, password: slPnm8Zkp0OGBLec6tTcPuPITgOU2PAn, username: robot@proxy-robot"
}
1 Answers

without knowing the exact content of your result, you could try that:

- name: Reproduce issue
  hosts: localhost
  gather_facts: no
  vars:
    project_namespaces:
      - projectName: helm
        Namespaces: 
          - default
          - core
    robot_result:
      results:
          - json:
              creation_time: '2022-03-21T10:29:18.331Z'
              expires_at: -1
              id: 75
              username: robot@helm-robot
              secret: YzDDEtJcqYoBL2soZHfTqZxvhIfGKURT
          - json:
              creation_time: '2022-03-21T10:29:22.129Z'
              expires_at: -1
              id: 76
              username: robot@proxy-robot
              secret: 7imXCVAGHV91AkeN7LAhWxQHabmYDRmg

  tasks:
    - debug: 
        msg: "name: {{ item.0.projectName }}, namespace: {{ item.1 }}, password: {{ project.secret }}, username: {{ project.username }}"  #{{ ansible_loop.index0 }}"
      vars:
        project: "{{ (robot_result.results | json_query('[*].json'))[ansible_loop.index0] }}"         
      loop: "{{ project_namespaces | subelements('Namespaces') }}"
      loop_control:
        extended: yes
        label: "{{ item.0.projectName }}"

result:

ok: [localhost] => (item=helm) => {
    "msg": "name: helm, namespace: default, password: YzDDEtJcqYoBL2soZHfTqZxvhIfGKURT, username: robot@helm-robot"
}
ok: [localhost] => (item=helm) => {
    "msg": "name: helm, namespace: core, password: 7imXCVAGHV91AkeN7LAhWxQHabmYDRmg, username: robot@proxy-robot"
}
Related