在Ansible中创建已安装软件包的报告

发布于 2025-02-11 21:08:30 字数 1384 浏览 7 评论 0原文

我正在尝试使用几台机器中安装的软件包及其版本进行报告。

该报告必须在执行剧本的计算机上创建。

这是我当前的剧本,


---
- name: main
  hosts: all
  gather_facts: no
  become: true
  tasks:
  - setup:
        gather_subset:
         - '!all'

  - name: Gather rpm packages
    package_facts:
          manager: auto
    ignore_errors: true

  - name: Create the result file
    local_action:
     module: copy
     content: |
          {% for h in groups.all %}
          {{h}}; OS: {{hostvars[h]['ansible_distribution']|default('N/A')}}; Release: {{hostvars[h]['ansible_distribution_version']|default('N/A')}}

          List of instaled packages:.......
          #==============================================
          {% for k,v  in  hostvars[h].ansible_facts.packages.iteritems() %}
          package {{ k.rjust(24) }}      version {{ v[0].version }}
          {%endfor%}
          #==============================================
          {%endfor%}
     dest: '/reports/OS_info.txt'

现在可以运行

ansible-playbook -i rh_inventory  -u ansuser02  os_extract_info.yml

,如果所有主机都可以联系,则该报告是创建的,但是,如果一个或多个主机无法实现 剧本执行失败:

致命:[Instra-116]:失败! => {“ msg”:“任务包含一个带有未定义变量的选项。错误是:'dict Object'没有属性'packages'

,我无法弄清楚什么可以是克服此问题的最佳方法。

I'm trying to make a report with installed packages and their version from a couple of machines.

The report must be created on the machine from where the playbook is executed.

Here is my current playbook


---
- name: main
  hosts: all
  gather_facts: no
  become: true
  tasks:
  - setup:
        gather_subset:
         - '!all'

  - name: Gather rpm packages
    package_facts:
          manager: auto
    ignore_errors: true

  - name: Create the result file
    local_action:
     module: copy
     content: |
          {% for h in groups.all %}
          {{h}}; OS: {{hostvars[h]['ansible_distribution']|default('N/A')}}; Release: {{hostvars[h]['ansible_distribution_version']|default('N/A')}}

          List of instaled packages:.......
          #==============================================
          {% for k,v  in  hostvars[h].ansible_facts.packages.iteritems() %}
          package {{ k.rjust(24) }}      version {{ v[0].version }}
          {%endfor%}
          #==============================================
          {%endfor%}
     dest: '/reports/OS_info.txt'

Which is run with

ansible-playbook -i rh_inventory  -u ansuser02  os_extract_info.yml

Now, if all hosts are reachable, the report is created, however, if one or more hosts are unreachable
the playbook execution fails with:

fatal: [INFRA-116]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'packages'

And I can't figure out what could be the best approach to overcome this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2025-02-18 21:08:31

您必须像为其他信息一样应用默认< / code>过滤到该事实:

hostvars[h].ansible_distribution | default('N/A')

在这种情况下,由于您想循环浏览字典的键 /值对,因此您可以默认代码>软件包事实对任何一个空的事实:

{% for k, v in (hostvars[h].packages | default({})).items() %}

You have to apply a default filter to that fact, as you did for the other information:

hostvars[h].ansible_distribution | default('N/A')

In this case, since you want to loop over the key / value pair of a dictionary, you can default the packages fact to any empty one:

{% for k, v in (hostvars[h].packages | default({})).items() %}
千鲤 2025-02-18 21:08:31

经过一项非常详尽的研究,我发现了该过程:

首先在剧本中设置了“已安装” packages:

- name: Gather the package facts
  package_facts:
    manager: auto

- name: Get only list of name of installed packages
  set_fact:
    installed_packages: "{{ ansible_facts.packages.keys() }}"

其次,定义jinja2模板如下:

{% for item in installed_packages %}
{{ item }} = {{ ansible_facts.packages.get(item).0.version }}
{% endfor %}

输出/模板在目标服务器中生成包装库存。

After a really exhaustive research I found out the procedure:

First in the playbook set the fact installed_packages:

- name: Gather the package facts
  package_facts:
    manager: auto

- name: Get only list of name of installed packages
  set_fact:
    installed_packages: "{{ ansible_facts.packages.keys() }}"

Secondly, define the jinja2 template as follows:

{% for item in installed_packages %}
{{ item }} = {{ ansible_facts.packages.get(item).0.version }}
{% endfor %}

The output/template produces the package inventory in the target server(s).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文