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_env_vars are not set before version check #157

Open kernel-sanders opened 1 year ago

kernel-sanders commented 1 year ago

Overview of the Issue

When using the ansible provisioner on a system with home directories that are read only, it will fail with Error running "ansible-playbook --version": exit status 5

Reproduction Steps

Create a packer file with an ansible provisioner and custom env vars. We will use a test script as the command to see the full output.

...
  provisioner "ansible" {
    command = "/tmp/test.sh"
    user = "${var.ssh_username}"
    use_proxy = false
    extra_arguments = ["-vvv"]
    ansible_env_vars = ["ANSIBLE_HOME=${var.ansible_home}", "TEST_ENV_VAR=JUST_FOR_TESTING"]
    playbook_file   = "playbook.yml"
  }
...

/tmp/test.sh contains

#!/bin/bash
env > /tmp/env.log
strace /usr/local/bin/ansible-playbook "$@" >> /tmp/ansible-debug.log 2>/tmp/strace.log

Run the packer file and view the env.log and strace.log files. The env.log will show that none of the custom ansible_env_vars are set, and strace will show an error writing to the default home directory (assuming your home directory is read-only)

mkdir("/home/username/.ansible/tmp/ansible-local-38561yu5y65d3", 0700) = -1 EROFS (Read-only file system)
write(2, "ERROR: Unhandled exception when "..., 240ERROR: Unhandled exception when retrieving DEFAULT_LOCAL_TMP:
[Errno 30] Read-only file system: '/home/username/.ansible/tmp/ansible-local-38561yu5y65d3'. [Errno 30] Read-only file system: '/home/username/.ansible/tmp/ansible-local-38561yu5y65d3') = 240

Adding skip_version_check = true to the ansible provisioner prevents this error, as the env vars are set correctly before the actual ansible playbook is run.

Packer version

Packer v1.8.5

Operating system and Environment details

Debian 11