使用 Ansible Git 模块时如何传递用户名和密码?
How do I pass username and password while using Ansible Git module?
在使用 Ansible 的 Git 模块克隆、推送或拉取内部托管的私有 git 存储库(例如在 GitLab 实例上)时,我如何指定用户名和使用 Git 服务器进行身份验证的密码?
我在 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 }}
,如果你的密码还包含@、#、$等特殊字符
然后执行以下剧本:
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
改进 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
[...]
并在任务中引用变量。
task.yml
- name: Get updated files from git repository
git:
repo=https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/privrepo.git
dest=/tmp
这会将 .git/config
中的密码以明文形式保存为 remote "origin"
的 url
。
可以使用以下任务将其删除。
- name: Ensure remote URL does not contain credentials
git_config:
name: remote.origin.url
value: https://github.com/privrepo.git
scope: local
repo: /tmp
取自:Clone a private git repository with Ansible (using password prompt)
虽然 and 答案是正确的,但有一个重要的警告:使用 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
这里的所有答案都使得将 username/password 泄漏到日志或错误消息中有点太容易了,这似乎是不可取的,即使在我的情况下它是只读部署令牌。
这是一个替代方案:
- 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
模板如下所示:
https://{{ gitlab_username|urlencode }}:{{ gitlab_password|urlencode }}@gitlab.example.org
在使用 Ansible 的 Git 模块克隆、推送或拉取内部托管的私有 git 存储库(例如在 GitLab 实例上)时,我如何指定用户名和使用 Git 服务器进行身份验证的密码?
我在 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 }}
,如果你的密码还包含@、#、$等特殊字符
然后执行以下剧本:
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
改进 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
[...]
并在任务中引用变量。
task.yml
- name: Get updated files from git repository
git:
repo=https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/privrepo.git
dest=/tmp
这会将 .git/config
中的密码以明文形式保存为 remote "origin"
的 url
。
可以使用以下任务将其删除。
- name: Ensure remote URL does not contain credentials
git_config:
name: remote.origin.url
value: https://github.com/privrepo.git
scope: local
repo: /tmp
取自:Clone a private git repository with Ansible (using password prompt)
虽然 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
这里的所有答案都使得将 username/password 泄漏到日志或错误消息中有点太容易了,这似乎是不可取的,即使在我的情况下它是只读部署令牌。
这是一个替代方案:
- 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
模板如下所示:
https://{{ gitlab_username|urlencode }}:{{ gitlab_password|urlencode }}@gitlab.example.org