Pre-requisite tasks

Hello everybody, this is my first post here.

I have a playbook that includes these tasks:

`

  • name: Staging directory exists
    file:
    path: “{{ staging_dir }}”
    recurse: yes
    state: directory

  • name: “{{ java_package }} is present in staging directory”
    copy:
    src: “{{ java_package }}”
    dest: “{{ staging_dir }}/{{ java_package }}”

  • name: “{{ java_package }} is installed”
    yum:
    name: “{{ staging_dir }}/{{ java_package }}”
    state: present
    `

I would like to copy the file to the host only if needed, that is, the RPM in question is not already installed. Can I do this in Ansible?

Thanks!

Dan

- name: Check if package is installed
   command: rpm -q {{ java_package }}
   register: package_installed

- block:
   - name: Staging directory exists
     file:
       path: "{{ staging_dir }}"
       recurse: yes
       state: directory

   - name: "{{ java_package }} is present in staging directory"
     copy:
       src: "{{ java_package }}"
       dest: "{{ staging_dir }}/{{ java_package }}"

   - name: "{{ java_package }} is installed"
     yum:
       name: "{{ staging_dir }}/{{ java_package }}"
       state: present
   when: package_installed.rc == 1