How can i push locally created branch on a repository to origin via Ansible

4,369 views
Skip to first unread message

ishan jain

unread,
Oct 4, 2016, 11:21:37 AM10/4/16
to Ansible Project
Seems like there is isn't any module in Ansible which can commit back to a GIT repo. My case is such that i am allowing the user to create a new branch in any of our several repositories and with the help of templates, generate some necessary files, all this via ansible. I am able to clone the repo (even a branch/tag) via GIT module, create a new branch with shell module, populate it with templates, but now i need to push the new branch and its contents and this is where i am stuck. GIT push would prompt for username and password which is something i have in the variables but don't know how to use in Ansible shell command. Any ideas how to proceed here ?

Alexander H. Laughlin

unread,
Oct 4, 2016, 4:51:34 PM10/4/16
to Ansible Project
I think something like the below would work to solve your problem with the variable interpolation.

- name: Push git commits with local variables.

  hosts: localhost

  vars:

    username: git_username

    password: git_password

  tasks:

    - name: Set origin to include username and password.

      shell: "git remote set-url origin https://{{ git_username }}:{{ git_password }}@yourhost.com/storage/repo.git"

    - name: Push to origin.

      shell: "git push origin branch"


That is, if you can safely transmit the passwords via https. If you're using http this would be incredibly unsafe. 

You could also look at setting the credential.helper variable in git (`git config --global credential.helper store`), but that's beyond the scope of this mailing list.

ishan jain

unread,
Oct 5, 2016, 4:29:17 AM10/5/16
to Ansible Project
I found a single GIT command to do that.

git push origin <tag/branch> --repo https://username:pa...@repourl.com
Reply all
Reply to author
Forward
0 new messages