5

我正在尝试使用 ansible 解压缩 tar.gz 文件(我已经做了很多),但由于某种原因,这次我无法解决这个错误。

我不断得到:

"msg": "Failed to find handler for \"/tmp/ansible_mnXJGp/internalbuildscripts1.tar.gz\". Make sure the required command to extract the file is installed. Command \"/bin/tar\" could not handle archive. Command \"/usr/bin/unzip\" could not handle archive."

奇怪的是,它似乎试图在 tar.gz 文件上使用解压缩。

任务如下所示:

- name: Download build scripts
  unarchive:
    src: https://sblah.com/internalbuildscripts1.tar.gz
    dest: /home/xxxx/buildscripts
    remote_src: yes
4

1 回答 1

6

所以我遇到了同样的问题,最终调试了 unarchive 模块,以找出它认为它无法处理文件的原因。

TL; DR Mydest是相对路径。检查unarchive是否gtar会处理该文件的检查是这样的:

/usr/local/bin/gtar --list -C ./my/dest/path -z -f /Users/fransf/.ansible/tmp/ansible-tmp-1603283396.0748851-45418-232955281061540/source

但它在工作目录也设置为 的情况./my/dest/path下做到了这一点,因此它有效地尝试解压到不存在的 ./my/dest/path/my/dest/path 中!

所以这就是我的问题。我改为使用绝对路径并且unarchive很高兴。我确实认为这是一个错误,并且显然是一个unarchive尚未包含在测试套件中的测试用例。

调试 Ansible 模块

  1. ANSIBLE_KEEP_REMOTE_FILES将环境变量设置为1(即)运行您的剧本ANSIBLE_KEEP_REMOTE_FILES=1 ansible-playbook -vvvv deploy.yml。在输出中的某处(如果您使用 运行-vvvv),您将看到如下内容:

     EXEC /bin/sh -c '/usr/local/Cellar/ansible/2.9.10/libexec/bin/python3.8 /Users/fransf/.ansible/tmp/ansible-tmp-1603283396.0748851-45418-232955281061540/AnsiballZ_unarchive.py && sleep 0'
    
  2. 运行相同的命令,但使用explode参数,如下所示:

     /usr/local/Cellar/ansible/2.9.10/libexec/bin/python3.8 /Users/fransf/.ansible/tmp/ansible-tmp-1603283396.0748851-45418-232955281061540/AnsiballZ_unarchive.py explode
    
  3. 你会得到一些输出,告诉你分解的模块源被写入哪里,例如

     2955281061540/AnsiballZ_unarchive.py explode
     Module expanded into:
     /Users/fransf/.ansible/tmp/ansible-tmp-1603283396.0748851-45418232955281061540/debug_dir
    
  4. 现在您已经获得了 Python 源代码,您可以使用print语句或(这就是我所做的)运行实际的调试器。要运行该模块,请使用与爆炸相同的命令,但指定execute而不是explode

     /usr/local/Cellar/ansible/2.9.10/libexec/bin/python3.8 /Users/fransf/.ansible/tmp/ansible-tmp-1603283396.0748851-45418-232955281061540/AnsiballZ_unarchive.py execute
    

    您可以从命令行或 IDE 执行此操作。我已经在使用 IntelliJ Ultimate 并且安装 Python 插件是我需要能够通过调试器运行这个模块、设置断点、查看变量并找出它搞砸的地方。

希望这可以帮助其他有类似问题的人。

于 2020-10-21T14:46:06.723 回答