I am trying to do a git pull/push using ansible. I am running ansible on one server and want to automate or orchestrate a git pull/push on a remote host.

Now since i didn't find a mmodule to do this on ansible doc website, i decided to go the script route using the script module

The problem is ansible hags when it gets to running the git pull called in the script

Anyone know how to run git pull/push using ansible?

Thanks

  • Ansible's just executing git pull as a particular user. Are you able to do that with Ansible's user? Probably needs access to the repo. – ceejayoz Feb 10 '15 at 19:32
  • Whaqt ansible user? whatever command is on the script can be ran on the remote host..also ansible got past git commit command and got stuck in git pull int he script – grant tailor Feb 10 '15 at 19:36
  • 2
    Are you sure it's not hanging on a login screen (https) or password prompt (ssh)? – Mackwerk Feb 10 '15 at 23:02
  • actually..that might be true..will check with that..thanks for pointing that out – grant tailor Feb 11 '15 at 16:41
  • @Mackwerk ok i ran the same script on the remote host and it worked without asking for password since keys are used. .now when the ansible runs the script from a server..it logs in tot he other server with ssh..so it should be able to run the script just as i did manually from the server wihtout any password..but no it still hangs – grant tailor Feb 11 '15 at 17:11
up vote 11 down vote accepted

Ansible's Git Module will do this for you as far as the "pull" is concerned, just make sure that the user that is running the command has key-based access to the git repo.

You can specify the user that the command runs as by adding the "sudo_user" parameter to your task:

- name: Get stuff from git
  git: repo=git@github.com:you/your-git-repo.git dest=/opt/git-stuff
  sudo_user: <your user that has the ssh key>

See https://docs.ansible.com/playbooks_intro.html for more information on using sudo_user.

  • 1
    isn't that going to do a clone? i want pull to get only updates changes..not clone the whole repo all over – grant tailor Feb 11 '15 at 16:40
  • 1
    from docs.ansible.com/git_module.html: update - If no, do not retrieve new revisions from the origin repository (added in Ansible 1.2) My understanding is that if the repo exists already it will update, otherwise it will clone – ProfFalken Feb 11 '15 at 18:30
  • 3
    src appears to be changed to repo now, FYI. – mlissner Sep 15 '16 at 22:34

It should be like:-

tasks: - name: pull from git git: repo: git@gitlab.com:xyz.git dest: /root/Development/abc update: yes version: master

Note: Here the remote user is root

  • You're basically just restating the accepted answer from three years ago. – ceejayoz Feb 19 at 12:27
  • 1
    But the accepted answer doesn't seem to be working, git doesn't have an argument called src, I used repo instead. @ceejayoz – Souvik Haldar Feb 19 at 15:31
  • 1
    You should be able to propose an edit to adjust it. It does link to the full docs. – ceejayoz Feb 19 at 15:35

you can try this

- git:
    repo: 'https://foosball.example.org/path/to/repo.git'
    dest: /srv/checkout
    version: release-0.22

full documentation on https://docs.ansible.com/ansible/latest/modules/git_module.html

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.