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
Answer from Henrik Pingel on serverfault.comAnsible 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
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
I think the best solution for this is as follows:
- name: Find all rpm files in /tmp folder
ansible.builtin.find:
paths: "/tmp"
patterns: "*.rpm"
register: rpm_files
- name: Setting rpm_list
ansible.builtin.set_fact:
rpm_list: "{{ rpm_files.files | map(attribute='path') | list}}"
- name: installing the rpm files
ansible.builtin.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.
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 }}"
rhel - Transfer RPMs file with Ansible and localinstall them - Unix & Linux Stack Exchange
redhat - If I have a local rpm in my ansible-playbook can I do yum install in one step? - Stack Overflow
Ansible yum install rpm on remote server - Stack Overflow
Yum module's state=present with URL/path of RPM won't update a package (behavior change)
What are the prerequisites for using yum localinstall in Ansible?
Can Ansible resolve dependencies for RPMs?
How can I verify if the RPM is installed successfully via Ansible?
So, I want to install some rpm package where it has dependencies between each other. I didnt use loop because it will not work. So, I create below task:
- name: Find rpm files and register the result
find:
paths: /tmp/docker
patterns: "*.rpm"
register: rpm_files
- set_fact:
rpm_list: "{{ rpm_files.files | map(attribute='path') | list}}"
- debug:
var: rpm_list
- name: Install rpm files using rpm_list
yum:
name: "{{ rpm_list }}"
state: presentBut the result is not as I expected, because it seems still looking from internet:
TASK [docker : Find rpm files and register the result] *****************************************************************************************************************************************************
ok: [10.54.54.22]
TASK [docker : set_fact] ***********************************************************************************************************************************************************************************
ok: [10.54.54.22]
TASK [docker : debug] **************************************************************************************************************************************************************************************
ok: [10.54.54.22] => {
"rpm_list": [
"/tmp/docker/docker-scan-plugin-0.8.0-3.el8.x86_64.rpm",
"/tmp/docker/fuse3-3.2.1-12.el8.x86_64.rpm",
"/tmp/docker/fuse3-libs-3.2.1-12.el8.x86_64.rpm",
"/tmp/docker/fuse-common-3.2.1-12.el8.x86_64.rpm",
"/tmp/docker/fuse-overlayfs-1.6-1.module_el8.5.0+870+f792de72.x86_64.rpm",
"/tmp/docker/libcgroup-0.41-19.el8.x86_64.rpm",
"/tmp/docker/libslirp-4.4.0-1.module_el8.5.0+870+f792de72.x86_64.rpm",
"/tmp/docker/python3-dnf-plugins-core-4.0.21-1.el8.noarch.rpm",
"/tmp/docker/slirp4netns-1.1.8-1.module_el8.5.0+733+9bb5dffa.x86_64.rpm",
"/tmp/docker/tar-1.30-5.el8.x86_64.rpm",
"/tmp/docker/dnf-plugins-core-4.0.21-1.el8.noarch.rpm",
"/tmp/docker/yum-utils-4.0.21-1.el8.noarch.rpm",
"/tmp/docker/container-selinux-2.164.1-1.module_el8.5.0+870+f792de72.noarch.rpm",
"/tmp/docker/containerd.io-1.4.9-3.1.el8.x86_64.rpm",
"/tmp/docker/docker-ce-20.10.7-3.el8.x86_64.rpm",
"/tmp/docker/docker-ce-cli-20.10.7-3.el8.x86_64.rpm",
"/tmp/docker/docker-ce-rootless-extras-20.10.7-3.el8.x86_64.rpm"
]
}
TASK [docker : Install rpm files using rpm_list] ***********************************************************************************************************************************************************
fatal: [10.54.54.22]: FAILED! => {"changed": false, "msg": "Failed to download packages: Curl error (6): Couldn't resolve host name for http://mirrorlist.centos.org/?release=8-stream&arch=x86_64&repo=BaseOS&infra=stock [Could not resolve host: mirrorlist.centos.org]", "results": []}
PLAY RECAP *************************************************************************************************************************************************************************************************
10.54.54.22 : ok=6 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0Did something wrong on my rpm_list variable?