Ansible Unarchive Error: ‘Could Not Handle Archive’

Today, while trying to use Ansible to extract a local tar package, I received the message “could not handle archive. Command “/usr/bin/unzip” could not handle archive”.

This is my YAML file. I intend to extract the files to the bin directory.

it@mgt:~/kubelazy$ cat roles/unarchive-pkgs/tasks/main.yml---- name: unarchive kubernetes  unarchive:    src: bin/kubernetes-server-linux-amd64.tar.gz    dest: "bin/"

Running which unzip revealed that unzip is already installed.

it@mgt:~/kubelazy$ which unzip/usr/bin/unzip

I tried using the tar command, and the package could be extracted normally.

Through experimentation, I found that this issue is related to the dest path. I was using a relative path, but I needed to use an absolute path. However, the absolute path for bin may vary depending on the project’s directory. Here, I used the variable ansible_env.PWD to represent the current working directory. This allows me to locate the bin directory based on this directory.

The final YAML file.

it@mgt:~/kubelazy$ cat roles/unarchive-pkgs/tasks/main.yml---- name: unarchive kubernetes  unarchive:    src: bin/kubernetes-server-linux-amd64.tar.gz    dest: "{{ ansible_env.PWD }}/bin/"

Leave a Comment