hashicorp / packer-plugin-ansible

Packer plugin for Ansible Provisioner

Home Page:https://www.packer.io/docs/provisioners/ansible

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: unrecognized arguments: playbook.yml

shanduur opened this issue · comments

Overview of the Issue

While trying to build image with packer, at Ansible provisioner step, the following log is created, and packer exits with RC=1:

==> arm-image.ubuntu-2204: Executing Ansible: ansible-playbook -e packer_build_name="ubuntu-2204" -e packer_builder_type=arm-image -vvvvv --connection=chroot --become-user=root --extra-vars ansible_host=/tmp/armimg-2292410357 --extra-vars default_username=admin --extra-vars default_password=***** -e ansible_ssh_private_key_file=/tmp/ansible-key1021651395 -i /tmp/packer-provisioner-ansible802100546 /home/shanduur/Repos/deployment/images/rpi-ubuntu-22.04/ansible/playbooks/user.yml
.
.
.
    arm-image.ubuntu-2204: ansible-playbook: error: unrecognized arguments: /home/shanduur/Repos/deployment/images/rpi-ubuntu-22.04/ansible/playbooks/user.yml

Reproduction Steps

Create the following folder structure:

.
├── Makefile
├── rpi-ubuntu-22.04
│   ├── ansible
│   │   └── playbooks
│   │       └── user.yml
│   └── img.pkr.hcl
└── variables.pkrvars.hcl

Paste the content of the files below to the appropriate files.
Run make rpi-ubuntu-22.04

Plugin and Packer version

From packer version

  • Packer v1.8.5
  • github.com/hashicorp/ansible v1.0.3
  • github.com/solo-io/packer-plugin-arm-image @ main (manual install)
  • ansible-playbook [core 2.14.1]

Simplified Packer Buildfile

packer {
  required_plugins {
    ansible = {
      version = ">= 1.0.2"
      source  = "github.com/hashicorp/ansible"
    }
  }
}

variable "default_username" {
  type    = string
  default = "admin"
}

variable "default_password" {
  type    = string
  default = "Ch@ngeme1"
}

source "arm-image" "ubuntu-2204" {
  iso_url = "https://cdimage.ubuntu.com/releases/22.04.1/release/ubuntu-22.04.1-preinstalled-server-arm64+raspi.img.xz"
  iso_checksum = "5d0661eef1a0b89358159f3849c8f291be2305e5fe85b7a16811719e6e8ad5d1"
  target_image_size = "${var.target_image_size}"
}

build {
  sources = ["source.arm-image.ubuntu-2204"]

  provisioner "file" {
    source = "templates/"
    destination = "/apps"
  }

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

    ansible_env_vars = [
      "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}"
    ]
  }
}

Playbook file, located at ansible/playbooks/user.yml:

- hosts: all
  tasks:
    - name: Ensure that 'default_username' is defined
      fail: msg="Bailing out. This play requires 'default_username'"
      when: default_username is undefined

    - name: Ensure that 'default_password' is defined
      fail: msg="Bailing out. This play requires 'default_password'"
      when: default_password is undefined

    - name: Delete default "ubuntu" user
      ansible.builtin.user:
        name: ubuntu
        state: absent
        remove: true
        force: true

    - name: Create new user
      ansible.builtin.user:
        name: '{{ default_username }}'
        password: '{{ default_password }}'

Simplified Makefile:

PKR_VARIABLES=../variables.pkrvars.hcl
PKR_CONFIG=img.pkr.hcl

.PHONY: rpi-ubuntu-22.04
rpi-ubuntu-22.04:
	TARGET=rpi-ubuntu-22.04 make img

.PHONY: img
img:
	cd ${TARGET} && \
	PACKER_LOG=1 sudo -E packer init ${PKR_CONFIG} && \
	PACKER_LOG=1 sudo -E packer build \
		-var-file ${PKR_VARIABLES} \
		${PKR_CONFIG}

Operating system and Environment details

$ uname -a
Linux rocky.dev 4.18.0-372.19.1.el8_6.x86_64 #1 SMP Tue Aug 2 16:19:42 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/os-release
NAME="Rocky Linux"
VERSION="8.5 (Green Obsidian)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.5"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Rocky Linux 8.5 (Green Obsidian)"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:rocky:rocky:8:GA"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
ROCKY_SUPPORT_PRODUCT="Rocky Linux"
ROCKY_SUPPORT_PRODUCT_VERSION="8"
$ ansible --version
ansible [core 2.14.1]
  config file = None
  configured module search path = ['/home/shanduur/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/shanduur/.local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/shanduur/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.9.6 (default, Nov  9 2021, 13:31:27) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] (/bin/python3)
  jinja version = 3.1.2
  libyaml = True

Log Fragments and crash.log files

packer.log

Hi @shanduur,

Out of curiosity, are you using a custom version of the ansible plugin? I have version 1.0.3 on my test setup, and it looks like the -vvvvv is not part of the produced command-line (as expected since I don't see us adding it to the generated command).

Aside from this curiosity, I do experience the same behaviour as you, I'll continue looking into this, and hopefully figure out a fix or workaround for this use case.

Thanks for bringing this up to us!

Re-reading your template, the extra_arguments section has arguments like the following: "--extra-vars ansible_host=${build.MountPath}", as per the documentation for the plugin (SEE: https://developer.hashicorp.com/packer/plugins/provisioners/ansible/ansible#extra_arguments), these are supposed to be declared as separate arguments in the array, like:

extra_args [
    "--extra-vars", "ansible_host=${build.MountPath}"
]

Doing this in your template does not yield the error that you experienced, I still have some, but they are linked to the SSH configuration, so it's not related to this problem at least.

If you try this, let me know if you still experience this problem.

Yes, this worked!

-vvv - I wanted to get more verbose output, this was one of my "trials".

Good to hear!

If your problem's solved then, I'll go on and close this issue, feel free to reopen if you still experience some issue related to this.