Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ghost opened this issue Apr 16, 2021 · 2 comments
Open

Comments

@ghost
Copy link

ghost commented Apr 16, 2021

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
Copy link

itoffshore commented Apr 1, 2022

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
Copy link

eliasp commented Dec 20, 2023

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, " ")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants