Packer build image in DigitalOcean

I’m having this error when trying to create the image in DigitalOcean! The error always happens when you start copying the ansible temp file to the remote drolpet…
it doesn’t seem to me to be an ssh error, but ansible! But it’s not clear to me :thinking:

==> digitalocean.autogenerated_1: Provisioning with Ansible...
    digitalocean.autogenerated_1: Setting up proxy adapter for Ansible....
2023/02/02 16:32:57 packer-provisioner-ansible plugin: Creating inventory file for Ansible run...
2023/02/02 16:32:57 packer-provisioner-ansible plugin: SSH proxy: serving on 127.0.0.1:43337
==> digitalocean.autogenerated_1: Executing Ansible: ansible-playbook -e packer_build_name="autogenerated_1" -e packer_builder_type=digitalocean --ssh-extra-args '-oHostKeyAlgorithms=+ssh-rsa -oPubkeyAcceptedKeyTypes=+ssh-rsa' --vault-password-file=***** -e ansible_ssh_private_key_file=/tmp/ansible-key1861898679 -i /tmp/packer-provisioner-ansible3091749051 /home/faioli/Trampo/Catarse/GIT/infra/packer/ansible/dev/roles/dev-catarse-sandbox_v1/playbook.yml
    digitalocean.autogenerated_1:
    digitalocean.autogenerated_1: PLAY [esse é o play para dev-catarse-sandbox_v1] *******************************
    digitalocean.autogenerated_1:
    digitalocean.autogenerated_1: TASK [Gathering Facts] *********************************************************
2023/02/02 16:32:57 packer-provisioner-ansible plugin: SSH proxy: accepted connection
2023/02/02 16:32:58 packer-provisioner-ansible plugin: authentication attempt from 127.0.0.1:60192 to 127.0.0.1:43337 as root using none
2023/02/02 16:32:58 packer-provisioner-ansible plugin: authentication attempt from 127.0.0.1:60192 to 127.0.0.1:43337 as root using publickey
2023/02/02 16:32:58 packer-provisioner-ansible plugin: authentication attempt from 127.0.0.1:60192 to 127.0.0.1:43337 as root using publickey
2023/02/02 16:32:58 packer-provisioner-ansible plugin: authentication attempt from 127.0.0.1:60192 to 127.0.0.1:43337 as root using publickey
2023/02/02 16:32:58 packer-provisioner-ansible plugin: authentication attempt from 127.0.0.1:60192 to 127.0.0.1:43337 as root using publickey
2023/02/02 16:32:58 packer-provisioner-ansible plugin: authentication attempt from 127.0.0.1:60192 to 127.0.0.1:43337 as root using publickey
2023/02/02 16:32:58 packer-provisioner-ansible plugin: new exec request: /bin/sh -c 'echo ~root && sleep 0'
2023/02/02 16:32:58 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:58 [DEBUG] Opening new ssh session
2023/02/02 16:32:58 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:58 [DEBUG] starting remote command: /bin/sh -c 'echo ~root && sleep 0'
2023/02/02 16:32:58 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:58 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:32:58 [INFO] 6 bytes written for 'stdout'
2023/02/02 16:32:58 [INFO] 0 bytes written for 'stderr'
2023/02/02 16:32:58 [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:32:58 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:32:58 [INFO] 0 bytes written for 'stdin'
2023/02/02 16:32:58 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stderr'
2023/02/02 16:32:58 packer-provisioner-ansible plugin: [INFO] 6 bytes written for 'stdout'
2023/02/02 16:32:58 packer-provisioner-ansible plugin: [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:32:58 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stdin'
2023/02/02 16:32:58 packer-provisioner-ansible plugin: new exec request: /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1675366378.4474046-1263537-260134773164240 `" && echo ansible-tmp-1675366378.4474046-1263537-260134773164240="` echo /root/.ansible/tmp/ansible-tmp-1675366378.4474046-1263537-260134773164240 `" ) && sleep 0'
2023/02/02 16:32:58 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:58 [DEBUG] Opening new ssh session
2023/02/02 16:32:58 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:58 [DEBUG] starting remote command: /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1675366378.4474046-1263537-260134773164240 `" && echo ansible-tmp-1675366378.4474046-1263537-260134773164240="` echo /root/.ansible/tmp/ansible-tmp-1675366378.4474046-1263537-260134773164240 `" ) && sleep 0'
2023/02/02 16:32:58 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:58 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:32:58 [INFO] 129 bytes written for 'stdout'
2023/02/02 16:32:58 [INFO] 0 bytes written for 'stderr'
2023/02/02 16:32:58 [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:32:58 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:32:58 [INFO] 0 bytes written for 'stdin'
2023/02/02 16:32:58 packer-provisioner-ansible plugin: [INFO] 129 bytes written for 'stdout'
2023/02/02 16:32:58 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stderr'
2023/02/02 16:32:58 packer-provisioner-ansible plugin: [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:32:58 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stdin'
2023/02/02 16:32:58 packer-provisioner-ansible plugin: new exec request: /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'python3.11'"'"'; command -v '"'"'python3.10'"'"'; command -v '"'"'python3.9'"'"'; command -v '"'"'python3.8'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'
2023/02/02 16:32:58 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:58 [DEBUG] Opening new ssh session
2023/02/02 16:32:59 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:59 [DEBUG] starting remote command: /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'python3.11'"'"'; command -v '"'"'python3.10'"'"'; command -v '"'"'python3.9'"'"'; command -v '"'"'python3.8'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'
2023/02/02 16:32:59 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:59 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:32:59 [INFO] 66 bytes written for 'stdout'
2023/02/02 16:32:59 [INFO] 0 bytes written for 'stderr'
2023/02/02 16:32:59 [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:32:59 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:32:59 [INFO] 0 bytes written for 'stdin'
2023/02/02 16:32:59 packer-provisioner-ansible plugin: [INFO] 66 bytes written for 'stdout'
2023/02/02 16:32:59 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stderr'
2023/02/02 16:32:59 packer-provisioner-ansible plugin: [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:32:59 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stdin'
2023/02/02 16:32:59 packer-provisioner-ansible plugin: new exec request: /bin/sh -c '/usr/bin/python3.8 && sleep 0'
2023/02/02 16:32:59 packer-provisioner-ansible plugin: [INFO] 1234 bytes written for 'stdin'
2023/02/02 16:32:59 [INFO] 1234 bytes written for 'stdin'
2023/02/02 16:32:59 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:59 [DEBUG] Opening new ssh session
2023/02/02 16:32:59 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:59 [DEBUG] starting remote command: /bin/sh -c '/usr/bin/python3.8 && sleep 0'
2023/02/02 16:32:59 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:59 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:32:59 [INFO] 0 bytes written for 'stderr'
2023/02/02 16:32:59 [INFO] 464 bytes written for 'stdout'
2023/02/02 16:32:59 [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:32:59 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:32:59 packer-provisioner-ansible plugin: [INFO] 464 bytes written for 'stdout'
2023/02/02 16:32:59 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stderr'
2023/02/02 16:32:59 packer-provisioner-ansible plugin: [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:32:59 packer-provisioner-ansible plugin: new subsystem request: sftp
2023/02/02 16:32:59 packer-provisioner-ansible plugin: starting sftp subsystem
2023/02/02 16:32:59 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:32:59 [DEBUG] Opening new ssh session
2023/02/02 16:33:00 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:33:00 [DEBUG] starting remote command: /usr/lib/sftp-server -e
2023/02/02 16:33:02 packer-provisioner-ansible plugin: [INFO] 297787 bytes written for 'stdin'
2023/02/02 16:33:02 [INFO] 297787 bytes written for 'stdin'
2023/02/02 16:33:02 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:33:02 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:33:02 [INFO] 569 bytes written for 'stdout'
2023/02/02 16:33:02 [INFO] 0 bytes written for 'stderr'
2023/02/02 16:33:02 [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:33:02 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:33:02 packer-provisioner-ansible plugin: [INFO] 569 bytes written for 'stdout'
2023/02/02 16:33:02 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stderr'
2023/02/02 16:33:02 packer-provisioner-ansible plugin: [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:33:02 packer-provisioner-ansible plugin: new exec request: /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1675366378.4474046-1263537-260134773164240/ > /dev/null 2>&1 && sleep 0'
2023/02/02 16:33:02 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:33:02 [DEBUG] Opening new ssh session
2023/02/02 16:33:02 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:33:02 [DEBUG] starting remote command: /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1675366378.4474046-1263537-260134773164240/ > /dev/null 2>&1 && sleep 0'
2023/02/02 16:33:02 packer-plugin-digitalocean_v1.1.1_x5.0_linux_amd64 plugin: 2023/02/02 16:33:02 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:33:02 [INFO] 0 bytes written for 'stdout'
2023/02/02 16:33:02 [INFO] 0 bytes written for 'stderr'
2023/02/02 16:33:02 [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:33:02 [INFO] RPC endpoint: Communicator ended with: 0
2023/02/02 16:33:02 [INFO] 0 bytes written for 'stdin'
2023/02/02 16:33:02 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stdout'
2023/02/02 16:33:02 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stderr'
2023/02/02 16:33:02 packer-provisioner-ansible plugin: [INFO] RPC client: Communicator ended with: 0
2023/02/02 16:33:02 packer-provisioner-ansible plugin: [INFO] 0 bytes written for 'stdin'
    digitalocean.autogenerated_1: fatal: [default]: FAILED! => {"msg": "failed to transfer file to /home/faioli/.ansible/tmp/ansible-local-1263531k4fwaxkf/tmpweqx6oq7 /root/.ansible/tmp/ansible-tmp-1675366378.4474046-1263537-260134773164240/AnsiballZ_setup.py:\n\n"}
    digitalocean.autogenerated_1:
    digitalocean.autogenerated_1: PLAY RECAP *********************************************************************
    digitalocean.autogenerated_1: default                    : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
    digitalocean.autogenerated_1:
2023/02/02 16:33:02 packer-provisioner-ansible plugin: shutting down the SSH proxy
2023/02/02 16:33:02 [INFO] (telemetry) ending ansible
==> digitalocean.autogenerated_1: Provisioning step had errors: Running the cleanup provisioner, if present...
==> digitalocean.autogenerated_1: Destroying droplet...
==> digitalocean.autogenerated_1: Deleting temporary ssh key...
2023/02/02 16:33:03 [INFO] (telemetry) ending digitalocean.autogenerated_1
==> Wait completed after 3 minutes 29 seconds
2023/02/02 16:33:03 machine readable: error-count []string{"1"}
==> Some builds didn't complete successfully and had errors:
2023/02/02 16:33:03 machine readable: digitalocean.autogenerated_1,error []string{"Error executing Ansible: Non-zero exit status: exit status 2"}
==> Builds finished but no artifacts were created.
2023/02/02 16:33:03 [INFO] (telemetry) Finalizing.
Build 'digitalocean.autogenerated_1' errored after 3 minutes 29 seconds: Error executing Ansible: Non-zero exit status: exit status 2

==> Wait completed after 3 minutes 29 seconds

==> Some builds didn't complete successfully and had errors:
--> digitalocean.autogenerated_1: Error executing Ansible: Non-zero exit status: exit status 2

The error was in the ssh proxy enabled: which is the default!

Initially, because of the proxy, the following error occurs when negotiating the ssh encryption algorithm:

ssh: Unable to negotiate with 127.0.0.1 port 34941: No matching host key type found. Their offer: ssh-rsa", "unreachable": true}

Reading the documentation, to fix the error, use the label:

"ansible_ssh_extra_args": ["-oHostKeyAlgorithms=+ssh-rsa -oPubkeyAcceptedKeyTypes=+ssh-rsa"]

Despite fixing the error, in my case a new error happened while copying the local ansible temp files to the remote host:

{"msg": "Failed to transfer file to /home/faioli/.ansible/tmp/ansible-local-1263531k4fwaxkf/tmpweqx6oq7 /root/.ansible/tmp/ansible-tmp-1675366378.4474046-1263537-260134773164240/Ansiball:Z_setup. py \n\n"}

The debug log is not clear as to why! It just points out that there was a failure during the copy…

The permission cannot be because on my local machine the temp file was created without issue and on the remote host the destination directory was created with correct permissions on the root directory.

The debug log makes you think of python, ansible and packer version! But as the error only occurs in the data stream when copying files… This makes you suspicious of ‘scp’ but without conclusive information

So I did the following test: disable proxy “use_proxy=no”

Without the proxy the ssh connection happened via the remote host’s IP… Which solved all the problems