solo-io / packer-plugin-arm-image

Packer plugin for ARM images
Apache License 2.0
425 stars 104 forks source link

Exposing MountPath build variable for use by Ansible provisioner #125

Closed AlexTawse closed 2 years ago

AlexTawse commented 2 years ago

This addresses the issue described here: https://github.com/solo-io/packer-plugin-arm-image/issues/121

As per the suggested solution, the variable build.MountPath can now be accessed from within a provisioner.

This allows the use of Packer's built-in Ansible provisioner, which removes the need to manually invoke Ansible using shell-local as per /samples/raspbian_ansible_chroot.json

I've included a new sample, which is essentially a rewrite of /samples/raspbian_ansible_chroot.json but now using the Ansible provisioner (and written in HCL2).

I don't know Go very well, so I can make changes as needed. I've done some basic manual testing on my machine, and everything seems to work!

Thanks.

solo-build-bot[bot] commented 2 years ago

Waiting for approval from someone in the solo-io org to start testing.

shanduur commented 1 year ago

Did you managed to make the Ansible provisioner working? I am having issue with that:

==> arm-image.ubuntu-2204: Executing Ansible: ansible-playbook -e packer_build_name="ubuntu-2204" -e packer_builder_type=arm-image --connection=chroot --become-user=root --extra-vars ansible_host=/tmp/armimg-723220737 --extra-vars default_username=admin --extra-vars default_password=***** -e ansible_ssh_private_key_file=/tmp/ansible-key3896691879 -i /tmp/packer-provisioner-ansible3568520362 /home/shanduur/Repos/deployment/images/rpi-ubuntu-22.04/ansible/playbooks/user.yaml
    arm-image.ubuntu-2204: usage: ansible-playbook [-h] [--version] [-v] [--private-key PRIVATE_KEY_FILE]
    arm-image.ubuntu-2204:                         [-u REMOTE_USER] [-c CONNECTION] [-T TIMEOUT]
    arm-image.ubuntu-2204:                         [--ssh-common-args SSH_COMMON_ARGS]
    arm-image.ubuntu-2204:                         [--sftp-extra-args SFTP_EXTRA_ARGS]
    arm-image.ubuntu-2204:                         [--scp-extra-args SCP_EXTRA_ARGS]
    arm-image.ubuntu-2204:                         [--ssh-extra-args SSH_EXTRA_ARGS]
    arm-image.ubuntu-2204:                         [-k | --connection-password-file CONNECTION_PASSWORD_FILE]
    arm-image.ubuntu-2204:                         [--force-handlers] [--flush-cache] [-b]
    arm-image.ubuntu-2204:                         [--become-method BECOME_METHOD]
    arm-image.ubuntu-2204:                         [--become-user BECOME_USER]
    arm-image.ubuntu-2204:                         [-K | --become-password-file BECOME_PASSWORD_FILE]
    arm-image.ubuntu-2204:                         [-t TAGS] [--skip-tags SKIP_TAGS] [-C]
    arm-image.ubuntu-2204:                         [--syntax-check] [-D] [-i INVENTORY] [--list-hosts]
    arm-image.ubuntu-2204:                         [-l SUBSET] [-e EXTRA_VARS] [--vault-id VAULT_IDS]
    arm-image.ubuntu-2204:                         [--ask-vault-password | --vault-password-file VAULT_PASSWORD_FILES]
    arm-image.ubuntu-2204:                         [-f FORKS] [-M MODULE_PATH] [--list-tasks]
    arm-image.ubuntu-2204:                         [--list-tags] [--step] [--start-at-task START_AT_TASK]
    arm-image.ubuntu-2204:                         playbook [playbook ...]
    arm-image.ubuntu-2204: ansible-playbook: error: unrecognized arguments: /home/shanduur/Repos/deployment/images/rpi-ubuntu-22.04/ansible/playbooks/user.yaml

This particular entry in packer template looks like this:

  provisioner "ansible" {
    playbook_file = "ansible/playbooks/user.yaml"

    ansible_env_vars = [
      "ANSIBLE_FORCE_COLOR=1",
      "PYTHONUNBUFFERED=1",
    ]

    extra_arguments = [
      "--connection=chroot",
      "--become-user=root",
      "--extra-vars ansible_host=${build.MountPath}",
      "--extra-vars default_username=${var.default_username}",
      "--extra-vars default_password=${var.default_password}",
    ]
  }