---
- block:
  - name: register host
    include_role:
      name: rhsm-subscription

  - name: enable repos
    include_role:
      name: rhsm-repos

  - name: install centos-release-openshift-origin repo
    package:
      name: centos-release-openshift-origin
      state: present
    when: openshift_deployment_type == 'origin'

  - name: check subscription. if this task fails, please check your username, password and pool id
    command: yum list atomic-openshift-utils
    changed_when: false

  - block:
    - name: add google repo
      yum_repository:
        name: google-cloud-compute
        description: Google Cloud Compute
        baseurl: https://packages.cloud.google.com/yum/repos/google-cloud-compute-el7-x86_64
        enabled: true
        gpgcheck: true
        repo_gpgcheck: true
        gpgkey: "https://packages.cloud.google.com/yum/doc/yum-key.gpg\n https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg"

    - name: remove irqbalance package
      package:
        name: irqbalance
        state: absent
    when: openshift_deployment_type == 'openshift-enterprise'

  - name: install PyYAML for update-instances
    package:
      name: PyYAML
      state: present

  - name: update instance
    include_role:
      name: update-instances

  - name: install required packages
    package:
      name: '{{ item }}'
      state: present
    with_items:
    - google-compute-engine
    - cloud-init
    - docker

  - name: add disk_setup to cloud_config_modules
    lineinfile:
      dest: /etc/cloud/cloud.cfg
      insertafter: '^cloud_config_modules:$'
      line: ' - disk_setup'
      state: present

  - name: clear yum cache
    command: yum clean all

  - name: unregister the system
    include_role:
      name: rhsm-unregister

  rescue:
  - name: unregister the system
    include_role:
      name: rhsm-unregister

  - name: delete temp instance
    include_role:
      name: deployment-delete
    vars:
      deployment_name: tmp-instance
    delegate_to: localhost

  - name: delete temp instance disk
    gce_pd:
      name: '{{ prefix }}-tmp-instance'
      zone: '{{ gcloud_zone }}'
      state: absent
    delegate_to: localhost

  - name: delete local ssh config for temp instance
    include_role:
      name: ssh-config-tmp-instance-delete
    delegate_to: localhost

  - name: fail after cleanup
    fail:
      msg: Failed to create gold image. Please look for an error in the output above.
