- block:
    - name: Install foo-1.0-1
      yum:
        name: foo-1.0-1
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-1')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Install foo-1.0-1 again
      yum:
        name: foo-1.0-1
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "not yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-1')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Install foo-1:1.0-2
      yum:
        name: "foo-1:1.0-2.{{ ansible_architecture }}"
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-2')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"

    - name: Remove foo
      yum:
        name: foo
        state: absent
    # ============================================================================
    - name: Install 1:foo-1.0-2
      yum:
        name: "1:foo-1.0-2.{{ ansible_architecture }}"
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-2')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Install foo-1.0-2 again
      yum:
        name: foo-1.0-2
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "not yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-2')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Update to the latest foo
      yum:
        name: foo
        state: latest
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.1-1')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Install foo-1.0-1 from a file (higher version is already installed)
      yum:
        name: "{{ repodir }}/foo-1.0-1.{{ ansible_architecture }}.rpm"
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "not yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.1-1')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"

    - name: Remove foo
      yum:
        name: foo
        state: absent
    # ============================================================================
    - name: Install foo-1.0-1 from a file
      yum:
        name: "{{ repodir }}/foo-1.0-1.{{ ansible_architecture }}.rpm"
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-1')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Install foo-1.0-1 from a file again
      yum:
        name: "{{ repodir }}/foo-1.0-1.{{ ansible_architecture }}.rpm"
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "not yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-1')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Install foo-1.0-2 from a file
      yum:
        name: "{{ repodir }}/foo-1.0-2.{{ ansible_architecture }}.rpm"
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-2')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Install foo-1.0-2 from a file again
      yum:
        name: "{{ repodir }}/foo-1.0-2.{{ ansible_architecture }}.rpm"
        state: present
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "not yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-2')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Try to downgrade foo without allow_downgrade being set
      yum:
        name: foo-1.0-1
        state: present
        allow_downgrade: no
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "not yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-2')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Downgrade foo
      yum:
        name: foo-1.0-1
        state: present
        allow_downgrade: yes
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-1')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Update foo with update_only set
      yum:
        name: foo
        state: latest
        update_only: yes
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.1-1')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"

    - name: Remove foo
      yum:
        name: foo
        state: absent
    # ============================================================================
    - name: Try to update foo which is not installed, update_only is set
      yum:
        name: foo
        state: latest
        update_only: yes
      register: yum_result
      ignore_errors: yes

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result
      ignore_errors: yes

    - name: Verify installation
      assert:
        that:
            - "rpm_result.rc == 1"
            - "yum_result.rc == 0"
            - "not yum_result.changed"
            - "not yum_result is failed"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Try to install incompatible arch
      yum:
        name: "{{ repodir_ppc64 }}/foo-1.0-1.ppc64.rpm"
        state: present
      register: yum_result
      ignore_errors: yes

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result
      ignore_errors: yes

    - name: Verify installation
      assert:
        that:
            - "rpm_result.rc == 1"
            - "yum_result.rc == 1"
            - "not yum_result.changed"
            - "yum_result is failed"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - name: Make sure latest foo is installed
      yum:
        name: foo
        state: latest

    - name: Downgrade foo using rpm file
      yum:
        name: "{{ repodir }}/foo-1.0-1.{{ ansible_architecture }}.rpm"
        state: present
        allow_downgrade: yes
      register: yum_result

    - name: Check foo with rpm
      shell: rpm -q foo
      register: rpm_result

    - name: Verify installation
      assert:
        that:
            - "yum_result.changed"
            - "rpm_result.stdout.startswith('foo-1.0-1')"

    - name: Verify yum module outputs
      assert:
        that:
            - "'msg' in yum_result"
            - "'rc' in yum_result"
            - "'results' in yum_result"
    # ============================================================================
    - block:
      - name: make sure foo is not installed
        yum:
          name: foo
          state: absent

      - name: install foo both archs
        yum:
          name: "{{ item }}"
          state: present
        with_items:
          - "{{ repodir }}/foo-1.1-1.x86_64.rpm"
          - "{{ repodir_i686 }}/foo-1.1-1.i686.rpm"

      - name: try to install lower version of foo from rpm file, without allow_downgrade, just one arch
        yum:
          name: "{{ repodir_i686 }}/foo-1.0-1.i686.rpm"
          state: present
        register: yum_result

      - name: check foo with rpm
        shell: rpm -q foo
        register: rpm_result

      - name: verify installation
        assert:
          that:
              - "not yum_result.changed"
              - "rpm_result.stdout_lines[0].startswith('foo-1.1-1')"
              - "rpm_result.stdout_lines[1].startswith('foo-1.1-1')"

      - name: verify yum module outputs
        assert:
          that:
              - "'msg' in yum_result"
              - "'rc' in yum_result"
              - "'results' in yum_result"
      when: ansible_architecture == "x86_64"
    # ============================================================================
    - block:
      - name: make sure foo is not installed
        yum:
          name: foo
          state: absent

      - name: install foo both archs
        yum:
          name: "{{ item }}"
          state: present
        with_items:
          - "{{ repodir }}/foo-1.0-1.x86_64.rpm"
          - "{{ repodir_i686 }}/foo-1.0-1.i686.rpm"

      - name: Update both arch in one task using rpm files
        yum:
          name: "{{ repodir }}/foo-1.1-1.x86_64.rpm,{{ repodir_i686 }}/foo-1.1-1.i686.rpm"
          state: present
        register: yum_result

      - name: check foo with rpm
        shell: rpm -q foo
        register: rpm_result

      - name: verify installation
        assert:
          that:
              - "yum_result.changed"
              - "rpm_result.stdout_lines[0].startswith('foo-1.1-1')"
              - "rpm_result.stdout_lines[1].startswith('foo-1.1-1')"

      - name: verify yum module outputs
        assert:
          that:
              - "'msg' in yum_result"
              - "'rc' in yum_result"
              - "'results' in yum_result"
      when: ansible_architecture == "x86_64"
    # ============================================================================
  always:
    - name: Clean up
      yum:
        name: foo
        state: absent
