setup-packer
setup-packer copied to clipboard
Ansible provisioner does not work and no artifacts are created.
Expected Behavior
Able to build AMI using the ansible provisioner.
Current Behavior
Ansible provisioner does not work, and no artifacts are created.
2021/04/19 01:27:01 packer-provisioner-shell plugin: Serving a plugin connection...
on example.pkr.hcl line 46:
Error: Failed preparing provisioner-block "ansible" ""
(source code not available)
1 error(s) occurred:
* Error running "ansible-playbook --version": exec: "ansible-playbook":
on example.pkr.hcl line 46:
executable file not found in $PATH
(source code not available)
1 error(s) occurred:
2021/04/19 01:27:01 Build debug mode: false
* Error running "ansible-playbook --version": exec: "ansible-playbook":
2021/04/19 01:27:01 Force build: false
executable file not found in $PATH
2021/04/19 01:27:01 On error: abort
2021/04/19 01:27:01 Waiting on builds to complete...
==> Wait completed after 4 microseconds
==> Wait completed after 4 microseconds
==> Builds finished but no artifacts were created.
==> Builds finished but no artifacts were created.
Steps to Reproduce
The build that is executed on GitHubActions using the Ansible provider always fails.
Environment
1.) Link to a Gist of your Workflow configuration: https://gist.github.com/MiyamotoTa/705b18449165f7472f42382adfe4807c
2.) Any other relevant environment information:
Running the same code on a local machine can build the AMI. Local packer version: v1.7.2
Hi @MiyamotoTa, thanks for reporting this!
Could you share part of your Packer image configuration?
It looks like you're using the Shell provisioner, rather than the Ansible (local) provisioner (see here) - any specific reason for this?
Hi, @ksatirli
I uploaded the example.pkr.hcl file to gist. Let me know if I can give you any other information.
The reason for using the Shell provisioner is not really necessary, as the packer quick start was still there. However, the same error occurred when I removed it for testing purposes and used only the Ansible provisioned.
@ksatirli I've always understood that you need to install Ansible on the machine you're running packer on to use the ansible provisioner. It is not installed in either the base packer docker image or the docker image created for this action therefor it doesn not exist in in the container hence the executable file not found in $PATH
error. I imagine there are other provisioners that fall into the same bucket (software must be installed).
In addition, there are many ansible modules that require additional python libraries to be pre-installed on the machine running the ansible playbook. This is another complication.
I'm having the same issue even though I'm installing the pip package from GitHub action
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install ansible
run: pip install ansible
- name: Append binary PATH
run: echo "$(dirname $(which ansible))" >> $GITHUB_PATH
- name: Build Artifact
uses: hashicorp/packer-github-actions@master
with:
command: build
@oba11 The packer GitHub action runs inside a docker container that runs inside the GitHub Action runner instance. You're workflow is installing Ansible in the runner instance but since packer runs inside Docker inside your runner packer cannot access your Ansible installation. I worked around this myself by creating my own Docker based action inside our packer repo and using the code in this repo as a base and then editing the Dockerfile to also install ansible and any libs we need. I'm not sure how else you would be able to work around this limitation.
I'm having the same issue.
@jw-maynard so the issue was the GitHub actions runner images (hashicorp/packer:light
) didn't have an ansible on it? so if we want to use it, we have to modify the Dockerfile to with an image that includes ansible on it?
That was my experience.
@ksatirli I'm curious is there a reason the packer team is going the route of running packer inside a container instead of creating an action similar to the terraform-setup
action which sets up terraform on the runner. This would seem to me to make supporting provisioners other than shell scripts a lot easier as the requirement to ensure any extra executables are available would be on the workflow author and not the packer action itself.
Hi, any updates on this issue ?
Hi, any updates on this issue?
I've been solving this issue by updating the docker images, you can use the forked version here https://github.com/pintu-crypto/packer-github-actions
the changes of the docker images can be checked here https://github.com/2pai/docker-hub-images/commit/a5331287a32289764fcf6a7ec88d80f894bdde91 or if you want to build the image by yourself
Ah ! Saw your reply too late :)
I ended up doing this :
- run: 'sudo apt-get install -y ansible'
shell: bash
# NB: will only work with ubuntu 20.04
- uses: myci-actions/add-deb-repo@10
with:
repo: deb [arch=amd64] https://apt.releases.hashicorp.com focal main
keys-asc: https://apt.releases.hashicorp.com/gpg
update: true
install: packer
- name: Validate Template
uses: hashicorp/packer-github-actions@master
with:
command: validate
arguments: -syntax-only
target: infra/packer
- run: 'packer init .'
- run: 'packer build somefile.pkr.hcl'
Hi, any updates on this issue?
I've been solving this issue by updating the docker images, you can use the forked version here https://github.com/pintu-crypto/packer-github-actions
the changes of the docker images can be checked here 2pai/docker-hub-images@a533128 or if you want to build the image by yourself
Thanks for this. I am rolling with my own, but your Fork gave me the inspiration I needed :)
Hi all,
I figured out a work-around. It worked for me I am not sure. How far it will help the rest.
This is the github-actions i created for my purpose.
---
name: Packer Build
on:
push:
jobs:
packer:
runs-on: ubuntu-latest
name: packer
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-south-1
- name: Install python
uses: actions/setup-python@v3
with:
python-version: '3.8'
- name: Install ansible
run: pip install ansible
- name: Install Packer
run: |
sudo apt-get update; sudo apt-get install -y curl gnupg software-properties-common ;
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - ;
sudo apt-add-repository -y "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" ;
sudo apt-get update && sudo apt-get install -y packer;
- name: Build artifacts
run : |
cd deploy/packer;
packer build -var 'vpc_id=vpc-xxxxxx' -var 'subnet_id=xxxx' template.json.pkr.hcl
I hope it helps
name: PackerBuild
on: push
jobs:
Build:
runs-on: ubuntu-18.04
container: pearlthoughts/packer-ansible:latest
steps:
- uses: actions/checkout@v2
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-south-1
- run: |
cd packer
packer init .
packer build .
name: Run in container
Above I am using a docker image in which the packer and ansible is installed. This method will reduce the execution time compared to installing the packer and ansible in the runner. You can build your own docker image (publicly available) and mention it in the yaml script.
Dockerfile:
FROM alpine as build
RUN apk add packer
FROM alpine:3.13
COPY --from=build /usr/bin/packer /usr/bin/packer
RUN apk add ansible git
Hi!
I create this PR with the fix and include others https://github.com/hashicorp/packer-github-actions/pull/52
this issue is not solved yet :)
Heads up for anyone dealing with this. Packer and Ansible are now included software in the default GitHub actions Ubuntu runners so you should be able to just call packer to complete your builds.
If your using Windows it looks like there is packer but not Ansible so you would need to install that on the runner before running your build.