---
- block:
    - name: "Obtain currently enabled repos"
      shell: 'subscription-manager repos --list-enabled | sed -ne "s/^Repo ID:[^a-zA-Z0-9]*\(.*\)/\1/p"'
      register: enabled_repos

    - name: "Disable repositories that should not be enabled"
      shell: "subscription-manager repos --disable={{ item }}"
      with_items:
        - "{{ enabled_repos.stdout_lines | difference(openshift_required_repos) }}"

    - name: "Enable specified repositories not already enabled"
      command: "subscription-manager repos --enable={{ item }}"
      with_items:
        - "{{ openshift_required_repos | difference(enabled_repos.stdout_lines) }}"
      register: subscribe_repos
      until: subscribe_repos | succeeded

    - name: check to see if rhui exists
      stat:
        path: /etc/yum.repos.d/redhat-rhui.repo
      register: rhui

    - name: check to see if rhui client exists
      stat:
        path: /etc/yum.repos.d/redhat-rhui-client-config.repo
      register: client

    - name: disable rhui repo
      replace:
        dest: /etc/yum.repos.d/redhat-rhui.repo
        regexp: 'enabled=1'
        replace: 'enabled=0'
      when: rhui.stat.exists

    - name: disable rhui client repos
      replace:
        dest: /etc/yum.repos.d/redhat-rhui-client-config.repo
        regexp: 'enabled=1'
        replace: 'enabled=0'
      when: client.stat.exists

  when: ansible_distribution == "RedHat"
