如何在 Microsoft Windows 中使用 Ansible 克隆 Git 存储库

3

Ansible 2.1,控制主机为CentOS7,客户端数量为Windows 10。

我需要在远程端克隆Git存储库,但不知道如何操作:

我尝试了类似以下的操作:

- name: clone repositories
  git:
   repo: "{{ item.repo }}"
   dest: "C:\openserver\domains\{{ domain_name }}\{{ item.value.path }}"
   version: "{{ item.version | default('HEAD') }}"
  with_items: "{{ repositories }}"

你可能需要在Windows PowerShell中运行git,如http://docs.ansible.com/ansible/intro_windows.html所述。 - msw
2
msw 是正确的 - 使用像 git 这样的“普通”Ansible模块来管理Windows框不可能。由于现在的Windows模块中没有git模块,所以您必须使用PowerShell脚本和/或原始模块进行 improvisation。 - jonashackt
3个回答

4

2

如果还不算晚,你可以使用类似这样的东西

- name: clone repositories
  win_command: >
    "C:\Program Files\Git\bin\git.exe"
    "--no-pager"
    "clone"
    "http://user:pass@github.com/OpenCorpora/opencorpora.git"
    "C:\OPenserver\domains\opencorpora.local\www"
    "--branch"
    "master"
    "--recursive"

参考 https://docs.ansible.com/ansible/latest/user_guide/windows_usage.html


0

Ansible 建议使用脚本模块,但我使用了原始模块(这种方式比脚本模块不安全吗?):

- name: clone repositories
  raw: C:\OpenServer\modules\git\cmd\git.exe clone https://github.com/OpenCorpora/opencorpora.git C:\OPenserver\domains\opencorpora.local\www

前提条件:已安装msysgit(在我的情况下为C:\OpenServer\modules\git\),或者Windows本地git客户端(请参见此处:https://git-scm.com/download/win)。

PS不幸的是,我今天不知道如何使用带有空格的默认安装路径的本地客户端:

- name: clone repositories
  raw: C:\Program Files\Git\cmd\git.exe clone https://github.com/OpenCorpora/opencorpora.git C:\OPenserver\domains\opencorpora.local\www

它失败并显示错误:

TASK [website_win : clone repositories] ****************************************
fatal: [192.168.1.43]: FAILED! => {"changed": false, "failed": true, "rc": 1, "stderr": "C:\\Program : The term 'C:\\Program' is not recognized as the name of a cmdlet, function, script file, or operable progra\r\nm. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.\r\nAt line:1 char:1\r\n+ C:\\Program Files\\Git\\cmd\\git.exe clone https://github.com/OpenCorpora ...\r\n+ ~~~~~~~~~~\r\n+ CategoryInfo          : ObjectNotFound: (C:\\Program:String) [], CommandNotFoundException\r\n+ FullyQualifiedErrorId : CommandNotFoundException\r\n", "stdout": "", "stdout_lines": []}

NO MORE HOSTS LEFT *************************************************************

仍然存在SSH协议和SSL密钥的问题,以下是详细信息:https://groups.google.com/forum/#!topic/ansible-project/NjND8LYqHnQ - A K
错误似乎与您使用的原始模块有关。尝试使用引号并转义所有反斜杠:raw: C:\\Program Files\\Git\\cmd\\git.exe clone https://github.com/OpenCorpora/opencorpora.git - jonashackt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接