community.general.packer module – Manage HashiCorp Packer builds

Note

This module is part of the community.general collection (version 14.0.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.general. You need further requirements to be able to use this module, see Requirements for details.

To use it in a playbook, specify: community.general.packer.

New in community.general 13.5.0

Synopsis

  • Manage Packer builds and templates.

  • Supports building and initializing Packer templates.

Requirements

The below requirements are needed on the host that executes this module.

  • packer >= 1.7.0

Parameters

Parameter

Comments

cleanup

boolean

Clean up temporary files after build (-cleanup).

Choices:

  • false ← (default)

  • true

color

boolean

Enable colored output if true.

Choices:

  • false ← (default)

  • true

except_builders

aliases: except

list / elements=string

Build all builders except the specified ones (-except).

force

boolean

Force rebuilding (passes -force flag).

Choices:

  • false ← (default)

  • true

log_level

string

Set the log level (--log-level).

Choices:

  • "trace"

  • "debug"

  • "info" ← (default)

  • "warn"

  • "error"

machine_readable

boolean

Generate the output in machine-readable format (-machine-readable if true).

Choices:

  • false ← (default)

  • true

name

string / required

Name of the Packer build configuration (for identification).

only

list / elements=string

Build only the specified builders (-only).

parallel

boolean

Disable parallel building (-parallel=false if false).

Choices:

  • false

  • true ← (default)

state

string / required

Desired state of the Packer build configuration.

Choices:

  • "built": Build the image from template.

    In check mode, the build configuration will be validated.

  • "initialized": Initialize the template.

    This installs required plugins.

template

path / required

Path to the Packer template file or directory.

Required for all states.

var_files

list / elements=path

List of variable files to load (-var-file).

Default: []

variables

dictionary

Dictionary of variables to pass to Packer (-var).

Default: {}

Attributes

Attribute

Support

Description

check_mode

Support: partial

In check mode, state=built runs packer validate instead of building.

Check mode is ignored for state=initialized, so packer init is executed and can install required plugins.

state=initialized always returns changed=true in check mode.

Can run in check_mode and return changed status prediction without modifying target.

diff_mode

Support: none

Returns details on what has changed (or possibly needs changing in check_mode), when in diff mode.

Examples

- name: Initialize Packer template (install plugins)
  community.general.packer:
    name: init-template
    state: initialized
    template: aws-ubuntu.pkr.hcl

- name: Initialize Packer template from directory
  community.general.packer:
    name: init-template-dir
    state: initialized
    template: ./packer-templates/

- name: Build AWS AMI with Packer (or validate in check_mode)
  community.general.packer:
    name: my-ami
    state: built
    template: aws-ubuntu.pkr.hcl
    variables:
      aws_region: us-west-2
      instance_type: t2.micro
    force: false

- name: Build from directory containing multiple templates
  community.general.packer:
    name: multi-template-build
    state: built
    template: ./packer-templates/
    force: false

- name: Validate template using check_mode
  community.general.packer:
    name: validate-check
    state: built
    template: template.pkr.hcl
    var_files:
      - dev.pkrvars.hcl
      - common.pkrvars.hcl
  check_mode: true

- name: Build with force rebuild
  community.general.packer:
    name: force-build
    state: built
    template: template.pkr.hcl
    force: true
    parallel: false
    machine_readable: true

- name: Build only specific builders
  community.general.packer:
    name: selective-build
    state: built
    template: template.pkr.hcl
    only:
      - amazon-ebs.builder1
      - virtualbox-iso.builder2

- name: Build local artifact (VirtualBox)
  community.general.packer:
    name: local-build
    state: built
    template: virtualbox.pkr.hcl
    force: true

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

artifacts

list / elements=dictionary

List of created artifacts.

Returned: when state=built and build successful

Sample: [{"build_index": 0, "name": "ami-12345678", "type": "amazon-ebs"}]

artifacts_count

integer

Number of artifacts created.

Returned: when state=built and build successful

Sample: 1

build_start_timestamp

string

Timestamp when the build started (UTC).

Returned: when state=built

Sample: "2024-01-15T10:30:00Z"

cmd

list / elements=string

Full command line executed as a list.

Returned: always

Sample: ["/usr/local/bin/packer", "build", "template.pkr.hcl"]

packer_version

string

Packer version used.

Returned: always

Sample: "1.9.4"

Authors

  • Aleksandr Gabidullin (@a-gabidullin)