why does git clone stall with personal access token?
How do I pass username and password while using Ansible Git module? - Stack Overflow
git - How can I use a gitlab deploy token from ansible? - Stack Overflow
ansible git clone without rights on the remote - Stack Overflow
I have a basic test task to clone from a gitlab repo using the following:
tasks:
- name: Check if Gitlab repo updated
git:
repo: https://my.gitlab.repo/test.git
dest: /var/test-repos/
accept_hostkey: yes
update: yes
version: master
register: git
ignore_errors: true
- debug:
var: gitI've generated a personal access token in gitlab with all read rights + api rights. It runs fine until it attempts to clone then will just stall forever.
If i change the task definition to:
tasks:
- name: Check if Gitlab repo updated
git:
repo: git://my.gitlab.repo/test.git
dest: /var/test-reops/
accept_hostkey: yes
update: yes
version: master
key_file: /path/to/private/key
register: git
ignore_errors: true
- debug:
var: gitThen everything works but I don't want to store the private key on my ansible tower host. Any ideas?
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 | replace ('/', '%2f') }}@github.com/privrepo.git"
dest: /tmp
Note: {{ githubpassword | urlencode | replace ('/', '%2f') }} is used here to account for special characters. replace ('/', '%2f') is necessary because urlencode does not convert /.
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
Improving on Arbab Nazar's answer, you can avoid exposing your password in the terminal by prompting for the credentials.
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
[...]
And in the task reference the variables.
task.yml
- name: Get updated files from git repository
git:
repo=https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/privrepo.git
dest=/tmp
This will save the password as clear text in .git/config as url of remote "origin".
The following task can be used to remove it.
- name: Ensure remote URL does not contain credentials
git_config:
name: remote.origin.url
value: https://github.com/privrepo.git
scope: local
repo: /tmp
Taken from: Clone a private git repository with Ansible (using password prompt)
Why not just set the credentials directly into the repo path?
- name: Clone Project A git repo
git:
repo: 'https://{{ gitlab_project_A_deploy_username }}:{{ gitlab_project_A_deploy_password }}@company_gitlab_server.com/USER/Project_A.git'
dest: /some/dir/
If you want to use a helper for the credentials (whether store or cache) you can use the credential.useHttpPath option to make it match on the path too.
Then, for the store helper, save
https://{{ gitlab_project_A_deploy_username }}:{{ gitlab_project_A_deploy_password }}@company_gitlab_server.com/USER/Project_A.git
https://{{ gitlab_project_B_deploy_username }}:{{ gitlab_project_B_deploy_password }}@company_gitlab_server.com/USER/Project_B.git
in the credentials store.
Following the comment from β.εηοιτ.βε I found how to make this git statement work in ansible.
First this has been to create an specific ssh key to be used as a deployment key:
ssh-keygen -t ed25519 -C "ansible deploy key for gitlab"
With password and the password stored in the ansible-vault (as gitlab_deploy_key_passwd). The file is saved in the path of the roles in ansible.
In the gitlab project, one has to go the "Settings > Repository > Deploy Keys" and place the content of the "gitlab_deploy_key_ed25519.pub".
In ansible, it is necessary to have a set of tasks to copy the private key (password protecte) in the remote as well as configure ssh to use it when talk with gitlab.
- name: "gitlab deploy key"
block:
- name: ".ssh directory with the best rights"
file:
path: /home/{{ ansible_user_id }}/.ssh
state: directory
mode: "u=rwx,g=,o="
owner: "{{ ansible_user_id }}"
group: "{{ ansible_user_id }}"
- name: "gitlab deploy key copy"
copy:
src: "../files/gitlab_deploy_key_ed25519"
dest: "/home/{{ ansible_user_id }}/.ssh"
mode: "u=rwx,g=,o="
owner: "{{ ansible_user_id }}"
group: "{{ ansible_user_id }}"
- name: "gitlab deploy key config"
blockinfile:
path: "/home/{{ ansible_user_id }}/.ssh/config"
block: |
Host gitlab.com
User git
Hostname gitlab.com
IdentityFile /home/{{ ansible_user_id }}/.ssh/gitlab_deploy_key_ed25519
With this a "git clone" task can be setup:
- name: "git clone"
expect:
chdir: "{{ sources_dir }}"
command: "git clone [email protected]:(...)/repo.git repo.git"
responses:
passphrase: "{{ gitlab_deploy_key_passwd }}"
So then, the repo is present in the remote without storing any private information there (or at least store it somehow protected). It is read-only and even this read requires a password that is as save as the ansible-vault. (I haven't understand why in this case the expect worked with the passphrase but didn't with the username/password).
This clones the repo without initializing the submodules, but from here the git commands in further ansible tasks will have access to the necessary things to do that.
To add to the comment, you have here an example using a credential helper:
- 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
With template;
https://{{ gitlab_username|urlencode }}:{{ gitlab_password|urlencode }}@gitlab.example.org
You can also use the git_config_module to set the credential helper (instead of command)