---
- fail:
    msg: "This role is only supported for Red Hat hosts"
  when: ansible_distribution != 'RedHat'

- name: Install Red Hat Subscription manager
  yum:
    name: subscription-manager
    state: present
  register: result
  until: result is succeeded

- name: Is host already registered?
  command: "subscription-manager version"
  register: rh_subscribed
  changed_when: False

- name: Register host
  redhat_subscription:
    username: "{{ rhsub_user }}"
    password: "{{ rhsub_pass }}"
  register: rh_subscription
  until: rh_subscription is succeeded
  when:
    - "'not registered' in rh_subscribed.stdout"

- fail:
    msg: 'Unable to register host with Red Hat Subscription Manager'
  when:
    - "'not registered' in rh_subscribed.stdout"
    - rh_subscription.failed

- name: Determine if OpenShift Pool Already Attached
  command: "subscription-manager list --consumed --pool-only --matches '*OpenShift*'"
  register: openshift_pool_attached
  changed_when: False
  ignore_errors: yes

- name: Attach to OpenShift Pool
  command: "subscription-manager attach --pool {{ rhsub_pool }}"
  register: openshift_pool_attached
  changed_when: "'Successfully attached a subscription' in openshift_pool_attached.stdout"
  when: rhsub_pool not in openshift_pool_attached.stdout

- include_tasks: satellite.yml
  when:
    - rhsub_server is defined
    - rhsub_server
