如何直接通过 ubuntu-latest 上的 GitHub Actions 运行带有 aws 动态库存的 ansible?

问题描述 投票:0回答:2

我尝试了以下步骤:

  1. 在 GitHub 操作中配置的工作流程:
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

但我收到以下错误:

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:

在 ansible.cfg 中我启用了以下功能:

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

当然,AWS 凭证是通过 GitHub 机密正确提供的。 Terraform 代码工作完美,问题在于运行 ansible。

**您能帮忙缺少什么吗?或者应该如何配置,以便 ansible 可以运行 aws 动态清单 **

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

插件文件 aws_ec2.yaml 如下:

plugin: aws_ec2
regions:
  - eu-central-1

amazon.aws 集合已安装

ansible github-actions ansible-inventory
2个回答
1
投票

问题是您正在尝试使用

pipx
安装依赖项。这对您没有任何好处 -
pipx
将每个模块安装到一个隔离的环境中,其他任何东西都看不到它。

替换这个:

- name: install
  continue-on-error: true
  run: |
    pipx install boto3 --include-deps
    pipx install botocore --include-deps

与:

- name: install
  run: |
    pip install boto3 botocore

(请注意,我故意删除了

continue-on-error: true
,因为在这种情况下继续出现错误没有任何意义——如果未安装依赖项,您对 Ansible 的使用将会失败。)


0
投票

我通过在工作中添加以下步骤解决了该问题:

  - name: install additional dependencies for ansible which runs in venv
    run: |
      source /opt/pipx/venvs/ansible-core/bin/activate
      python -m pip install boto3
© www.soinside.com 2019 - 2024. All rights reserved.