Make template variables SSHPublicKey and SSHPrivateKey as strings (#8829)

This commit is contained in:
Sylvia Moss 2020-03-09 17:25:05 +01:00 committed by GitHub
parent 4bdf30165a
commit 76f13deaf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 20 deletions

View File

@ -61,8 +61,8 @@ func PopulateProvisionHookData(state multistep.StateBag) map[string]interface{}
hookData["User"] = commConf.User()
hookData["Password"] = commConf.Password()
hookData["ConnType"] = commConf.Type
hookData["SSHPublicKey"] = commConf.SSHPublicKey
hookData["SSHPrivateKey"] = commConf.SSHPrivateKey
hookData["SSHPublicKey"] = string(commConf.SSHPublicKey)
hookData["SSHPrivateKey"] = string(commConf.SSHPrivateKey)
// Backwards compatibility; in practice, WinRMPassword is fulfilled by
// Password.

View File

@ -83,13 +83,11 @@ func TestPopulateProvisionHookData(t *testing.T) {
if hookData["ConnType"] != commConfig.Type {
t.Fatalf("Bad: Expecting hookData[\"ConnType\"] was %s but actual value was %s", commConfig.Type, hookData["ConnType"])
}
sshPublicKey := fmt.Sprintf("%v", hookData["SSHPublicKey"].(interface{}))
if sshPublicKey == string(commConfig.SSHPublicKey) {
t.Fatalf("Bad: Expecting hookData[\"SSHPublicKey\"] was %s but actual value was %s", string(commConfig.SSHPublicKey), sshPublicKey)
if hookData["SSHPublicKey"] != string(commConfig.SSHPublicKey) {
t.Fatalf("Bad: Expecting hookData[\"SSHPublicKey\"] was %s but actual value was %s", string(commConfig.SSHPublicKey), hookData["SSHPublicKey"])
}
sshPrivateKey := fmt.Sprintf("%v", hookData["SSHPrivateKey"].(interface{}))
if sshPrivateKey == string(commConfig.SSHPrivateKey) {
t.Fatalf("Bad: Expecting hookData[\"SSHPrivateKey\"] was %s but actual value was %s", string(commConfig.SSHPrivateKey), sshPrivateKey)
if hookData["SSHPrivateKey"] != string(commConfig.SSHPrivateKey) {
t.Fatalf("Bad: Expecting hookData[\"SSHPrivateKey\"] was %s but actual value was %s", string(commConfig.SSHPrivateKey), hookData["SSHPrivateKey"])
}
if hookData["WinRMPassword"] != commConfig.WinRMPassword {
t.Fatalf("Bad: Expecting hookData[\"WinRMPassword\"] was %s but actual value was %s", commConfig.WinRMPassword, hookData["WinRMPassword"])

View File

@ -326,7 +326,7 @@ func (c *Config) Port() int {
}
}
// Host returns the port that will be used for access based on config.
// Host returns the host that will be used for access based on config.
func (c *Config) Host() string {
switch c.Type {
case "ssh":
@ -338,7 +338,7 @@ func (c *Config) Host() string {
}
}
// User returns the port that will be used for access based on config.
// User returns the user that will be used for access based on config.
func (c *Config) User() string {
switch c.Type {
case "ssh":
@ -350,7 +350,7 @@ func (c *Config) User() string {
}
}
// Password returns the port that will be used for access based on config.
// Password returns the password that will be used for access based on config.
func (c *Config) Password() string {
switch c.Type {
case "ssh":

View File

@ -74,15 +74,37 @@ Here is a full list of the available functions for reference.
}
```
Valid variables to request are: "ID", "Host",
"Port", "User", "Password", "ConnType",
"PackerRunUUID", "PackerHTTPAddr", "SSHPublicKey", and "SSHPrivateKey".
Depending on which communicator you are using, some of these values may be
empty -- for example, the public and private keys are unique to the SSH
communicator. InstanceID represents the vm being provisioned. For example,
in Amazon it is the instance id; in digitalocean, it is the droplet id; in
Vmware, it is the vm name.
Valid variables to request are:
- __ID__: Represents the vm being provisioned. For example, in Amazon it is the instance id; in digitalocean,
it is the droplet id; in Vmware, it is the vm name.
- __Host__, __Port__, __User__ and __Password__: The host, port, user, and password that Packer uses to access the machine.
Useful for using the shell local provisioner to run Ansible or Inspec against the provisioned instance.
- __ConnType__: Type of communicator being used. For example, for SSH communicator this will be "ssh".
- __PackerRunUUID__: Current build's unique id. Can be used to specify build artifacts.
- __PackerHTTPAddr__: HTTP address of the file server Packer creates to serve items in the "http" dir to the vm, displayed in the format `IP:PORT`.
- __SSHPublicKey__ and __SSHPrivateKey__: The public and private key that Packer uses to connect to the instance.
These are unique to the SSH communicator and are unset when using other communicators.
__SSHPublicKey__ and __SSHPrivateKey__ can have escape sequences and special characters so their output should be single quoted to avoid surprises. For example:
```
{
...
"provisioners": [
{
"type": "shell",
"inline": [
"echo '{{ build `SSHPrivateKey`}}' > /tmp/packer-session.pem"
]
}
]
}
```
For backwards compatability, `WinRMPassword` is also available through this
engine, though it is no different than using the more general `Password`.