Merge pull request #3203 from mtb-xt/ansible-env

Allow specifying environment variables for ansible provisioner
This commit is contained in:
Chris Bednarski 2016-02-14 19:11:11 -08:00
commit fb7870ad94
2 changed files with 17 additions and 1 deletions

View File

@ -38,6 +38,8 @@ type Config struct {
// Extra options to pass to the ansible command
ExtraArguments []string `mapstructure:"extra_arguments"`
AnsibleEnvVars []string `mapstructure:"ansible_env_vars"`
// The main playbook file to execute.
PlaybookFile string `mapstructure:"playbook_file"`
Groups []string `mapstructure:"groups"`
@ -242,17 +244,23 @@ func (p *Provisioner) Cancel() {
func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator, privKeyFile string, checkHostKey bool) error {
playbook, _ := filepath.Abs(p.config.PlaybookFile)
inventory := p.config.inventoryFile
var envvars []string
args := []string{playbook, "-i", inventory}
if len(privKeyFile) > 0 {
args = append(args, "--private-key", privKeyFile)
}
args = append(args, p.config.ExtraArguments...)
if len(p.config.AnsibleEnvVars) > 0 {
envvars = append(envvars, p.config.AnsibleEnvVars...)
}
cmd := exec.Command(p.config.Command, args...)
if !checkHostKey {
if len(envvars) > 0 {
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, envvars...)
} else if !checkHostKey {
cmd.Env = append(cmd.Env, "ANSIBLE_HOST_KEY_CHECKING=False")
}

View File

@ -79,6 +79,14 @@ Optional Parameters:
- `extra_arguments` (array of strings) - Extra arguments to pass to Ansible.
- `ansible_env_vars` (array of strings) - Environment variables to set before running Ansible.
If unset, defaults to `ANSIBLE_HOST_KEY_CHECKING=False`.
Usage example:
```
"ansible_env_vars": [ "ANSIBLE_HOST_KEY_CHECKING=False", "ANSIBLE_SSH_ARGS='-o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s'", "ANSIBLE_NOCOLOR=True" ]
```
## Limitations
The `ansible` provisioner does not support SCP to transfer files.