在Ansible中创建已安装软件包的报告
我正在尝试使用几台机器中安装的软件包及其版本进行报告。
该报告必须在执行剧本的计算机上创建。
这是我当前的剧本,
---
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须像为其他信息一样应用
默认< / code>过滤到该事实:
在这种情况下,由于您想循环浏览字典的键 /值对,因此您可以默认代码>软件包事实对任何一个空的事实:
You have to apply a
default
filter to that fact, as you did for the other information: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:经过一项非常详尽的研究,我发现了该过程:
首先在剧本中设置了“已安装” packages:
其次,定义jinja2模板如下:
输出/模板在目标服务器中生成包装库存。
After a really exhaustive research I found out the procedure:
First in the playbook set the fact installed_packages:
Secondly, define the jinja2 template as follows:
The output/template produces the package inventory in the target server(s).