# Copyright (c) 2019 Dell Inc. or its subsidiaries. All Rights Reserved.
---
- name: Get vault login token
  shell: >
    kubectl get secret {{ ngci_vault_root_token_secret }}
    --namespace={{ ngci_vault_namespace }}
    --output=jsonpath="{['data']['root_token']}" | base64 --decode
  register: ldap_login_token

- name: Define AWX admin user details
  set_fact:
    awx_admin_user: '{ "{{ admin_user }}": { "username": "{{ admin_user }}", "password": "{{ awx_admin_password }}" } }'
  no_log: "{{ enforce_no_log }}"

- name: Retrieve consul pod name
  shell: >
    kubectl get pods
    --namespace={{ ngci_consul_namespace }}
    --selector=release=ngci-secret-storage
    --output=jsonpath='{.items..metadata.name}'
  register: consul_pod_name

- name: Store AWX admin credentials in Vault
  shell: >
    kubectl exec -it -n ngci-consul "{{ consul_pod_name.stdout }}" -c ngci-secret-storage-consul --
    curl -X POST -H X-Vault-Token:{{ ldap_login_token.stdout }} -H 'Content-Type: application/json' {{ngci_vault_internal_service_address}}/v1/{{awx_details_path}} -d '{{ awx_admin_user | to_json }}'
  no_log: "{{ enforce_no_log }}"
  register: vault_add_awx_admin_creds
  until: vault_add_awx_admin_creds.rc == 0
  delay: 30
  retries: 30