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

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

问题描述:

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

While doing clone, push or pull of a private git repository hosted internally (e.g. on a GitLab instance) with Ansible's Git module, how do I specify username and password to authenticate with the Git server?

文档中,我看不到任何实现此目的的方法.

I don't see any way to do this in the documentation.

您可以使用以下内容:

---
- 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 | urlencode }}@github.com/privrepo.git"
        dest: /tmp

注意::如果您的密码还包含特殊字符@,#,$等,则在此处使用{{ githubpassword | urlencode }}

Note: {{ githubpassword | urlencode }} is used here, if your password also contains special characters @,#,$ etc

然后执行以下剧本:

ansible-playbook -i hosts github.yml -e "githubuser=arbabname" -e "githubpassword=xxxxxxx"

注意:请确保将凭据放入ansible保管库中或通过它 安全方式

Note: Make sure you put the credentials in ansible vaults or pass it secure way