使用Ansible安装rpm软件包


22

如何使用Ansible在远程计算机上安装.rpm软件包?

显而易见的解决方案是使用command模块,但这有点愚蠢。我也想避免只为一个软件包设置一个yum存储库。

有没有更实用的方法来解决这个问题?


这就是ssh的优点。我知道有很酷的工具可以使用,但是对于实际的,不重要的任务来说,它是ssh。
洛林奇·齐格蒙德'18

Answers:


32

Ansible yum模块已经为该问题提供了解决方案。可以将服务器上本地rpm文件的路径传递给该name参数。

Ansible yum模块文档中

您还可以将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

抱歉,我忽略了它。谢谢您的帮助!:)
NefariousOctopus

有什么方法可以做到这一点而不必先复制文件?
技术专家

1
如果您通过http使软件包可用,则可以将url传递给name参数。
亨里克·平格尔

@technocrat,有点迟了,但回答了
Flippym

4

实际上,该yum模块可以直接从给定URL安装RPM:

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

出于好奇(不是错误的方式),这为什么不起作用?对于我来说,它失败了:yum install redhat.com/repo/my.rpm
eramm

我会说这可能是因为缺少协议
Flippym

2

这是从源计算机安装多个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}}"

1

因为我陷入了同样的问题,并且@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

0

尝试这个:

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

忽略警告。


-1

因为没有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

该任务对Ansible而言仅是幂等的。实际上,每次运行RPM都会重新下载并重新安装。
迈克(Mike)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.