JeyJ
JeyJ

Reputation: 4070

ansible get rpm version of variable

I'm getting as an external-var the name of a service. I want to find the rpm version of that service.

I used the following playbook : tasks:

   - name: get the rpm or apt package facts
     package_facts:
      manager: "auto"

   - name: get version of current installed rpm
     set_fact:
       rpm_version:  "{{ packages['{{ service_name }}'] }}"


   - name: print version of current installed rpm
     debug:
       msg: "{{ rpm_version }}"

I got the following error :

TASK [get version of current installed rpm] ************************************************************************************************************************************************
fatal: [10.20.10.74]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute '{{ service_name }}'\n\nThe error appears to be in '/home/m/my-playbook.yml': line 11, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n   - name: get version of current installed rpm\n     ^ here\n"}

I tried using escape characters like \' but it didn't really help.

How can I pass a variable to the packages dict?

When I replace the {{ service_name }} with a hard coded name it works as expected

Upvotes: 2

Views: 492

Answers (1)

Marco Baldelli
Marco Baldelli

Reputation: 3728

Try this syntax:

   - name: get version of current installed rpm
     set_fact:
       rpm_version: "{{ ansible_facts.packages[service_name] }}"

Upvotes: 2

Related Questions