且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-26 08:58:01

你可以这样使用:

---- 主持人:所有收集事实:没有变成:是任务:- 名称:安装 git 包易于:名称: git- 名称:从 git 存储库获取更新的文件吉特:回购:"https://{{ githubuser |urlencode }}:{{ github密码 |urlencode }}@github.com/privrepo.git"目标:/tmp

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

然后执行以下剧本:

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

注意:确保将凭据放入 ansible 保管库或传递它安全方式

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.

You can use something like this:

---
- 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

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

Then execute the following playbook:

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

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