How to Install RPM Packages on Remote Machines Using Ansible Without Local Repositories


2 views

When managing Linux systems at scale, we often need to deploy standalone RPM packages directly without the overhead of setting up local repositories. While the yum or dnf modules are ideal for repository-based installs, they don't handle local RPM files elegantly.

The most robust approach combines Ansible's rpm_key and yum modules to handle both the package and its dependencies:


- name: Copy RPM package to target
  ansible.builtin.copy:
    src: /path/to/package.rpm
    dest: /tmp/package.rpm

- name: Install RPM package
  ansible.builtin.yum:
    name: /tmp/package.rpm
    state: present
    disable_gpg_check: yes  # Only if absolutely necessary

For more complex scenarios, consider these patterns:


# Method 1: Using get_url + yum
- name: Download RPM directly
  ansible.builtin.get_url:
    url: http://example.com/package.rpm
    dest: /tmp/

# Method 2: Atomic transaction approach  
- name: Install with transactional rollback
  block:
    - ansible.builtin.yum:
        name: /tmp/package.rpm
        state: present
  rescue:
    - ansible.builtin.yum:
        name: /tmp/package.rpm
        state: absent

For dependency resolution without repositories:


- name: Install dependencies first
  ansible.builtin.yum:
    name:
      - dependency1
      - dependency2
    state: present

- name: Then install main package
  ansible.builtin.yum:
    name: /tmp/package.rpm
    state: present

Key points for enterprise environments:

  • Always verify package checksums
  • Consider using createrepo if deploying multiple packages
  • Monitor disk space when working with large RPMs
  • Implement proper cleanup procedures post-installation

Installing an RPM package on a remote machine using Ansible is a common task, but many developers resort to the command module or setting up a Yum repository unnecessarily. There are more efficient ways to handle this.

rpm Module /h2>

Ansible provides the rpm module, which is specifically designed for installing RPM packages. Here's an example:


- name: Install an RPM package
  ansible.builtin.rpm:
    state: present
    path: /path/to/package.rpm

If the RPM file isn't already on the remote machine, you'll need to transfer it first:


- name: Copy RPM package to remote host
  ansible.builtin.copy:
    src: /local/path/package.rpm
    dest: /tmp/package.rpm

- name: Install the RPM package
  ansible.builtin.rpm:
    state: present
    path: /tmp/package.rpm

The rpm module doesn't handle dependencies automatically. For dependency resolution, you might need to:


- name: Install dependencies first
  ansible.builtin.yum:
    name: "{{ item }}"
    state: present
  loop:
    - dependency1
    - dependency2

- name: Then install the RPM
  ansible.builtin.rpm:
    state: present
    path: /tmp/package.rpm

yum with Local File /h2>

You can also use the yum module with a local file path:


- name: Install RPM using yum local install
  ansible.builtin.yum:
    name: /tmp/package.rpm
    state: present
  • Always verify the package integrity before installation
  • Consider using checksums when transferring files
  • For multiple packages, create a role to organize the tasks
  • Document any manual dependency resolution steps