---
- name: Create temporary directory for admin kubeconfig
  command: mktemp -u /tmp/openshift-ansible-XXXXXXX.kubeconfig
  register: nuage_tmp_conf_mktemp
  changed_when: False

- set_fact:
    nuage_tmp_conf: "{{ nuage_tmp_conf_mktemp.stdout }}"

- name: Copy Configuration to temporary conf
  command: >
    cp {{ openshift.common.config_base }}/master/admin.kubeconfig {{nuage_tmp_conf}}
  changed_when: false

- name: Create Admin Service Account
  shell: >
    echo {{ nuage_service_account_config | to_json | quote }} |
    {{ openshift.common.client_binary }} create
    -n default
    --config={{nuage_tmp_conf}}
    -f -
  register: osnuage_create_service_account
  failed_when: "'already exists' not in osnuage_create_service_account.stderr and osnuage_create_service_account.rc != 0"
  changed_when: osnuage_create_service_account.rc == 0

- name: Configure role/user permissions
  command: >
    {{ openshift.common.client_binary }} adm {{item}}
    --config={{nuage_tmp_conf}}
  with_items: "{{nuage_tasks}}"
  register: osnuage_perm_task
  failed_when: "'already exists' not in osnuage_perm_task.stderr and osnuage_perm_task.rc != 0"
  changed_when: osnuage_perm_task.rc == 0

- name: Generate the node client config
  command: >
    {{ openshift.common.client_binary }} adm create-api-client-config
      --certificate-authority={{ openshift_master_ca_cert }}
      --client-dir={{ cert_output_dir }}
      --master={{ openshift.master.api_url }}
      --public-master={{ openshift.master.api_url }}
      --signer-cert={{ openshift_master_ca_cert }}
      --signer-key={{ openshift_master_ca_key }}
      --signer-serial={{ openshift_master_ca_serial }}
      --basename='nuage'
      --user={{ nuage_service_account }}

- name: Clean temporary configuration file
  command: >
    rm -f {{nuage_tmp_conf}}
  changed_when: false
