---
- name: "Subscription Manifest"
  ansible.builtin.include_role:
    name: theforeman.foreman.manifest

- name: "Enable RHEL7 repository"
  ansible.builtin.include_role:
    name: theforeman.foreman.repositories
  vars:
    foreman_products:
      - name: Red Hat Enterprise Linux Server
        repository_sets:
          - name: Red Hat Enterprise Linux 7 Server (RPMs)
            basearch: x86_64
            releasever: 7Server
  when: foreman_content_rhel_enable_rhel7

- name: "Enable RHEL8 repositories"
  ansible.builtin.include_role:
    name: theforeman.foreman.repositories
  vars:
    foreman_products:
      - name: Red Hat Enterprise Linux for x86_64
        repository_sets:
          - name: Red Hat Enterprise Linux 8 for x86_64 - BaseOS (RPMs)
            releasever: "{{ foreman_content_rhel_rhel8_releasever }}"
          - name: Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs)
            releasever: "{{ foreman_content_rhel_rhel8_releasever }}"
  when: foreman_content_rhel_enable_rhel8

- name: "Sync RHEL7 repository"
  theforeman.foreman.repository_sync:
    username: "{{ foreman_username }}"
    password: "{{ foreman_password }}"
    organization: "{{ foreman_organization }}"
    server_url: "{{ foreman_server_url }}"
    validate_certs: "{{ foreman_validate_certs }}"
    product: Red Hat Enterprise Linux Server
  async: 14400
  poll: 0
  register: __foreman_rhel7_sync
  when: foreman_content_rhel_enable_rhel7 and foreman_content_rhel_sync_now

- name: "Sync RHEL8 repositories"
  theforeman.foreman.repository_sync:
    username: "{{ foreman_username }}"
    password: "{{ foreman_password }}"
    organization: "{{ foreman_organization }}"
    server_url: "{{ foreman_server_url }}"
    validate_certs: "{{ foreman_validate_certs }}"
    product: Red Hat Enterprise Linux for x86_64
  async: 14400
  poll: 0
  register: __foreman_rhel8_sync
  when: foreman_content_rhel_enable_rhel8 and foreman_content_rhel_sync_now

- name: "Create Sync Plan"
  ansible.builtin.include_role:
    name: theforeman.foreman.sync_plans
  vars:
    foreman_sync_plans:
      - name: "{{ foreman_sync_plan_name | default('RHEL Sync Plan') }}"
        interval: "{{ foreman_sync_plan_interval | default('daily') }}"
        cron_expression: "{{ foreman_sync_plan_cron_expression | default(omit) }}"
        sync_date: "{{ foreman_sync_plan_sync_date | default('2020-01-01 00:00:00 UTC') }}"
        products:
          "{{ [foreman_content_rhel_enable_rhel7 | ternary('Red Hat Enterprise Linux Server', ''),
          foreman_content_rhel_enable_rhel8 | ternary('Red Hat Enterprise Linux for x86_64', '')]
          | select() | list }}"

- name: "Create Activation Key"
  ansible.builtin.include_role:
    name: theforeman.foreman.activation_keys
  vars:
    foreman_activation_keys:
      - name: "{{ foreman_activation_key_name | default('base_rhel_key') }}"
        description: "Generated by ansible role theforeman.foreman.content_rhel"

- name: "Wait for RHEL7 sync completion"  # noqa: args[module]
  ansible.builtin.async_status:
    jid: "{{ __foreman_rhel7_sync.ansible_job_id }}"
  register: __foreman_rhel7_job_result
  until: __foreman_rhel7_job_result.finished
  retries: 99999
  delay: 10
  when: foreman_content_rhel_enable_rhel7 and foreman_content_rhel_sync_now and foreman_content_rhel_wait_for_syncs

- name: "Wait for RHEL8 sync completion"  # noqa: args[module]
  ansible.builtin.async_status:
    jid: "{{ __foreman_rhel8_sync.ansible_job_id }}"
  register: __foreman_rhel8_job_result
  until: __foreman_rhel8_job_result.finished
  retries: 99999
  delay: 10
  when: foreman_content_rhel_enable_rhel8 and foreman_content_rhel_sync_now and foreman_content_rhel_wait_for_syncs
