2

I have to install dozen of rpms located in a specific directory using ansible. Right now I'm using the syntax like:

- name: install uploaded rpms
  command: rpm -ivh /tmp/*.rpm

I want to do it using yum module, but don't know, how to tell it to install all rpms in a directory (not to specify name of each file).

Any suggestions?

Thanks in advance

agordgongrant
  • 31
  • 1
  • 2
  • 4

4 Answers4

8

I think the best solution for this is as follows:

 - name: Find all rpm files in /tmp folder
   find:
     paths: "/tmp"
     patterns: "*.rpm"
   register: rpm_files
 
 - name: Setting rpm_list
   set_fact:
    rpm_list: "{{ rpm_files.files | map(attribute='path') | list}}"

 - name: installing the rpm files
   yum:
     name: "{{rpm_list}}"
     state: present

Looping through the files might cause Yum Lock issues. So this is better and efficient as we don't have to loop through all the files, instead we are passing a list of file paths to the yum module.

caot
  • 3,066
  • 35
  • 37
Pavan Srinivas
  • 106
  • 1
  • 3
  • 1
    Thanks for this, been pulling my hair on this. – Pablo Fallas May 26 '20 at 21:44
  • What does "{{ rpm_files.files | map(attribute='path') | list}}" do exactly? – Jontia Apr 20 '21 at 14:05
  • @Jontia reads the `register` variable `rpm_files`, gets the .files property from that object, pipes that value to a map function that extracts the .path property from each rpm "files" object (returned from the ansible `find` module), and concatenates each item into a list. it's a jinja template that sets the value of `rpm_list` – activedecay Oct 14 '21 at 23:03
6

Can you try this(I didn't test it):

- name: Finding RPM files
  find:
    paths: "/tmp"
    patterns: "*.rpm"
  register: rpm_result

- name: Install RPM
  yum:
    name: "{{ item.path }}"
    state: present
  with_items: "{{ rpm_result.files }}"
Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82
  • The solution doesn't work the same way as yum *.rpm since it install the files in the order of rpm_result.files and could fail on dependencies – caot Feb 18 '21 at 14:29
1

try this:

- name: Installed the rpm files
  shell: yum localinstall *.rpm -y
  args:
    chdir: /tmp/rpm_dir

ignore the warning.

liuhao
  • 631
  • 5
  • 3
0

As https://stackoverflow.com/a/45708676/11887927 commented and I tested. In most cases, it succeeded but adding retries to the task will help ensure the dependencies problems since we're using find module and it returns the results one by one. Here is the example of the revised:

- name: Finding RPM files
  find:
    paths: "/tmp"
    patterns: "*.rpm"
  register: rpm_result

- name: Install RPM
  yum:
    name: "{{ item.path }}"
    state: present
  with_items: "{{ rpm_result.files }}"
  retries: 5
  register: result
  until: result.rc == 0

The issue happened when there are 6 RPMs.

References:

  1. https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#retrying-a-task-until-a-condition-is-met
  2. https://stackoverflow.com/a/44135131/11887927
Chang Joe
  • 33
  • 6