---
- block:
  # ##################################
  # # Run config to take in settings #
  # ##################################
  # - name: Run ghe-config-apply for Settings to Take Effect
  #   shell: "nohup ghe-config-apply </dev/null >/dev/null 2>&1 &"
  #   async: 45
  #   poll: 0
  #   args:
  #     executable: "/bin/bash"

  ##################################
  # Run config to take in settings #
  ##################################
  - name: Run Configure for Settings to Take Effect
    uri:
      url: "https://{{ ansible_host }}:8443/setup/api/configure"
      method: POST
      return_content: "yes"
      user: "api_key"
      password: "{{ github_admin_password }}"
      force_basic_auth: "yes"
      validate_certs: "no"
      status_code: 202

  #################################################################
  # Wait for 'ghe-config-apply' to be completed before continuing #
  #################################################################
  - name: Ensure ghe-config-apply is completed
    # https://docs.ansible.com/ansible/uri_module.html
    # yamllint disable-line
    # https://developer.github.com/enterprise/v3/enterprise-admin/management_console/#check-configuration-status
    uri:
      url: "https://{{ ansible_host }}:8443/setup/api/configcheck"
      method: GET
      return_content: "yes"
      user: "api_key"
      password: "{{ github_admin_password }}"
      force_basic_auth: "yes"
      validate_certs: "no"
    register: configcheck
    until: configcheck.status == 200 and configcheck.json.status == "success"
    retries: 100
    delay: 10

  #####################################
  # Remove the files from the machine #
  #####################################
  # Need to remove the license file and settings
  # files that were copied to the ghe server
  - name: Remove temp Files from GHE
    become: true
    file:
      path: "{{ item }}"
      state: absent
    with_items:
      - "/tmp/ghe-license.ghl"
      - "/tmp/settings.json"
      - "/tmp/ldap-settings.json"

  ######################
  # Set the tags block #
  ######################
  tags:
    - github
    - ghe_primary
    - initialize