16

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

grant tailor
  • 495
  • 2
  • 6
  • 13
  • 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
  • 3
    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
  • I'm guessing you don't get any output from running with -vvv. Can you put your task running the commands in your question please? – Mackwerk Feb 11 '15 at 17:43
  • did it and it works now...added `sudo_user` and it worked!..thanks – grant tailor Feb 11 '15 at 18:16
  • Great :) Don't forget to either accept ProfFalken's answer (I guess the ``sudo_user`` line in there helped you a bit of create your own answer and accept that :) – Mackwerk Feb 11 '15 at 21:02

3 Answers3

23

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.

Bit-Man
  • 103
  • 5
ProfFalken
  • 366
  • 2
  • 5
  • 2
    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
  • 2
    from http://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
8

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

Souvik Haldar
  • 91
  • 1
  • 3
0

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

Sarath Ak
  • 101
  • 2