如何在使用Ansible Git模块时传递用户名和密码?

kar*_*vee 28 ansible ansible-playbook ansible-2.x

在使用Ansible的Git模块进行内部托管的私有git存储库(例如,在GitLab实例上)的克隆,推送或拉取时,如何指定用户名和密码以通过Git服务器进行身份验证?

我在文档中看不到任何方法.

Arb*_*zar 43

你可以使用这样的东西:

---
- hosts: all 
  gather_facts: no
  become: yes
  tasks:
    - name: install git package
      apt:
        name: git

    - name: Get updated files from git repository 
      git: 
        repo: "https://{{ githubuser | urlencode }}:{{ githubpassword }}@github.com/privrepo.git"
        dest: /tmp
Run Code Online (Sandbox Code Playgroud)

注意:如果您的密码还包含特殊字符@,#,$ etc,那么也使用urlencode密码: {{ githubpassword | urlencode }}

然后执行以下playbook:

ansible-playbook -i hosts github.yml -e "githubuser=arbabname" -e "githubpassword=xxxxxxx"
Run Code Online (Sandbox Code Playgroud)

  • 如果有某些特殊字符,例如密码中的`!`,则不起作用. (2认同)
  • @StockB我已经更新了答案,请立即使用`urlencode`过滤器进行检查 (2认同)
  • 顺便说一句,您可以生成令牌并使用它代替密码。您可以创建没有管理员权限的令牌,并且可以随时将其吊销。令牌永远不会包含您需要进行urlencode的符号。 (2认同)
  • @Vadym,你是对的,但这里提问者询问他/她如何传递用户名/密码而不是令牌。谢谢 (2认同)

F. *_*ago 23

改善Arbab Nazar的回答,您可以通过提示输入凭据来避免在终端中泄露您的密码.

playbook.yml

--- 
- name: ANSIBLE - Shop Installation 
  hosts: '{{ target }}' 

  vars_prompt: 
    - name: "githubuser" 
      prompt: "Enter your github username" 
      private: no 
    - name: "githubpassword" 
      prompt: "Enter your github password" 
      private: yes 

  [...] 
Run Code Online (Sandbox Code Playgroud)

并在任务中引用变量.

task.yml

- name: Get updated files from git repository 
  git:
    repo=https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/privrepo.git
    dest=/tmp
Run Code Online (Sandbox Code Playgroud)

取自:使用Ansible克隆私人git存储库(使用密码提示)

  • 这也是公开用户名/密码(远程源存储为`https:// user:password@github.com/...`) (4认同)
  • 您无法避免暴露令牌或密码。Git 将其写入 .git/config。在此处查看更多详细信息,https://help.github.com/en/articles/git-automation-with-oauth-tokens。 (3认同)

Mar*_*ein 11

虽然ArbabF. Santiago 的答案是正确的,但有一个重要的警告:https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/privrepo.git作为结帐 URL,Git 会将您的密码以明文形式存储在.git/文件夹中。这已经在评论中提到了,但我认为它值得更多关注。您可能想要取消 Git 模块并使用原始 Git,例如:

vars_prompt: 
  - name: "githubuser" 
    prompt: "Enter your github username" 
    private: no 
  - name: "githubpassword" 
    prompt: "Enter your github password" 
    private: yes
tasks:
  - name: Git clone
    expect:
      command: git clone https://github.com/privrepo.git /tmp
      responses:
        Username: "{{ githubuser }}" # Username is a regex
        Password: "{{ githubpassword }}" # Password is a regex
    no_log: true
Run Code Online (Sandbox Code Playgroud)


Chr*_*ams 5

这里的所有答案都使得将用户名/密码泄漏到日志或错误消息中变得有点太容易了,即使在我的情况下它是只读部署令牌,这似乎也是不可取的。

这是一个替代方案:

    - name: Configure Git credential storage
      command: "git config --global credential.helper store"
    - name: Populate the Git credential store
      template:
        src: files/git_credentials.j2
        dest: /home/appuser/.git-credentials
        owner: appuser
        group: appuser
        mode: u=rw,g=,o=
      no_log: true
Run Code Online (Sandbox Code Playgroud)

模板如下所示:

https://{{ gitlab_username|urlencode }}:{{ gitlab_password|urlencode }}@gitlab.example.org
Run Code Online (Sandbox Code Playgroud)