简体   繁体   English

如何使用ansible从本地git存储库克隆到虚拟机

[英]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. 我有一个本地git存储库,正在尝试将其克隆到无业游民的计算机上。 I'm trying to use ansible's "git" module to do this, I have the following task, 我正在尝试使用ansible的“ git”模块执行此操作,我有以下任务,

- 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? 似乎正在尝试在我的VM而不是本地计算机上找到存储库? 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. git模块完全在VM内部执行-您必须为其提供VM可以访问的路径。 Either do a vagrant NFS shared/synced folder with your host, or expose it to the VM over the network via http/ssh. 与您的主机做一个无用的NFS共享/同步文件夹,或者通过http / ssh通过网络将其公开给VM。 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). 请注意,与Virtualbox(以及可能的其他提供程序)一起流浪的非NFS共享文件夹只是来回复制副本,而不是真正的“共享”(即,取决于存储库的大小,如果不是NFS,您可能会感到遗憾)。

The git commands will be run from the remote machine, in this case your Vagrant VM, not your local machine. git命令将从远程计算机(在这种情况下为您的Vagrant VM)而非本地计算机上运行。

One way to accomplish this is through SSH remote port forwarding. 一种方法是通过SSH远程端口转发。 You can forward connections from a port on the remote (Vagrant VM) to a host+port from your local machine. 您可以将连接从远程(Vagrant VM)上的端口转发到本地计算机上的主机+端口。

Your local machine needs to make the git repository available. 您的本地计算机需要使git存储库可用。 This can be done with sshd, but I will use the relatively obscure git-daemon , as it is easier to set up. 可以通过sshd完成,但是我将使用相对模糊的git-daemon ,因为它更易于设置。

In your Ansible inventory file, add the following options to your Vagrant VM host. 在您的Ansible库存文件中,将以下选项添加到Vagrant VM主机中。 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. 在连接期间,这会将请求从端口9418上的远程计算机转发到端口9418上的本地计算机(git-daemon)。

# 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 . 对于此示例,我将假设您本地计算机上的GIT_DIR位于/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): 在运行Ansible剧本之前,请在另一个终端上启动以下命令(如果要查看输出,请添加--verbose选项):

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. 现在,当git连接到本地主机(相对于Vagrant VM)时,请求将转发到在本地计算机上运行的git守护程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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