hashicorp / packer-plugin-ansible

Packer plugin for Ansible Provisioner
https://www.packer.io/docs/provisioners/ansible
Mozilla Public License 2.0
47 stars 36 forks source link

ansible-local provisioner extra_arguments do not work with LXD builder #34

Open ghost opened 3 years ago

ghost commented 3 years ago

This issue was originally opened by @ppennanen as hashicorp/packer#6146. It was migrated here as a result of the Packer plugin split. The original body of the issue is below.


When used with the LXD builder, the ansible-local provisioner extra_arguments do not work as expected. See a (full example in this gist. Given this provisioner:

    {
      "type": "ansible-local",
      "clean_staging_directory": true,
      "playbook_file": "playbook.yml",
      "extra_arguments": [
        "--extra-vars \"test_variable={{user `test_variable`}}\"",
        "-vvv"
      ]
    }

It looks like extra_arguments are added to the command when ansible is called:

test-image: Executing Ansible: cd /tmp/packer-provisioner-ansible-local/5ad5b523-7407-ba59-c9c3-9c717f25eaa7 && ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook /tmp/packer-provisioner-ansible-local/5ad5b523-7407-ba59-c9c3-9c717f25eaa7/playbook.yml --extra-vars "packer_build_name=test-image packer_builder_type=lxd packer_http_addr=" --extra-vars "test_variable=cli-test" -vvv -c local -i /tmp/packer-provisioner-ansible-local/5ad5b523-7407-ba59-c9c3-9c717f25eaa7/packer-provisioner-ansible-local031086414

However the extra variable is not defined and ansible does not run in extra verbose -vvv mode:

    test-image:
    test-image: TASK [debug] *******************************************************************
    test-image: ok: [localhost] => {
    test-image:     "test_variable": "VARIABLE IS NOT DEFINED!"
    test-image: }

Packer version: 1.2.2 Host platform: Ubuntu 16.04 Gist: https://gist.github.com/ppennanen/77e64f55fa1d7218e10ecd8b1ae1f2e8

itoffshore commented 2 years ago

I think the ssh option -o IdentitiesOnly=yes should be removed from extraArgs in the ansible-local provisioner

(perhaps this invalid option is causing ansible to ignore the additional extraArgs)

I've tried removing it from the src & go clean && go build - & removing the existing plugin & placing the new plugin in:

~/$PACKER_CONIG_DIR/.packer.d/plugins/github.com/hashicorp/ansible

but I still see it showing up in the ansible command:

lxd.build-local: Executing Ansible: cd /tmp/packer-provisioner-ansible-local/62463d8a-fb15-88fd-755c-fa094aeaaa9e && ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook /tmp/packer-provisioner-ansible-local/62463d8a-fb15-88fd-755c-fa094aeaaa9e/playbooktest.yml --extra-vars "packer_build_name=build-local packer_builder_type=lxd packer_http_addr=ERR_HTTP_ADDR_NOT_IMPLEMENTED_BY_BUILDER -o IdentitiesOnly=yes" --extra-vars "build_template=alpine-lxd" -c local -i /tmp/packer-provisioner-ansible-local/62463d8a-fb15-88fd-755c-fa094aeaaa9e/inventory.yml 

I can make a PR for this change but wanted to test the change properly first - am I missing a step ?


(NB for others: the ansible provisioner works perfectly with the ansible lxd connection plugin community.general.lxd)

eliasp commented 8 months ago

When it comes to the -o IdentitiesOnly=yes issue, I believe the issue is caused by the fact, that what should be actually a value of --ssh-extra-args has been crammed into the value of --extra-vars.

So the initial fix (untested) might look a little more like this (although I believe the whole extraArgs handling should be a refactored a little for better readability/maintainability):

diff --git a/provisioner/ansible-local/provisioner.go b/provisioner/ansible-local/provisioner.go
index 9619733..c6ece7d 100644
--- a/provisioner/ansible-local/provisioner.go
+++ b/provisioner/ansible-local/provisioner.go
@@ -549,7 +549,7 @@ func (p *Provisioner) invokeGalaxyCommand(args []string, ui packersdk.Ui, comm p
 func (p *Provisioner) executeAnsible(ui packersdk.Ui, comm packersdk.Communicator) error {
        inventory := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.InventoryFile)))

-       extraArgs := fmt.Sprintf(" --extra-vars \"packer_build_name=%s packer_builder_type=%s packer_http_addr=%s -o IdentitiesOnly=yes\" ",
+       extraArgs := fmt.Sprintf(" --extra-vars \"packer_build_name=%s packer_builder_type=%s packer_http_addr=%s\" --ssh-extra-args \"-o IdentitiesOnly=yes\" ",
                p.config.PackerBuildName, p.config.PackerBuilderType, p.generatedData["PackerHTTPAddr"])
        if len(p.config.ExtraArguments) > 0 {
                extraArgs = extraArgs + strings.Join(p.config.ExtraArguments, " ")