Answers:
Ansible yum
模块已经为该问题提供了解决方案。可以将服务器上本地rpm文件的路径传递给该name
参数。
您还可以将url或本地路径传递到rpm文件。要对多个软件包进行操作,可以接受以逗号分隔的软件包列表或(自2.0版本起)软件包列表。
正确的步骤是这样的:
- name: Copy rpm file to server
copy:
src: package.rpm
dest: /tmp/package.rpm
- name: Install package.
yum:
name: /tmp/package.rpm
state: present
实际上,该yum
模块可以直接从给定URL安装RPM:
- name: Remote RPM install with yum
yum: name=http://example.com/some_package.rpm
这是从源计算机安装多个RPM的操作:
- 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}}"
因为我陷入了同样的问题,并且@Jim Hunziker的帖子将我推向正确的方向,所以我想分享我的解决方案,以安装特定的rpm版本,并且仅在必要时才上载rpm。
---
- hosts: testhosts
tasks:
- name: check if package installed
command: rpm -q --qf "%{VERSION}\n" package
args:
warn: false
register: package_installed
failed_when: false
changed_when: package_installed.rc != 0
- name: check package version
shell: "[[ `rpm -q --qf \"%{VERSION}\n\" package` == '{{ package_version }}' ]]"
args:
warn: false
register: package_installed_version
failed_when: false
changed_when: package_installed_version.rc != 0
- name: uninstall old package
yum:
name: package
state: removed
when: package_installed_version.rc != 0 and package_installed.rc == 0
- name: copy package-rpm to /tmp
copy:
src: package-{{ package_version }}-1.el7.x86_64.rpm
dest: /tmp/
notify:
- remove package-rpm from /tmp
when: package_installed_version.rc != 0
- name: install package
yum:
name: /tmp/package-{{ package_version }}-1.el7.x86_64.rpm
state: present
when: package_installed_version.rc != 0
handlers:
- name: remove package-rpm from /tmp
file:
name: /tmp/package-{{ package_version }}-1.el7.x86_64.rpm
state: absent
因为没有yum(它是AIX OS),所以必须使用强制性的rpm命令。
我已经解决了幂等性:
- command: rpm -U "{{rpm}}"
args:
warn: false
register: rpm_install
failed_when: rpm_install.rc != 0 and "is already installed" not in rpm_install.stderr
changed_when: rpm_install.rc == 0