微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过 Ansible 将 Git Repo 克隆到所有文件夹中

如何解决如何通过 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 | urlencode }}:{{ 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

应该对上述任务进行哪些更改,以便它可以在一次执行剧本时递归地将同一存储库克隆到所有 55 个文件

任何帮助将不胜感激。

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

解决方法

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

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

- 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
,

我们正在使用此解决方案;我们将创建的文件夹注册到一个变量中,然后重用该变量来克隆 git 存储库。

---
- name: Sample playbook for git
  hosts: localhost
  connection: local
  become: True
  become_user: root
  gather_facts: False
  tasks:
  - name: Creating multiple test environments
    file: dest=/var/www/test{{ item }}  state=directory
    with_sequence: start=1 end=55
    register: my_test_folders

  - name: Clone repo to the newly created test environments
    git: 
      repo: "https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/ABC/sample.git"
      dest: "{{ item.path }}"
    loop: "{{ my_test_folders.results }}"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。