简体   繁体   中英

How to clone from a local git repository to a vm using ansible

I have a local git repository which I am trying to clone onto a vagrant machine. I'm trying to use ansible's "git" module to do this, I have the following task,

- name: Clone repository
  git: repo=git://../.git dest=/home/vagrant/source accept_hostkey=True

When I run this task I receive the error,

failed: [webserver] => {"cmd": "/usr/bin/git ls-remote git://../.git -h refs/heads/HEAD", "failed": true, "rc": 128}
stderr: fatal: unable to connect to ..:
..[0: 42.185.229.96]: errno=Connection timed out

msg: fatal: unable to connect to ..:
..[0: 42.185.229.96]: errno=Connection timed out

FATAL: all hosts have already failed -- aborting

It looks like it's trying to find the repository on my VM rather than on my local machine? How to I clone from my local repo?

The git module executes completely inside the VM- you have to give it a path that's reachable by the VM. Either do a vagrant NFS shared/synced folder with your host, or expose it to the VM over the network via http/ssh. Be aware that non-NFS shared folders in vagrant with Virtualbox (and possibly other providers) just do dumb copies back and forth, not true "sharing" (ie, depending on how big your repo is, you might be sorry if it's not NFS).

The git commands will be run from the remote machine, in this case your Vagrant VM, not your local machine.

One way to accomplish this is through SSH remote port forwarding. You can forward connections from a port on the remote (Vagrant VM) to a host+port from your local machine.

Your local machine needs to make the git repository available. This can be done with sshd, but I will use the relatively obscure git-daemon , as it is easier to set up.

In your Ansible inventory file, add the following options to your Vagrant VM host. This will forward requests from your remote machine on port 9418 to your local machine at port 9418 (git-daemon) for the duration of the connection.

# inventory
webserver ansible_ssh_extra_args="-R 9418:localhost:9418"

# *OR* for a group of hosts
[webservers:vars]
ansible_ssh_extra_args="-R 9418:localhost:9418"

For this example, I will assume the GIT_DIR on your local machine is located at /home/you/repos/your-git-repo/.git . Before running your Ansible playbook, start the following command in another terminal (add a --verbose option if you want to see output):

git daemon \
    --listen=127.0.0.1 \
    --export-all \
    --base-path=/home/you/repos \
    /home/you/repos/your-git-repo/.git

Your task would look like this:

- git: repo=git://localhost/your-git-repo dest=/home/vagrant/source

Now when git connects to localhost (relative to your Vagrant VM), requests are forwarded to the git daemon running on your local machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM