hashicorp / packer-plugin-ansible

Packer plugin for Ansible Provisioner
https://www.packer.io/docs/provisioners/ansible
Mozilla Public License 2.0
49 stars 35 forks source link

Error: "fail to handshake" with vagrant and qemu provider #130

Open iapicca opened 1 year ago

iapicca commented 1 year ago

Overview of the Issue

ansible returns a fail to handshake to handshake when used with vagrant

Reproduction Steps

setup 0. test on `macos m1` w/ [homebrew](https://brew.sh/) installed 1. install [qemu](https://www.qemu.org/) `brew install qemu` 2. install [Vagrant](https://www.vagrantup.com/) `brew install --cask vagrant` 3. install [vagrant-qemu](https://github.com/ppggff/vagrant-qemu) plugin `vagrant plugin install vagrant-qemu` 4. install [packer](https://www.packer.io/) `brew install packer`
verify version ```console ➜ packer --version 1.8.4 ➜ vagrant plugin list vagrant-qemu (0.3.3, global) ➜ vagrant --version Vagrant 2.3.2 ➜ brew install qemu Warning: qemu 7.1.0 is already installed and up-to-date. To reinstall 7.1.0, run: brew reinstall qemu ```

first run

SSH Port was not properly retrieved from SSHConfig ```console ➜ packer build . vagrant.this: output will be in this color. ==> vagrant.this: Creating a Vagrantfile in the build directory... ==> vagrant.this: Adding box using vagrant box add ... vagrant.this: (this can take some time if we need to download the box) ==> vagrant.this: Calling Vagrant Up (this can take some time)... ==> vagrant.this: destroying Vagrant box... ==> vagrant.this: Deleting output directory... Build 'vagrant.this' errored after 5 minutes 46 seconds: error: SSH Port was not properly retrieved from SSHConfig. ==> Wait completed after 5 minutes 46 seconds ==> Some builds didn't complete successfully and had errors: --> vagrant.this: error: SSH Port was not properly retrieved from SSHConfig. ==> Builds finished but no artifacts were created. ``` ### this is probably an error with vagrant package because is fixed by running ```console ➜ vagrant init -m perk/ubuntu-2204-arm64 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. ➜ vagrant up Bringing machine 'default' up with 'qemu' provider... ==> default: Box 'perk/ubuntu-2204-arm64' could not be found. Attempting to find and install... default: Box Provider: libvirt default: Box Version: >= 0 ==> default: Loading metadata for box 'perk/ubuntu-2204-arm64' default: URL: https://vagrantcloud.com/perk/ubuntu-2204-arm64 ==> default: Adding box 'perk/ubuntu-2204-arm64' (v20221101.1) for provider: libvirt default: Downloading: https://vagrantcloud.com/perk/boxes/ubuntu-2204-arm64/versions/20221101.1/providers/libvirt.box default: Calculating and comparing box checksum... ==> default: Successfully added box 'perk/ubuntu-2204-arm64' (v20221101.1) for 'libvirt'! ==> default: Checking if box 'perk/ubuntu-2204-arm64' version '20221101.1' is up to date... ==> default: Importing a QEMU instance default: Creating and registering the VM... default: Successfully imported VM ==> default: Warning! The QEMU provider doesn't support any of the Vagrant ==> default: high-level network configurations (`config.vm.network`). They ==> default: will be silently ignored. ==> default: Starting the instance... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:50022 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection reset. Retrying... default: Warning: Remote connection disconnect. Retrying... default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ➜ vagrant ssh Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-52-generic aarch64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Fri Nov 11 10:48:37 UTC 2022 System load: 0.14208984375 Usage of /: 2.2% of 61.84GB Memory usage: 5% Swap usage: 0% Processes: 114 Users logged in: 0 IPv4 address for eth0: 10.0.2.15 IPv6 address for eth0: fec0::5054:ff:fe12:3456 0 updates can be applied immediately. The list of available updates is more than a week old. To check for new updates run: sudo apt update vagrant@ubuntu:~$ exit logout ➜ vagrant destroy -f ==> default: Stopping the instance... ==> default: Destroying the instance... ```

second run

➜  packer build .
vagrant.this: output will be in this color.

==> vagrant.this: Creating a Vagrantfile in the build directory...
==> vagrant.this: Adding box using vagrant box add ...
    vagrant.this: (this can take some time if we need to download the box)
==> vagrant.this: Calling Vagrant Up (this can take some time)...
==> vagrant.this: Using SSH communicator to connect: 127.0.0.1
==> vagrant.this: Waiting for SSH to become available...
==> vagrant.this: Connected to SSH!
==> vagrant.this: Provisioning with Ansible...
    vagrant.this: Setting up proxy adapter for Ansible....
==> vagrant.this: Executing Ansible: ansible-playbook -e packer_build_name="this" -e packer_builder_type=vagrant --ssh-extra-args '-o IdentitiesOnly=yes' -e ansible_ssh_private_key_file=/var/folders/p9/m6wv1ql505109hc1gv2d1rx40000gn/T/ansible-key1244138332 -i /var/folders/p9/m6wv1ql505109hc1gv2d1rx40000gn/T/packer-provisioner-ansible1459604115 /Users/yakforward/dev/devops/dart_vagrant/playbook.yml
    vagrant.this:
    vagrant.this: PLAY [install dart] ************************************************************
    vagrant.this:
    vagrant.this: TASK [Gathering Facts] *********************************************************
==> vagrant.this: failed to handshake
    vagrant.this: fatal: [default]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Unable to negotiate with 127.0.0.1 port 56361: no matching host key type found. Their offer: ssh-rsa", "unreachable": true}
    vagrant.this:
    vagrant.this: PLAY RECAP *********************************************************************
    vagrant.this: default                    : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0
    vagrant.this:
==> vagrant.this: Provisioning step had errors: Running the cleanup provisioner, if present...
==> vagrant.this: destroying Vagrant box...
==> vagrant.this: Deleting output directory...
Build 'vagrant.this' errored after 6 minutes 16 seconds: Error executing Ansible: Non-zero exit status: exit status 4

==> Wait completed after 6 minutes 16 seconds

==> Some builds didn't complete successfully and had errors:
--> vagrant.this: Error executing Ansible: Non-zero exit status: exit status 4

==> Builds finished but no artifacts were created.

Plugin and Packer version

packer: 1.8.4
ansible: >= 1.0.2

Simplified Packer Buildfile

Buildfile ```hcl packer{ required_plugins { # see https://github.com/hashicorp/packer-plugin-vagrant vagrant = { version = ">= 1.0.2" source = "github.com/hashicorp/vagrant" } # see https://github.com/hashicorp/packer-plugin-ansible ansible = { version = ">= 1.0.2" source = "github.com/hashicorp/ansible" } } } source "vagrant" "this" { communicator = "ssh" # see https://app.vagrantup.com/perk/boxes/ubuntu-2204-arm64 source_path = "perk/ubuntu-2204-arm64" provider = "qemu" add_force = true } build { sources = [ "source.vagrant.this" ] provisioner "ansible" { playbook_file = "./playbook.yml" } } ```
playbook ```yaml --- # playbook.yml - name: 'install dart' hosts: default become: true tasks: - name: 'download package information' command: | sudo apt-get update - name: 'install APT transport' command: | sudo apt-get install apt-transport-https - name: 'download signing key' command: | wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | \ sudo gpg --dearmor -o /usr/share/keyrings/dart.gpg - name: 'copy signing key' command: | echo 'deb [signed-by=/usr/share/keyrings/dart.gpg arch=amd64] https://storage.googleapis.com/download.dartlang.org/linux/debian stable main' | \ sudo tee /etc/apt/sources.list.d/dart_stable.list - name: 're-download package information' command: | sudo apt-get update - name: 'install dart' command: | sudo apt-get install dart - name: 'update path' command: | echo 'export PATH="$PATH:/usr/lib/dart/bin"' >> ~/.profile - name: 'version' command: | dart --version ```

Operating system and Environment details

neofetch ```console 'c. yakforward@yakforward.local ,xNMM. --------------------------- .OMMMMo OS: macOS 13.0.1 22A400 arm64 OMMM0, Host: MacBookAir10,1 .;loddo:' loolloddol;. Kernel: 22.1.0 cKMMMMMMMMMMNWMMMMMMMMMM0: Uptime: 1 hour, 47 mins .KMMMMMMMMMMMMMMMMMMMMMMMWd. Packages: 89 (brew) XMMMMMMMMMMMMMMMMMMMMMMMX. Shell: zsh 5.8.1 ;MMMMMMMMMMMMMMMMMMMMMMMM: Resolution: 1440x900 :MMMMMMMMMMMMMMMMMMMMMMMM: DE: Aqua .MMMMMMMMMMMMMMMMMMMMMMMMX. WM: Quartz Compositor kMMMMMMMMMMMMMMMMMMMMMMMMWd. WM Theme: Blue (Dark) .XMMMMMMMMMMMMMMMMMMMMMMMMMMk Terminal: iTerm2 .XMMMMMMMMMMMMMMMMMMMMMMMMK. Terminal Font: Monaco 12 kMMMMMMMMMMMMMMMMMMMMMMd CPU: Apple M1 ;KMMMMMMMWXXWMMMMMMMk. GPU: Apple M1 .cooc,. .,coo:. Memory: 1187MiB / 8192MiB ```

Logs

[SSH Port was not properly retrieved from SSHConfig] logs.txt [failed to handshake] logs.txt

related issues

bheisig commented 1 year ago

From the log: no matching host key type found. Their offer: ssh-rsa It indicates that an out-dated RSA with SHA1 is used but the OpenSSH client doesn't support it (see this discussion on SO).

My workaround is to enable ssh-rsa in the provisioner block:

ansible_ssh_extra_args = [
    "-o IdentitiesOnly=yes -o PubkeyAcceptedAlgorithms=+ssh-rsa -o HostkeyAlgorithms=+ssh-rsa"
]