gpt4 book ai didi

docker - 当我们想通过github操作执行ansible命令时如何管理ssh key 文件

转载 作者:行者123 更新时间:2023-12-02 19:01:47 24 4
gpt4 key购买 nike

我有一个github存储库,一个docker存储库和一个Amazon ec2实例。我正在尝试使用这些工具创建CI / CD管道。这个想法是当对github仓库主分支的推送发生时,将docker容器部署到ec2实例。我已经使用github操作来构建代码,构建docker镜像并将docker镜像推送到docker hub。现在,我想将最新镜像从docker hub拉到远程ec2实例并运行相同的镜像。为此,我试图从github操作执行ansible命令。但是我需要指定.pem文件作为ansible命令的参数。我试图将.pem文件保留在github secret 中,但是没有用。我真的很困惑如何进行此操作。
这是我的github工作流程文件

name: helloworld_cicd
on:
push:
branches:
- master
jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Go Build
run: go build

- name: Docker build
run: docker build -t helloworld .

- name: Docker login
run: docker login --username=${{ secrets.docker_username }} --password=${{ secrets.docker_password }}

- name: Docker tag
run: docker tag helloworld vijinvv/helloworld:latest

- name: Docker push
run: docker push vijinvv/helloworld:latest

我试图运行类似
ansible all -i '3.15.152.219,' --private-key ${{ secrets.ssh_key }} -m rest of the command

但这没用。解决此问题的最佳方法是什么

最佳答案

我猜您所说的“它没有用”是指ansible期望私钥是一个文件,而您正在提供一个字符串。

github上的page演示了如何在github上使用 secret 文件。与您的情况相对应的是执行以下步骤:

  • gpg --symmetric --cipher-algo AES256 my_private_key.pem
  • 选择一个强密码短语并将此密码短语保存为github secret 中的 secret 。称之为LARGE_SECRET_PASSPHRASE
  • 在git
  • 中提交加密的 my_private_key.pem.gpg
  • 在操作中创建一个解密该文件的步骤。它可能看起来像:
  • - name: Decrypt Pem
    run: gpg --quiet --batch --yes --decrypt --passphrase="$LARGE_SECRET_PASSPHRASE" --output $HOME/secrets/my_private_key.pem my_private_key.pem.gpg
    env:
    LARGE_SECRET_PASSPHRASE: ${{ secrets.LARGE_SECRET_PASSPHRASE }}
  • 最后,您可以使用ansible运行ansible all -i '3.15.152.219,' --private-key $HOME/secrets/my_private_key.pem命令
  • 关于docker - 当我们想通过github操作执行ansible命令时如何管理ssh key 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58955637/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com