Install rpm package using Ansible

Solution 1:

Ansible yum module already provides a solution for this problem. The path to the local rpm file on the server can be passed to the name parameter.

From the Ansible yum module documentation:

You can also pass a url or a local path to a rpm file. To operate on several packages this can accept a comma separated list of packages or (as of 2.0) a list of packages.

The proper steps to do this would be something like this:

- name: Copy rpm file to server
  copy:
     src: package.rpm
     dest: /tmp/package.rpm

- name: Install package.
  yum:
     name: /tmp/package.rpm
     state: present

Solution 2:

Actually the yum module can install an RPM directly from a given URL:

- name: Remote RPM install with yum
  yum: name=http://example.com/some_package.rpm

Solution 3:

Here's what I do to install multiple RPMs from the source machine:

- name: mkdir /tmp/RPMS
  file: path=/tmp/RPMS state=directory

- name: copy RPMs to /tmp/RPMS
  copy:
    src: "{{ item }}"
    dest: /tmp/RPMS
  with_fileglob:
    - "../files/*.rpm"
  register: rpms_copied

- name: local RPMs not found
  fail:
    msg: "RPMs not found in ../files/"
  when: rpms_copied.results|length == 0 and rpms_copied.skipped and rpms_copied.skipped_reason.find('No items') != -1

- set_fact:
    rpm_list: "{{ rpms_copied.results | map(attribute='dest') | list}}"

- name: install RPMs
  yum:
    name: "{{rpm_list}}"