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)
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存储库(使用密码提示)
Mar*_*ein 11
虽然Arbab和F. 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)
这里的所有答案都使得将用户名/密码泄漏到日志或错误消息中变得有点太容易了,即使在我的情况下它是只读部署令牌,这似乎也是不可取的。
这是一个替代方案:
- 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)