【问题标题】:How can I use a gitlab deploy token from ansible?如何使用来自 ansible 的 gitlab 部署令牌?
【发布时间】:2023-04-06 08:17:01
【问题描述】:

我的公司有一个自托管的 GitLab 服务器。 我正在编写一个 Ansible 剧本,它使用我们 GitLab 服务器中的多个项目来配置服务器。

为了让目标服务器自动(非交互式)克隆项目,我使用了部署令牌。

首先,在我们的 GitLab 服务器的 Web 界面上,我进入了我的第一个项目并为它生成了一个部署密钥。我可以使用 ansible 将目标服务器配置为使用部署密钥,如下所示:

- name: Have git store credentials on disk
  community.general.git_config:
    name: credential.helper
    scope: global
    value: store

- name: Add credentials for project A
  copy:
      dest: /root/.git-credentials
      content: "https://{{ gitlab_project_A_deploy_username }}:{{ gitlab_project_A_deploy_password }}@company_gitlab_server.com"

- name: Clone Project A git repo
  git:
    repo: 'https://company_gitlab_server.com/USER/Project_A.git'
    dest: /some/dir/

我在 ansible 保险库中设置了 gitlab_project_A_deploy_usernamegitlab_project_A_deploy_password。这很好用,除了在/root/.git-credentials 中,凭据不是特定于 git-repo 的 - 它们是特定于服务器的。

当我获得项目 B 的部署密钥时,它具有完全不同的用户名和密码,但服务器是相同的。即使我将两组凭据都添加到 /root/.git-credentialsgit ansible 模块,甚至只是常规的 git 二进制文件,在克隆项目时也不知道使用哪个凭据。

有没有办法以某种方式为一个项目指定一个部署密钥,为不同的项目指定一个不同的部署密钥?

【问题讨论】:

    标签: git ansible gitlab


    【解决方案1】:

    为什么不直接将凭据设置到 repo 路径中?

    - name: Clone Project A git repo
      git:
        repo: 'https://{{ gitlab_project_A_deploy_username }}:{{ gitlab_project_A_deploy_password }}@company_gitlab_server.com/USER/Project_A.git'
        dest: /some/dir/
    

    【讨论】:

      【解决方案2】:

      如果您想对凭据使用帮助程序(无论是store 还是cache),您也可以使用credential.useHttpPath 选项使其与路径匹配。

      然后,对于 store 助手,保存

      https://{{ gitlab_project_A_deploy_username }}:{{ gitlab_project_A_deploy_password }}@company_gitlab_server.com/USER/Project_A.git
      https://{{ gitlab_project_B_deploy_username }}:{{ gitlab_project_B_deploy_password }}@company_gitlab_server.com/USER/Project_B.git
      

      在凭据存储中。

      【讨论】:

        猜你喜欢
        • 2022-12-11
        • 1970-01-01
        • 2019-09-20
        • 1970-01-01
        • 2021-10-17
        • 1970-01-01
        • 2021-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多