elbarna
elbarna

Reputation: 727

Ansible: how to install multiple rpm but one per os?

I want to install multiple rpm, one is for Fedora servers, another one for Centos server. I have did this playbook file, but is wrong

- name: Copy rpm file to server
  hosts: fedora
  copy:
     src: /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
     dest: /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm

- name: Install package.
  hosts: fedora
  yum:
     name: /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
     state: present

- name: Copy another rpm file to server
  hosts: centos
  copy:
     src: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
     dest: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm

- name: Install another package.
  hosts: centos
  yum:
     name: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
     state: present

Upvotes: 0

Views: 1037

Answers (3)

sebthebert
sebthebert

Reputation: 12497

You can use 2 vars files (one for each distribution):

vars/centos_8.yml

packages:
  - pam_krb5-2.4.8-6.el8.x86_64.rpm
  - ...

vars/fedora_31.yml

packages:
  - pam_krb5-2.4.8-6.fc31.x86_64.rpm
  - ...

Then in your tasks, you can do something like that:

- name: Include vars for host distribution
  include_vars: "{{ ansible_distribution|lower }}_{{ ansible_distribution_major_version }}.yml"

- name: Copy RPM files to server
  copy:
     src: /tmp/{{ item }}
     dest: /tmp/{{ item }}
  with_items:
    - "{{ packages }}" 

- name: Install RPM packages
  yum:
     name: /tmp/{{ item }}
     state: present
  with_items:
    - "{{ packages }}"

Upvotes: 1

elbarna
elbarna

Reputation: 727

I have solved with a little different syntax

- name: Transfer and install a rpm for Centos server
  hosts: centos
  become_user: root
  tasks:
     - name: Copy another rpm file to server
       copy: src=/tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm dest=/tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
     - name: Install the rpm
       command: dnf -y localinstall /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm

- name: Transfer and install a rpm for Fedora server
  hosts: fedora
  become_user: root
  tasks:
     - name: Copy another rpm file to server
       copy: src=/tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm dest=/tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
     - name: Install the rpm
       command: dnf -y localinstall /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm

Upvotes: 0

itiic
itiic

Reputation: 3712

Your playbook should work but you can double secure yourself by adding when statement:

- name: Copy another rpm file to server
  hosts: centos
  copy:
     src: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
     dest: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
  when:
    - ansible_facts['distribution'] == "CentOS"

- name: Install another package.
  hosts: centos
  yum:
     name: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
     state: present
  when:
    - ansible_facts['distribution'] == "CentOS"

Upvotes: 2

Related Questions