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

ansible_ssh_extra_args fails with multiple -o params #197

Open joshtrutwin opened 3 months ago

joshtrutwin commented 3 months ago

Overview of the Issue

I'm trying to use the packer ansible provisioner with some additional ssh arguments to debug some connection issues I was having, specifically I was trying to use these options:

-o ControlMaster=no -o ControlPath=none -o ControlPersist=no

My first attempt at this was to use this for ansible_ssh_extra_args:

ansible_ssh_extra_args = ["-o ControlMaster=no -o ControlPath=none -o ControlPersist=no"]

Unfortunately what I noticed is this led to duplicate options in the ansible command (added -vvv for additional detail):

==> amazon-ebs.bastion-host-ami: Executing Ansible: ansible-playbook -e packer_build_name="bastion-host-ami" -e packer_builder_type=amazon-ebs --ssh-extra-args '-o ControlMaster=no -o ControlPath=none -o ControlPersist=no' -vvv --extra-vars packer_source=bastion-host-ami -e ansible_ssh_private_key_file=/tmp/ansible-key3084602121 -i /tmp/packer-provisioner-ansible3649825940 /__w/packer-ami-bastion-host/packer-ami-bastion-host/ansible/packer-build.yml

<snip>

 amazon-ebs.bastion-host-ami: <127.0.0.1> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=40563 -o 'IdentityFile="/tmp/ansible-key3084602121"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="ec2-user"' -o ConnectTimeout=10 -o ControlMaster=no -o ControlPath=none -o ControlPersist=no 127.0.0.1 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /tmp `"&& mkdir "` echo /tmp/ansible-tmp-1721684153.571383-455-188302921414123 `" && echo ansible-tmp-1721684153.571383-455-188302921414123="` echo /tmp/ansible-tmp-1721684153.571383-455-188302921414123 `" ) && sleep 0'"'"''

note in the SSH exec that it has duplicate valuse for -o ControlMaster (auto/no) and -o ControlPersist (60s/none).

After I noticed that I attempted to split each option thinking it might merge the options better:

ansible_ssh_extra_args = ["-o ControlMaster=no", "-o ControlPath=none", "-o ControlPersist=no"]

Unfortunately this led to a completely bizarre error:

==> amazon-ebs.bastion-host-ami: Executing Ansible: ansible-playbook -e packer_build_name="bastion-host-ami" -e packer_builder_type=amazon-ebs --ssh-extra-args '-o ControlMaster=no' '-o ControlPath=none' '-o ControlPersist=no' -vvv --extra-vars packer_source=bastion-host-ami -e ansible_ssh_private_key_file=/tmp/ansible-key2861560558 -i /tmp/packer-provisioner-ansible775734347 /__w/packer-ami-bastion-host/packer-ami-bastion-host/ansible/packer-build.yml

<snip>

amazon-ebs.bastion-host-ami: <127.0.0.1> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=38433 -o 'IdentityFile="/tmp/ansible-key2861560558"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="ec2-user"' -o ConnectTimeout=10 -o 'ControlMaster=no -o' 'ControlPath=none -o' ControlPersist=no 127.0.0.1 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /tmp `"&& mkdir "` echo /tmp/ansible-tmp-1721686895.4705355-454-220222974834521 `" && echo ansible-tmp-1721686895.4705355-454-220222974834521="` echo /tmp/ansible-tmp-1721686895.4705355-454-220222974834521 `" ) && sleep 0'"'"''
    amazon-ebs.bastion-host-ami: <127.0.0.1> (255, b'', b'command-line line 0: garbage at end of line; "-o".\r\n')
    amazon-ebs.bastion-host-ami: fatal: [default]: UNREACHABLE! => {
    amazon-ebs.bastion-host-ami:     "changed": false,
    amazon-ebs.bastion-host-ami:     "msg": "Failed to connect to the host via ssh: command-line line 0: garbage at end of line; \"-o\".",
    amazon-ebs.bastion-host-ami:     "unreachable": true
    amazon-ebs.bastion-host-ami: }

I believe this is related to this commit:

https://github.com/hashicorp/packer-plugin-ansible/pull/168/commits/205faa4cf43f7ed164877649c6e97e8bdcb71ba4

Reproduction Steps

Use multiple -o options for ansible_ssh_extra_args each split as a separate list item - for example:

ansible_ssh_extra_args = ["-o ControlMaster=no", "-o ControlPath=none", "-o ControlPersist=no"]

Plugin and Packer version

Packer v1.11.1

Installed plugin github.com/hashicorp/ansible v1.1.1 in "/github/home/.config/packer/plugins/github.com/hashicorp/ansible/packer-plugin-ansible_v1.1.1_x5.0_linux_amd64"

Installed plugin github.com/hashicorp/amazon v1.3.2 in "/github/home/.config/packer/plugins/github.com/hashicorp/amazon/packer-plugin-amazon_v1.3.2_x5.0_linux_amd64"

Simplified Packer Buildfile

Anything with the ansible_ssh_extra_args as shown above, if needed let me know and I will try to make something.

Operating system and Environment details

AmazonLinux AL2023 running packer through GitHub Actions self-hosted runner.

Log Fragments and crash.log files

embedded in description above, can provide more details if necessary but do not think needed for this case.