Skip to content

Commit

Permalink
Fix quoting of arguments passed through ansible_ssh_extra_args
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackenmen authored and lbajolet-hashicorp committed Oct 11, 2023
1 parent f37c535 commit 250ddf2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion provisioner/ansible/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
}
Expand Down
12 changes: 12 additions & 0 deletions provisioner/ansible/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 250ddf2

Please sign in to comment.