使用ansible pip模块对已安装的pip软件包进行验证,但是却难以解析返回的结果。返回的result.results提供了一个数组,并且当 changed:true
时,我基本上只需要包 - name: test
pip:
name: "{{ item.package }}"
version: "{{ item.version }}"
state: present
executable: /opt/Anaconda/bin/pip
with_items:
- "{{ authorized_packages }}"
check_mode: yes
register: result
- name:
debug:
msg: "{{ result.results }}"
将.result.results作为数组返回,并且要在更改时== true时检索有关项目(包,版本)的信息;
{
"msg": {
"changed": true,
"msg": "All items completed",
"results": [
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/opt/Anaconda/bin/pip list --format=freeze",
"failed": false,
"invocation": {
"module_args": {
"chdir": null,
"editable": false,
"executable": "/opt/Anaconda/bin/pip",
"extra_args": null,
"name": [
"absl-py"
],
"requirements": null,
"state": "present",
"umask": null,
"use_mirrors": true,
"version": "0.1.10",
"virtualenv": null,
"virtualenv_command": "virtualenv",
"virtualenv_python": null,
"virtualenv_site_packages": false
}
},
"item": {
"package": "absl-py",
"version": "0.1.10"
},
"stderr": "",
"stderr_lines": [],
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"cmd": "/opt/Anaconda/bin/pip list --format=freeze",
"failed": false,
"invocation": {
"module_args": {
"chdir": null,
"editable": false,
"executable": "/opt/Anaconda/bin/pip",
"extra_args": null,
"name": [
"abstract-rendering"
],
"requirements": null,
"state": "present",
"umask": null,
"use_mirrors": true,
"version": "0.5.1",
"virtualenv": null,
"virtualenv_command": "virtualenv",
"virtualenv_python": null,
"virtualenv_site_packages": false
}
},
"item": {
"package": "abstract-rendering",
"version": "0.5.1"
},
"stderr": "",
"stderr_lines": [],
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/opt/Anaconda/bin/pip list --format=freeze",
"failed": false,
"invocation": {
"module_args": {
"chdir": null,
"editable": false,
"executable": "/opt/Anaconda/bin/pip",
"extra_args": null,
"name": [
"subprocess32"
],
"requirements": null,
"state": "present",
"umask": null,
"use_mirrors": true,
"version": "3.2.6",
"virtualenv": null,
"virtualenv_command": "virtualenv",
"virtualenv_python": null,
"virtualenv_site_packages": false
}
},
"item": {
"package": "subprocess32",
"version": "3.2.6"
},
"stderr": "",
"stderr_lines": [],
}
]
}
}
尝试
- name:
debug:
msg: "{{ result.results.0.item }}"
when: result.results.0.changed == true
但是仅返回单个结果;
{
"msg": {
"package": "absl-py",
"version": "0.1.10"
}
}
认为我实际上找到了可能的解决方案,任何人都可以审查,验证和/或提供任何进一步的反馈或改进吗?
- name:
debug:
msg: "Package:{{ item.item.package }} version:{{ item.item.version }} does not match the authorized package list"
with_items: "{{ result.results }}"
loop_control:
label: '{{ item.item }}'
when: item.changed==true
no_log: true