如何解决Ansible Unarchive安装Chrome Infra gn二进制包的时间格式错误?
解决Ansible unarchive模块处理GN二进制ZIP包的时间格式错误问题
这个问题我之前也碰到过!根源在于GN提供的ZIP包里的文件带有不符合标准的时间戳19800000.000000,Ansible的unarchive模块依赖Python的zipfile库解析归档信息,而这个库对时间格式的校验很严格,无法识别月份和日期为00的时间戳,因此抛出了格式不匹配的错误。
靠谱的解决方案:改用get_url+系统unzip命令
绕开Ansible内置的unarchive模块,用系统自带的unzip工具处理归档,它对异常时间戳的容错性更强。下面是完整的Playbook代码:
- name: Create temporary directory for GN download tempfile: state: directory suffix: gn register: gn_temp_dir - name: Download GN binary ZIP package get_url: url: https://chrome-infra-packages.appspot.com/dl/gn/gn/linux-amd64/+/latest dest: "{{ gn_temp_dir.path }}/gn.zip" mode: 0644 - name: Extract only the GN binary to /usr/bin become: yes command: unzip -o "{{ gn_temp_dir.path }}/gn.zip" gn -d /usr/bin/ args: creates: /usr/bin/gn # 避免重复执行 - name: Ensure GN binary has executable permissions become: yes file: path: /usr/bin/gn mode: 0755 - name: Clean up temporary download directory file: path: "{{ gn_temp_dir.path }}" state: absent
方案说明:
- 临时目录:用
tempfile模块创建临时目录,避免和系统中其他文件冲突,也方便后续清理。 - 下载文件:
get_url负责下载ZIP包,比直接在unarchive里用remote_src更可控。 - 解压二进制:用
unzip命令直接指定提取gn单个文件(自动跳过.cipdpkg目录),-o参数覆盖已存在的文件,creates参数保证只有当目标文件不存在时才执行。 - 权限设置:显式设置
0755权限,确保二进制可执行。 - 清理临时文件:避免残留无用文件,保持系统整洁。
为什么不继续用unarchive模块?
即使升级Ansible版本,这个问题大概率还是存在——因为核心矛盾是Python的zipfile库不支持这种异常时间戳,而系统的unzip工具在设计时就考虑了各种非标准归档的兼容场景,所以是更稳妥的选择。
内容的提问来源于stack exchange,提问作者John McGehee
相关产品推荐
相关产品推荐

