Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pushd in packer_validate hook optional #51

Open
pdecat opened this issue Dec 13, 2024 · 1 comment Β· May be fixed by #52
Open

Make pushd in packer_validate hook optional #51

pdecat opened this issue Dec 13, 2024 · 1 comment Β· May be fixed by #52
Assignees

Comments

@pdecat
Copy link

pdecat commented Dec 13, 2024

πŸ› Summary

Since #47, the current working directory is changed to the location of each HCL template.

This breaks setups where HCL templates are in subdirectories and relatively include ansible playbooks, e.g.:

.
β”œβ”€β”€ Makefile
β”œβ”€β”€ ansible
β”‚Β Β  β”œβ”€β”€ ansible.cfg
β”‚Β Β  β”œβ”€β”€ group_vars
β”‚Β Β  β”‚Β Β  └── all.yml
β”‚Β Β  β”œβ”€β”€ playbook.yml
β”‚Β Β  └── requirements.yml
β”œβ”€β”€ packer
β”‚Β Β  └── main.pkr.hcl
β”œβ”€β”€ renovate.json
└── requirements.txt

With packer/main.pkr.hcl containing:

build {
  provisioner "ansible" {
    playbook_file   = "./ansible/playbook.yml"
  }
}

And .pre-commit-config.yaml:

repos:
  - repo: https://github.com/cisagov/pre-commit-packer
    rev: v0.3.0
    hooks:
      - id: packer_validate
      - id: packer_fmt

As I understand the motivation behind the change, e.g. for monorepos containing multiple independent packer templates, it would be awesome to make this configurable.

To reproduce

Steps to reproduce the behavior:

  1. Create the above setup
  2. Then run pre-commit run --all-files packer_validate

Expected behavior

Hook should pass, just like a normal invocation:

# packer validate packer/main.pkr.hcl
The configuration is valid.

Any helpful log output or screenshots

Paste the results here:

# pre-commit run --all-files packer_validate
Packer Validate..........................................................Failed
- hook id: packer_validate
- exit code: 1

Error: Failed preparing provisioner-block "ansible" ""

  on main.pkr.hcl line 84:
  (source code not available)

1 error(s) occurred:

* playbook_file: ./ansible/playbook.yml is invalid: stat ./ansible/playbook.yml:
no such file or directory



Failed path: packer
================================
@pdecat pdecat linked a pull request Dec 13, 2024 that will close this issue
12 tasks
@pdecat
Copy link
Author

pdecat commented Dec 21, 2024

FWIW, a work-around to make the validation work in all situations is to refer to the playbook relative to path.root, e.g.:

build {
  provisioner "ansible" {
    playbook_file = "${path.root}/../ansible/playbook.yml"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants