skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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"
    
    Login or Signup to reply.
  3. 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 }}"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search