hashicorp / packer-plugin-ansible

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

ansible-local provisioner cannot set packer_http_addr properly when the variable is not implemented #173

Open 126ium opened 1 year ago

126ium commented 1 year ago

Overview of the Issue

When a builder doesn't set packer_http_addr, the Ansible-local provisioner sets packer_http_addr=%!s(<nil>) in the command during the execution of the Ansible stage. This results in some builders encountering command syntax errors.

Reproduction Steps

  1. Navigate to the source directory of Ansible-local.
  2. Run the test cases as follows:
    [ansible-local]$ go test
    2023/09/13 14:38:27 ui: Provisioning with Ansible...  
    2023/09/13 14:38:27 ui: Creating Ansible staging directory...  
    2023/09/13 14:38:27 ui: Creating directory: /tmp/packer-provisioner-ansible-local/65013cc3-a324-8f87-d5b9-404a095af639
    2023/09/13 14:38:27 ui: Uploading playbook file: /tmp/468917457
    2023/09/13 14:38:27 ui: Creating directory: /tmp/packer-provisioner-ansible-local/65013cc3-a324-8f87-d5b9-404a095af639/tmp
    2023/09/13 14:38:27 ui: Uploading playbook file: /tmp/4263333196
    2023/09/13 14:38:27 ui: Creating directory: /tmp/packer-provisioner-ansible-local/65013cc3-a324-8f87-d5b9-404a095af639/tmp
    2023/09/13 14:38:27 ui: Uploading playbook file: /tmp/2126671814
    2023/09/13 14:38:27 ui: Creating directory: /tmp/packer-provisioner-ansible-local/65013cc3-a324-8f87-d5b9-404a095af639/tmp
    2023/09/13 14:38:27 ui: Uploading inventory file...
    2023/09/13 14:38:27 ui: Executing Ansible: cd /tmp/packer-provisioner-ansible-local/65013cc3-a324-8f87-d5b9-404a095af639 &&  ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook /tmp/packer-provisioner-ansible-local/65013cc3-a324-8f87-d5b9-404a095af639/tmp/468917457 --extra-vars "packer_build_name= packer_builder_type= packer_http_addr=%!s(<nil>) -o IdentitiesOnly=yes"  -c local -i /tmp/packer-provisioner-ansible-local/65013cc3-a324-8f87-d5b9-404a095af639/packer-provisioner-ansible-local1134823180

    The expected command should be: --extra-vars "packer_build_name= packer_builder_type= packer_http_addr= -o IdentitiesOnly=yes" The issue arises because packer_http_addr should be set as an empty string when it's nil.

Plugin and Packer version

I have made a pretty simple patch to fix this issue.