在Ansible中使用package_facts模块时遇到问题。基本上,我只想安装zabbix-agent的版本,因为我需要根据安装的版本进行一些操作。
现在我在剧本任务中得到了这个
- name: Gather Installed Packages Facts
package_facts:
manager: "auto"
tags:
- zabbix-check
- name: "Zabbix Found test result"
debug: var=ansible_facts.packages['zabbix-agent']
when: "'zabbix-agent' in ansible_facts.packages"
tags:
- zabbix-check
- name: "Zabbix Not-found test result"
debug:
msg: "Zabbix NOT found"
when: "'zabbix-agent' not in ansible_facts.packages"
tags:
- zabbix-check
哪个吐出这样的东西:
ok: [vm3] => {
"ansible_facts.packages['zabbix-agent']": [
{
"arch": "x86_64",
"epoch": null,
"name": "zabbix-agent",
"release": "1.el7",
"source": "rpm",
"version": "4.0.10"
]
}
ok: [vm4] => {
"ansible_facts.packages['zabbix-agent']": [
{
"arch": "x86_64",
"epoch": null,
"name": "zabbix-agent",
"release": "1.el7",
"source": "rpm",
"version": "3.2.11"
}
]
}
我想获取该“版本”的值:“ 3.2.11”,以便可以将其存储在变量中并在以后使用。我已经看到了使用yum并执行一些json查询的帖子,但这对我不起作用。
答案 0 :(得分:0)
由于某种原因(可能是由于可能安装了同一软件包的更多版本),软件包字典的值是一个列表。一个简单的解决方案是采用第一个元素
- set_fact:
za_ver: "{{ ansible_facts.packages['zabbix-agent'][0].version }}"
when: "'zabbix-agent' in ansible_facts.packages"
要考虑安装更多版本的可能性,请使用map过滤器
- set_fact:
za_ver: "{{ ansible_facts.packages['zabbix-agent']|
map(attribute='version')|
list }}"
when: "'zabbix-agent' in ansible_facts.packages"
以下与json_query过滤器等效
- set_fact:
za_ver: "{{ ansible_facts.packages['zabbix-agent']|
json_query('[].version') }}"
when: "'zabbix-agent' in ansible_facts.packages"
答案 1 :(得分:0)
是否可以调试版本是否与特定值匹配?
- set_fact:
pkg_ver: "{{ ansible_facts.packages['{{ item.package_name }}'][0].version }}"
loop: "{{ lookup('dict', packagesToValidate) }}"
when: "'{{ item.package_name }}' in ansible_facts.packages" and inventory_hostname in item.hosts
是packagesToValidate包含主机,版本和程序包名称的字典以进行验证吗?