如何修复Ansible安装Snap包时出现的执行错误?
问题描述
我的Ansible任务代码
我用Ansible编写了一个通过snap安装WPS Office的简单任务:
- name: "Install WPS Office" snap: name: wps-office
执行时报错
运行playbook时出现以下错误:
TASK [Install WPS Office] ********************************************************************************************* task path: /home/anton/ansible/roles/wps-office/tasks/install.yml:4 redirecting (type: modules) ansible.builtin.snap to community.general.snap <localhost> ESTABLISH LOCAL CONNECTION FOR USER: anton <localhost> EXEC /bin/sh -c 'echo ~anton && sleep 0' <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/anton/.ansible/tmp `"&& mkdir "` echo /home/anton/.ansible/tmp/ansible-tmp-1688299651.1242838-18662-118187479744114 `" && echo ansible-tmp-1688299651.1242838-18662-118187479744114="` echo /home/anton/.ansible/tmp/ansible-tmp-1688299651.1242838-18662-118187479744114 `" ) && sleep 0' redirecting (type: modules) ansible.builtin.snap to community.general.snap Using module file /home/anton/.ansible/collections/ansible_collections/community/general/plugins/modules/snap.py <localhost> PUT /home/anton/.ansible/tmp/ansible-local-185496p_gs6vg/tmp_e3ocvey TO /home/anton/.ansible/tmp/ansible-tmp-1688299651.1242838-18662-118187479744114/AnsiballZ_snap.py <localhost> EXEC /bin/sh -c 'chmod u+x /home/anton/.ansible/tmp/ansible-tmp-1688299651.1242838-18662-118187479744114/ /home/anton/.ansible/tmp/ansible-tmp-1688299651.1242838-18662-118187479744114/AnsiballZ_snap.py && sleep 0' <localhost> EXEC /bin/sh -c '/usr/bin/python3 /home/anton/.ansible/tmp/ansible-tmp-1688299651.1242838-18662-118187479744114/AnsiballZ_snap.py && sleep 0' <localhost> EXEC /bin/sh -c 'rm -f -r /home/anton/.ansible/tmp/ansible-tmp-1688299651.1242838-18662-118187479744114/ > /dev/null 2>&1 && sleep 0' The full traceback is: Traceback (most recent call last): File "/tmp/ansible_snap_payload_nl3bus9k/ansible_snap_payload.zip/ansible_collections/community/general/plugins/module_utils/mh/deco.py", line 52, in wrapper func(self, *args, **kwargs) File "/tmp/ansible_snap_payload_nl3bus9k/ansible_snap_payload.zip/ansible_collections/community/general/plugins/module_utils/mh/base.py", line 80, in run self.__run__() File "/tmp/ansible_snap_payload_nl3bus9k/ansible_snap_payload.zip/ansible_collections/community/general/plugins/module_utils/mh/mixins/state.py", line 37, in __run__ return func() ^^^^^^ File "/tmp/ansible_snap_payload_nl3bus9k/ansible_snap_payload.zip/ansible_collections/community/general/plugins/modules/snap.py", line 353, in state_present File "/tmp/ansible_snap_payload_nl3bus9k/ansible_snap_payload.zip/ansible_collections/community/general/plugins/modules/snap.py", line 341, in _present File "/tmp/ansible_snap_payload_nl3bus9k/ansible_snap_payload.zip/ansible_collections/community/general/plugins/module_utils/mh/base.py", line 40, in do_raise raise _MHE(*args, **kwargs) ansible_collections.community.general.plugins.module_utils.mh.exceptions.ModuleHelperException fatal: [localhost]: FAILED! => { "changed": false, "channel": null, "classic": false, "cmd": "['state', 'classic', 'channel', 'wps-office']", "invocation": { "module_args": { "channel": null, "classic": false, "name": [ "wps-office" ], "options": null, "state": "present" } }, "msg": "Ooops! Snap installation failed while executing '['state', 'classic', 'channel', 'wps-office']', please examine logs and error output for more details.", "output": { "channel": null, "classic": false, "cmd": "['state', 'classic', 'channel', 'wps-office']", "snaps_installed": [ "wps-office" ] }, "snaps_installed": [ "wps-office" ], "vars": { "channel": null, "classic": false, "cmd": "['state', 'classic', 'channel', 'wps-office']", "snaps_installed": [ "wps-office" ] } }
实际安装结果
虽然报错,但WPS Office已经成功安装:
$ snap info wps-office name: wps-office summary: WPS Office Suite publisher: liuyang8 license: unset description: | WPS Office, is an office productivity suite. WPS Office including Writer, Presentation and Spreadsheets, is a powerful office suite, which is able to process word file, produce wonderful slides, and analyze data as well. It is deeply compatible with all of the latest Microsoft Office file formats. It can easily open and read the documents created with Microsoft Office. This is the Linux version, and it's now a TESTING package. snap-id: fTF2opFpkHPihObSECzYPMyYbVfi9G4u channels: latest/stable: 10.1.0.6757 2018-09-11 (1) 250MB - latest/candidate: ↑ latest/beta: ↑ latest/edge: ↑
环境与疑问
我安装任何其他Snap包都会出现相同错误,并非仅WPS Office。我的Ansible版本为2.14.2,请问该错误的原因是什么?
解决方法
这个错误是community.general.snap模块的bug导致的,具体是模块在安装完成后进行状态校验时,错误地构造了无效的命令(错误日志里的['state', 'classic', 'channel', 'wps-office']完全不是合法的snap命令),引发了无意义的异常。
具体原因
从报错的traceback和错误信息来看,模块在调用_present方法时,错误地将参数拼接成了无效的命令格式,而不是执行正确的snap list或其他校验命令。这个问题常见于特定版本的community.general集合,和Ansible 2.14.x搭配时容易触发。
修复方案
- 升级community.general集合:执行以下命令更新到最新版,新版本已经修复了这个参数拼接的bug:
ansible-galaxy collection install community.general --upgrade - 临时绕过校验:如果暂时无法升级集合,可以在任务中添加
ignore_errors: true,同时配合changed_when来正确标记任务状态:
注意:使用这个方法前需要确保已经收集了snap的facts,可以通过- name: "Install WPS Office" snap: name: wps-office ignore_errors: true changed_when: "'wps-office' not in ansible_facts.snaps"setup模块或开启gather_facts来获取。
内容的提问来源于stack exchange,提问作者27cm
相关产品推荐
相关产品推荐

