From 250ddf26c1ea58418cbf9061af74ef1f84abf91f Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Tue, 8 Aug 2023 11:47:00 +0200 Subject: [PATCH] Fix quoting of arguments passed through ansible_ssh_extra_args --- provisioner/ansible/provisioner.go | 2 +- provisioner/ansible/provisioner_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index 6f1d70d..5ec54d0 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -838,7 +838,7 @@ func (p *Provisioner) createCmdArgs(httpAddr, inventory, playbook, privKeyFile s if p.generatedData["ConnType"] == "ssh" && len(privKeyFile) > 0 { // Add ssh extra args to set IdentitiesOnly if len(p.config.AnsibleSSHExtraArgs) > 0 { - args = append(args, "--ssh-extra-args", fmt.Sprintf("'%s'", strings.Join(p.config.AnsibleSSHExtraArgs, " "))) + args = append(args, "--ssh-extra-args", fmt.Sprintf("'%s'", strings.Join(p.config.AnsibleSSHExtraArgs, "' '"))) } else { args = append(args, "--ssh-extra-args", "'-o IdentitiesOnly=yes'") } diff --git a/provisioner/ansible/provisioner_test.go b/provisioner/ansible/provisioner_test.go index 01180db..37afed3 100644 --- a/provisioner/ansible/provisioner_test.go +++ b/provisioner/ansible/provisioner_test.go @@ -483,6 +483,18 @@ func TestCreateCmdArgs(t *testing.T) { ExpectedArgs: []string{"-e", "packer_build_name=\"packerparty\"", "-e", "packer_builder_type=fakebuilder", "--ssh-extra-args", "'-o IdentitiesOnly=no'", "-e", "ansible_ssh_private_key_file=/path/to/privkey.pem", "-e", "hello-world", "-i", "/var/inventory", "test-playbook.yml"}, ExpectedEnvVars: []string{"ENV_1=pancakes", "ENV_2=bananas"}, }, + { + // SSH with private key and an extra argument and multiple ssh extra arguments. + TestName: "SSH with private key and an extra argument and multiple ssh extra arguments", + PackerBuildName: "packerparty", + generatedData: basicGenData(nil), + ExtraArguments: []string{"-e", "hello-world"}, + AnsibleSSHExtraArgs: []string{"-o HostKeyAlgorithms=+ssh-rsa", "-o PubkeyAcceptedKeyTypes=+ssh-rsa", "-o IdentitiesOnly=no"}, + AnsibleEnvVars: []string{"ENV_1=pancakes", "ENV_2=bananas"}, + callArgs: []string{commonsteps.HttpAddrNotImplemented, "/var/inventory", "test-playbook.yml", "/path/to/privkey.pem"}, + ExpectedArgs: []string{"-e", "packer_build_name=\"packerparty\"", "-e", "packer_builder_type=fakebuilder", "--ssh-extra-args", "'-o HostKeyAlgorithms=+ssh-rsa' '-o PubkeyAcceptedKeyTypes=+ssh-rsa' '-o IdentitiesOnly=no'", "-e", "ansible_ssh_private_key_file=/path/to/privkey.pem", "-e", "hello-world", "-i", "/var/inventory", "test-playbook.yml"}, + ExpectedEnvVars: []string{"ENV_1=pancakes", "ENV_2=bananas"}, + }, { TestName: "SSH with private key and an extra argument and UseProxy", PackerBuildName: "packerparty",