大网数据广告活动
服务器运维问答

Collect packages versions with ansible

· 52CE 我爱测

Hi friends hope you're being well. I got this very basic task i need to accomplish via ansible but i'm stuck with variable resiter : i need to collect some packages version with ansible and display the output as : host has 'this …

问题原文

原帖链接 · infoman33 · CC BY-SA 4.0

Hi friends hope you're being well.

I got this very basic task i need to accomplish via ansible but i'm stuck with variable resiter :

i need to collect some packages version with ansible and display the output as :

host has 'this software : version'

here is the play:

---
    - name: Collect All Package Versions
      hosts: all
      become: yes
      tasks:
        - name: Update APT package cache
          apt:
            update_cache: yes
          become: yes
    
        - name: Collect Package Versions
          shell: dpkg-query -f '${binary:Package}\t${Version}\n' -W "{{ item }}"
          with_items:
            - firefox
            - libwebp6
            - thunderbird
          register: package_versions
    
        - name: Display Package Versions
          debug:
            var: package_versions.stdout_lines

When i execute it i got this message:

"package_versions.stdout_lines": "VARIABLE IS NOT DEFINED!"

I know there is a problem with variable registration but i need a little hint.

Thank you ;)

已采纳解答

原帖链接 · Vladimir Botka · CC BY-SA 4.0

Use the module package_facts. You'll get a dictionary of installed packages in the variable ansible_facts.packages. For example,

    - package_facts:
    - debug:
        msg: "{{ ansible_facts.packages[item]|
                 map(attribute='version') }}"
      loop:
        - firefox
        - libwebp6
        - thunderbird
      when: ansible_facts.packages[item] is defined

gives in Ubuntu

ok: [localhost] => (item=firefox) => 
  msg:
  - 88.0.1+build1-0ubuntu0.20.04.2
ok: [localhost] => (item=libwebp6) => 
  msg:
  - 0.6.1-2
ok: [localhost] => (item=thunderbird) => 
  msg:
  - 1:78.8.1+build1-0ubuntu0.20.04.1

The results are lists because more versions of a package might be installed. If you want to take the first items add the filter first to the pipe

    - debug:
        msg: "{{ ansible_facts.packages[item]|
                 map(attribute='version')|first }}"
      ...

If you put the list of the packages into a variable. For example,

  packages: [firefox, libwebp6, thunderbird]

you can declare the dictionary package_versions

  package_versions: "{{ dict(packages|
                             zip(packages|
                                 map('extract', ansible_facts.packages)|
                                 map('map', attribute='version'))) }}"

gives

  package_versions:
    firefox:
    - 88.0.1+build1-0ubuntu0.20.04.2
    libwebp6:
    - 0.6.1-2
    thunderbird:
    - 1:78.8.1+build1-0ubuntu0.20.04.1

Example of a complete playbook for testing

- hosts: localhost

  vars:

    packages: [firefox, libwebp6, thunderbird]

    package_versions: "{{ dict(packages|
                               zip(packages|
                                   map('extract', ansible_facts.packages)|
                                   map('map', attribute='version'))) }}"

  tasks:

    - package_facts:

    - debug:
        msg: "{{ ansible_facts.packages[item]|
                 map(attribute='version')|first }}"
      loop:
        - firefox
        - libwebp6
        - thunderbird
      when: ansible_facts.packages[item] is defined

    - debug:
        var: package_versions

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-10-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。