Search code examples
ansiblegithub-actionsansible-inventory

How to run ansible with aws dynamic inventory directly via GitHub Actions on ubuntu-latest?


I've tryied with the following steps:

  1. configured workflow in GitHub action:
name: Terraform-ansible-apply

on:
  workflow_dispatch:

jobs:
  Terraform:
    name: Terraform Plan & Apply
    runs-on: ubuntu-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

    steps:
      - name: Checkout Repo
        uses: actions/checkout@v2

      - name: Terraform Setup
        uses: hashicorp/setup-terraform@v1
        with: 
          terraform_wrapper: false

      - name: Terraform Init
        run: terraform init
        working-directory: ./Terraform

      - name: Terraform Validate
        run: terraform validate
        working-directory: ./Terraform

      - name: Terraform Apply
        id: tf-apply
        run: terraform apply -auto-approve
        working-directory: ./Terraform

################
  
      - name: install
        continue-on-error: true
        run: |
          pipx install boto3 --include-deps
          pipx install botocore --include-deps
      
      - name: Run Ansible playbook
        run: |
          ansible --version
          ansible-galaxy collection list
          ansible-inventory -i aws_ec2.yaml --graph
        working-directory: ./Ansible

but I get the following error:

Warning: :  * Failed to parse /home/runner/work/AWS-project/AWS-
project/Ansible/aws_ec2.yaml with
ansible_collections.amazon.aws.plugins.inventory.aws_ec2 plugin: Failed to
import the required Python library (botocore and boto3) on fv-az613-985's
Python /opt/pipx/venvs/ansible-core/bin/python. Please read the module
documentation and install it in the appropriate location. If the required
library is installed, but Ansible is using the wrong Python interpreter, please
consult the documentation on ansible_python_interpreter
Warning: : Unable to parse /home/runner/work/AWS-project/AWS-
project/Ansible/aws_ec2.yaml as an inventory source
Warning: : No inventory was parsed, only implicit localhost is available
@all:
  |--@ungrouped:

In ansible.cfg I have the following enabled:

host_key_checking = False
remote_user=ubuntu
become=True
enable_plugins = aws_ec2
host_key_checking=False

Of course aws credentials are provided correctly via GitHub secrets. Terraform code work perfect, the issue is with running ansible.

**Can you please assist what is missing? Or how it should be configured so ansible could run aws dynamic inventory **

ansible --version
  
ansible [core 2.15.1]
  config file = /home/runner/work/AWS-project/AWS-project/Ansible/ansible.cfg
  configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/pipx/venvs/ansible-core/lib/python3.10/site-packages/ansible
  ansible collection location = /home/runner/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/pipx_bin/ansible
  python version = 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] (/opt/pipx/venvs/ansible-core/bin/python)
  jinja version = 3.1.2
  libyaml = True

Plugin file aws_ec2.yaml is the following:

plugin: aws_ec2
regions:
  - eu-central-1

amazon.aws collection is installed


Solution

  • I fixed the issue by adding the following step to my job:

      - name: install additional dependencies for ansible which runs in venv
        run: |
          source /opt/pipx/venvs/ansible-core/bin/activate
          python -m pip install boto3