首页 > 解决方案 > 如何通过 Ansible 将 Git Repo 克隆到所有文件夹中

问题描述

我正在使用 Ansible 设置多个测试服务器环境(基本上是将相同的 Git 存储库复制到 test1-test55 不同的测试文件夹中)。

现在,我可以使用文件模块/包递归地创建这些文件夹,如下所示

- name: Creating multiple test environments
  file: dest=/var/www/test{{ item }}  state=directory
  with_sequence: start=1 end=55

但我不确定如何使用 with_sequence 进行迭代,以便实际的 git clone 任务克隆 https://{{ githubuser | urlcode }}:{{ github密码 | urlencode }}@github.com/abc/sample.git 进入 test1,test2...test55 文件夹

我有这个任务将 git repo 克隆到单个文件夹/测试环境

- name: Clone repo to the newly created test environments
  git: 
    repo: "https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/ABC/sample.git"
    dest: /var/www/test1/sample #This will clone sample.git to test1, have tried using item here but gives an error
    #with_sequence: start=1 end=55 #This does not work

应该对上述任务进行哪些更改,以便它可以在 playbook 的单次执行中递归地将相同的存储库克隆到所有 55 个文件夹

任何帮助,将不胜感激。

注意:组织和回购名称仅用于表示目的。

标签: filegithubansiblegit-clone

解决方案


您应该能够with_sequence像使用它来创建目录一样使用它。

我设置了三个文件夹,名称分别test1为 、和test2test3并使用下面的任务将存储库克隆到所有文件夹中。这工作得很好。

- name: Clone repository into all folders
  git:
    repo: "<repo url>"
    dest: "./test{{ item }}"
  with_sequence: start=1 end=3

使之适应您的情况将导致类似于以下任务的结果。

- name: Clone repository into all folders
  git:
    repo: "https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/ABC/sample.git"
    dest: "/var/www/test{{ item }}"
  with_sequence: start=1 end=55

推荐阅读