Transfer RPMs file with Ansible and localinstall them

9,518

Foreword

You should never use loop or with_item with the yum module unless you have an extremely particular situation because (quoting the doc)

When used with a loop: each package will be processed individually, it is much more efficient to pass the list directly to the name option.

And do yourself a favor: adopt the modern full yaml syntax for calling modules. It's easier to read and linters (e.g. yamllint) will catch more errors earlier.

My example is following those two rules.

Core answer

None of your above tries are sending the actual list of files to the yum module with the absolute path for each element. This info is quite easy to retreive from your copy task if you register the result. You can then filter the data (with e.g. json_query) to get only the relevant info.

The below playbook should give you the keys to do the job

---
- name: Copy and install rpms
  hosts: all

  vars:
    RPM: 
      - gskcrypt64-8.0.50.86.linux.ppcle.rpm 
      - gskssl64-8.0.50.86.linux.ppcle.rpm 
      - TIVsm-API64.ppc64le.rpm 
      - TIVsm-BA.ppc64le.rpm

  tasks:

    - name: "Transfer Files"
      copy:
        src: "/root/ansible_playbooks/{{ item }}"
        dest: /root/
      loop: "{{ RPM }}"
      register: copied

    - name: "Install RPMs from local copied files"
      yum:
        name: "{{ copied | json_query('results[?!failed].dest[]') }}"

Notes

  • The ?!failed filter in the query is only here to filter out results with an error if you ever decide to ignore errors on the copy task. You can remove it if you wish (i.e. json_query('results[].dest'))
  • loop: "{{ RPM }}" is strictly equivalent to with_items: "{{ RPM }}". loop is a newer syntax and was introduced in ansible 2.5. Both syntax are valid. See ansible loops documentation for more details.
Share:
9,518

Related videos on Youtube

igiannak
Author by

igiannak

Updated on September 18, 2022

Comments

  • igiannak
    igiannak over 1 year

    I am having the following yaml ansible-playbook and want to transfer the rpms and then localinstall them on the remote machine. Until the transfer file step its working fine however on the installation part i am taking the following error: "Failure talking to yum: near \"gskcrypt64\": syntax error" Any idea how to overcome this part?

    Ansible Version:

    ansible 2.4.2.0
      config file = /etc/ansible/ansible.cfg
      configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
       ansible python module location = /usr/lib/python2.7/site-packages/ansible
       executable location = /usr/bin/ansible
      python version = 2.7.5 (default, Jun 11 2019, 12:19:05) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
    

    Ansible Playbook:

    ---
    - hosts: "1.2.3.4"
      connection: "ssh"
      vars:
        RPM: 
        - gskcrypt64-8.0.50.86.linux.ppcle.rpm 
        - gskssl64-8.0.50.86.linux.ppcle.rpm 
        - TIVsm-API64.ppc64le.rpm 
        - TIVsm-BA.ppc64le.rpm
      tasks:
       - name: "Transfer Files"
         copy: src=/root/ansible_playbooks/{{ item }} dest=/root/
         with_items: "{{ RPM }}"
       - name: "Install Local RPMs"
         shell: yum localinstall -y /root/*.rpm
    

    Same error is reported if i change the shell module with

       - name: "Install Local RPMs"
         yum:
          name: /root/{{ RPM }}
          state: present
    

    Error:

    root@server [/root/ansible_playbooks] > ansible-playbook tsm
    
    PLAY [1.2.3.4] ******************************************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************
    ok: [1.2.3.4]
    
    TASK [Transfer Files] ****************************************************************************************************************************************************************************************************
    ok: [1.2.3.4] => (item=gskcrypt64-8.0.50.86.linux.ppcle.rpm)
    ok: [1.2.3.4] => (item=gskssl64-8.0.50.86.linux.ppcle.rpm)
    ok: [1.2.3.4] => (item=TIVsm-API64.ppc64le.rpm)
    ok: [1.2.3.4] => (item=TIVsm-BA.ppc64le.rpm)
    
    TASK [Install Local RPMs] ************************************************************************************************************************************************************************************************
    fatal: [1.2.3.4]: FAILED! => {"changed": false, "msg": "Failure talking to yum: near \"gskcrypt64\": syntax error"}
        to retry, use: --limit @/root/ansible_playbooks/tsm.retry
    
    PLAY RECAP ***************************************************************************************************************************************************************************************************************
    1.2.3.4               : ok=2    changed=0    unreachable=0    failed=1 
    

    Edit1: I tried the following loop since the files are already on the other side:

    ---
    - hosts: "1.2.3.4"
    connection: "ssh"
    vars:
    RPM:
    - gskcrypt64-8.0.50.86.linux.ppcle.rpm
    - gskssl64-8.0.50.86.linux.ppcle.rpm
    - TIVsm-API64.ppc64le.rpm
    - TIVsm-BA.ppc64le.rpm
    tasks:
    - name: "Install Local RPMs"
    yum:
    name: /root/"{{ item }}"
    state: present
    loop: "{{ RPM }}"
    

    Now getting the following error:

    fatal: [1.2.3.4]: FAILED! => {"msg": "Unexpected failure in finding the lookup named '{{ RPM }}' in the available lookup plugins"}
    

    Edit2:

    Tried to move and test on the failing part of yum localinstall:

      ---
    - hosts: "1.2.3.4"
      connection: "ssh"
      vars:
        RPM:
        - gskcrypt64-8.0.50.86.linux.ppcle.rpm
        - gskssl64-8.0.50.86.linux.ppcle.rpm
        - TIVsm-API64.ppc64le.rpm
        - TIVsm-BA.ppc64le.rpm
      tasks:
       - name: "Install Local RPMs"
         yum:
          name: "/root/{{RPM}}"
          state: present
    

    but getting the error:

    fatal: [1.2.3.4]: FAILED! => {"changed": false, "msg": "Failure talking to yum: near \"gskcrypt64\": syntax error"}
    
    • rush
      rush almost 5 years
      are you able to manually install gskcrypt64-8.0.50.86.linux.ppcle.rpm package with yum on a target system? It looks like there's something wrong with the package itself, not with your playbook.
    • igiannak
      igiannak almost 5 years
      @rush manually i can install them. when trying with ansible i get the eeror from edit2. i want to localinstall rpms with ansible and provide the complete list over a variable lis. via that i want to improve current mechanism tha copy the rpm in a temp dir and execute with shell module yum localinstall -y *
    • Zeitounator
      Zeitounator almost 5 years
      @Panki: with_<lookup> is not deprecated. Quoting the anssible loops documentation We have not deprecated the use of with_<lookup> - that syntax will still be valid for the foreseeable future.
  • igiannak
    igiannak almost 5 years
    Thank you i am trying now, but taking the following: fatal: [1.2.3.4]: FAILED! => {"msg": "Unexpected failure in finding the lookup named '{{ RPM }}' in the available lookup plugins".I have changed loop to with_items and worked perfectly. i will mark the answer as successful.
  • Zeitounator
    Zeitounator almost 5 years
    Which version oh ansible are you using?
  • igiannak
    igiannak almost 5 years
    ansible 2.4.2.0 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Jun 11 2019, 12:19:05) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
  • Zeitounator
    Zeitounator almost 5 years
    loop was officially introduced in ansible 2.5 (see notes in documentation). I'm even surprised it recognized the parameter at all. 2.4 is quite old. You might consider upgrading.
  • igiannak
    igiannak almost 5 years
    True just checked and we have to move to the RedHat Ansible Engine Products/Repos in order to move further that current version. Thank you for the information and the answer.
  • Zeitounator
    Zeitounator almost 5 years
    Please see my edit in my answer and my comment to @Pank above. Although it is a good idea to upgrade ansible, with_items remains absolutely valid.