From f760ab2fd85afcd3ab63d2c53f12c606b8fcd683 Mon Sep 17 00:00:00 2001 From: "Billie H. Cleek" Date: Sun, 11 Sep 2016 23:29:24 -0700 Subject: [PATCH] Make SCP the default for provisioner/ansible Add a new option, `use_sftp` to the ansible provisioner. It's default value is false; ansible provisioner will use SCP by default. Refactor to consistently set all configure options for ansible provisioner in the Prepare step. Remove incorrect information about `ANSIBLE_HOST_KEY_CHECKING=False` being set when `ansible_env_vars` is not set in the packer template. Update BATS tests for the ansible provisioner to actually check that the fetched directory contains the contents expected. This revealed a problem with the all_options template that required adding a host to the hosts list in the test playbook. --- provisioner/ansible/provisioner.go | 17 +++++++++-------- .../provisioner-ansible/all_options.json | 1 + test/fixtures/provisioner-ansible/playbook.yml | 2 +- test/fixtures/provisioner-ansible/scp.json | 3 +-- test/fixtures/provisioner-ansible/sftp.json | 5 +++-- test/provisioner_ansible.bats | 5 +++++ .../source/docs/provisioners/ansible.html.md | 9 +++++---- 7 files changed, 25 insertions(+), 17 deletions(-) diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index 74e1acc53..97623dda7 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -52,6 +52,7 @@ type Config struct { SSHHostKeyFile string `mapstructure:"ssh_host_key_file"` SSHAuthorizedKeyFile string `mapstructure:"ssh_authorized_key_file"` SFTPCmd string `mapstructure:"sftp_command"` + UseSFTP bool `mapstructure:"use_sftp"` inventoryFile string } @@ -106,6 +107,12 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { log.Println(p.config.SSHHostKeyFile, "does not exist") errs = packer.MultiErrorAppend(errs, err) } + } else { + p.config.AnsibleEnvVars = append(p.config.AnsibleEnvVars, "ANSIBLE_HOST_KEY_CHECKING=False") + } + + if !p.config.UseSFTP { + p.config.AnsibleEnvVars = append(p.config.AnsibleEnvVars, "ANSIBLE_SCP_IF_SSH=True") } if len(p.config.LocalPort) > 0 { @@ -277,7 +284,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { }() } - if err := p.executeAnsible(ui, comm, k.privKeyFile, !hostSigner.generated); err != nil { + if err := p.executeAnsible(ui, comm, k.privKeyFile); err != nil { return fmt.Errorf("Error executing Ansible: %s", err) } @@ -294,7 +301,7 @@ func (p *Provisioner) Cancel() { os.Exit(0) } -func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator, privKeyFile string, checkHostKey bool) error { +func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator, privKeyFile string) error { playbook, _ := filepath.Abs(p.config.PlaybookFile) inventory := p.config.inventoryFile var envvars []string @@ -315,10 +322,6 @@ func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator, pri cmd.Env = append(cmd.Env, envvars...) } - if !checkHostKey { - cmd.Env = append(cmd.Env, "ANSIBLE_HOST_KEY_CHECKING=False") - } - stdout, err := cmd.StdoutPipe() if err != nil { return err @@ -435,7 +438,6 @@ func newUserKey(pubKeyFile string) (*userKey, error) { type signer struct { ssh.Signer - generated bool } func newSigner(privKeyFile string) (*signer, error) { @@ -464,7 +466,6 @@ func newSigner(privKeyFile string) (*signer, error) { if err != nil { return nil, errors.New("Failed to extract private key from generated key pair") } - signer.generated = true return signer, nil } diff --git a/test/fixtures/provisioner-ansible/all_options.json b/test/fixtures/provisioner-ansible/all_options.json index 6d74d65c8..4f7e16255 100644 --- a/test/fixtures/provisioner-ansible/all_options.json +++ b/test/fixtures/provisioner-ansible/all_options.json @@ -18,6 +18,7 @@ "-vvvv", "--private-key", "ansible-test-id" ], "sftp_command": "/usr/lib/sftp-server -e -l INFO", + "use_sftp": true, "ansible_env_vars": ["PACKER_ANSIBLE_TEST=1", "ANSIBLE_HOST_KEY_CHECKING=False"], "groups": ["PACKER_TEST"], "empty_groups": ["PACKER_EMPTY_GROUP"], diff --git a/test/fixtures/provisioner-ansible/playbook.yml b/test/fixtures/provisioner-ansible/playbook.yml index fbe9cc6ac..b352387c0 100644 --- a/test/fixtures/provisioner-ansible/playbook.yml +++ b/test/fixtures/provisioner-ansible/playbook.yml @@ -1,5 +1,5 @@ --- -- hosts: default +- hosts: default:packer-test gather_facts: no tasks: - raw: touch /root/ansible-raw-test diff --git a/test/fixtures/provisioner-ansible/scp.json b/test/fixtures/provisioner-ansible/scp.json index 21d72a3c4..b94078f2a 100644 --- a/test/fixtures/provisioner-ansible/scp.json +++ b/test/fixtures/provisioner-ansible/scp.json @@ -7,10 +7,9 @@ "extra_arguments": [ "-vvvv" ], - "ansible_env_vars": ["ANSIBLE_SCP_IF_SSH=True"], "sftp_command": "/usr/bin/false" } - ], + ], "builders": [ { "type": "googlecompute", diff --git a/test/fixtures/provisioner-ansible/sftp.json b/test/fixtures/provisioner-ansible/sftp.json index 4b2c73b34..bc2e5d731 100644 --- a/test/fixtures/provisioner-ansible/sftp.json +++ b/test/fixtures/provisioner-ansible/sftp.json @@ -12,9 +12,10 @@ }, { "type": "ansible", "playbook_file": "./playbook.yml", - "sftp_command": "/usr/lib/sftp-server -e -l INFO" + "sftp_command": "/usr/lib/sftp-server -e -l INFO", + "use_sftp": true } - ], + ], "builders": [ { "type": "googlecompute", diff --git a/test/provisioner_ansible.bats b/test/provisioner_ansible.bats index c25fc8012..537435ae6 100755 --- a/test/provisioner_ansible.bats +++ b/test/provisioner_ansible.bats @@ -48,6 +48,7 @@ teardown() { run packer build ${USER_VARS} $FIXTURE_ROOT/minimal.json [ "$status" -eq 0 ] [ "$(gc_has_image "packerbats-minimal")" -eq 1 ] + diff -r dir fetched-dir/default/tmp/remote-dir > /dev/null } @test "ansible provisioner: build all_options.json" { @@ -55,6 +56,7 @@ teardown() { run packer build ${USER_VARS} $FIXTURE_ROOT/all_options.json [ "$status" -eq 0 ] [ "$(gc_has_image "packerbats-alloptions")" -eq 1 ] + diff -r dir fetched-dir/packer-test/tmp/remote-dir > /dev/null } @test "ansible provisioner: build scp.json" { @@ -62,6 +64,7 @@ teardown() { run packer build ${USER_VARS} $FIXTURE_ROOT/scp.json [ "$status" -eq 0 ] [ "$(gc_has_image "packerbats-scp")" -eq 1 ] + diff -r dir fetched-dir/default/tmp/remote-dir > /dev/null } @test "ansible provisioner: build sftp.json" { @@ -69,4 +72,6 @@ teardown() { run packer build ${USER_VARS} $FIXTURE_ROOT/sftp.json [ "$status" -eq 0 ] [ "$(gc_has_image "packerbats-sftp")" -eq 1 ] + diff -r dir fetched-dir/default/tmp/remote-dir > /dev/null } + diff --git a/website/source/docs/provisioners/ansible.html.md b/website/source/docs/provisioners/ansible.html.md index 7c80b273a..2656130e9 100644 --- a/website/source/docs/provisioners/ansible.html.md +++ b/website/source/docs/provisioners/ansible.html.md @@ -78,10 +78,12 @@ Optional Parameters: - `sftp_command` (string) - The command to run on the machine being provisioned by Packer to handle the SFTP protocol that Ansible will use to transfer files. The command should read and write on stdin and stdout, respectively. - SCP can be used instead of SFTP by setting `ANSIBLE_SCP_IF_SSH=True` in - `ansible_env_vars`. Defaults to `/usr/lib/sftp-server -e`. +- `use_sftp` (boolean) - Whether to use SFTP. When false, + `ANSIBLE_SCP_IF_SSH=True` will be automatically added to `ansible_env_vars`. + Defaults to false. + - `extra_arguments` (array of strings) - Extra arguments to pass to Ansible. Usage example: @@ -90,8 +92,7 @@ Optional Parameters: ``` - `ansible_env_vars` (array of strings) - Environment variables to set before - running Ansible. If unset, defaults to `ANSIBLE_HOST_KEY_CHECKING=False`. - Set `ANSIBLE_SCP_IF_SSH=True` to use SCP instead of SFTP. + running Ansible. Usage example: ```