setup-packer icon indicating copy to clipboard operation
setup-packer copied to clipboard

Ansible provisioner does not work and no artifacts are created.

Open MiyamotoTa opened this issue 3 years ago • 16 comments

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

MiyamotoTa avatar Apr 19 '21 21:04 MiyamotoTa

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?

ksatirli avatar Apr 20 '21 11:04 ksatirli

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.

MiyamotoTa avatar Apr 20 '21 20:04 MiyamotoTa

@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.

jw-maynard avatar May 05 '21 19:05 jw-maynard

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 avatar May 06 '21 09:05 oba11

@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.

jw-maynard avatar May 06 '21 19:05 jw-maynard

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?

2pai avatar Jun 07 '21 01:06 2pai

That was my experience.

jw-maynard avatar Jun 07 '21 15:06 jw-maynard

@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.

jw-maynard avatar Jun 17 '21 22:06 jw-maynard

Hi, any updates on this issue ?

McPatate avatar Nov 17 '21 14:11 McPatate

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

2pai avatar Nov 17 '21 14:11 2pai

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'

McPatate avatar Nov 18 '21 07:11 McPatate

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 :)

james-green-affinity avatar Apr 06 '22 23:04 james-green-affinity

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 Screenshot 2022-05-16 at 11 42 34 AM

pratapaprasanna avatar May 16 '22 06:05 pratapaprasanna

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

AkashSivakumar-Dev avatar Jun 27 '22 10:06 AkashSivakumar-Dev

Hi!

I create this PR with the fix and include others https://github.com/hashicorp/packer-github-actions/pull/52

jveraduran avatar Jul 30 '22 23:07 jveraduran

this issue is not solved yet :)

joaoluiznaufel avatar Sep 28 '22 20:09 joaoluiznaufel

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.

jw-maynard avatar Nov 11 '22 00:11 jw-maynard