From 27915421710eb82fdf21f88ddb0fbf66ee375870 Mon Sep 17 00:00:00 2001 From: Marat Bakeev Date: Mon, 15 Feb 2016 13:07:02 +1300 Subject: [PATCH] Allow specifying environment variables for ansible provisioner --- provisioner/ansible/provisioner.go | 10 +++++++++- website/source/docs/provisioners/ansible.html.markdown | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index 9fcf9f835..e047cd1e3 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -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") } diff --git a/website/source/docs/provisioners/ansible.html.markdown b/website/source/docs/provisioners/ansible.html.markdown index a572bac6a..b5ea420b9 100644 --- a/website/source/docs/provisioners/ansible.html.markdown +++ b/website/source/docs/provisioners/ansible.html.markdown @@ -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.