How to Clone Git Repo into all folders via Ansible

Viewed 580

I am using Ansible to setup multiple test server environments (which are basically copies of same Git repository into test1-test55 different test folders).

Now, I'm able to create these folders recursively using the file module/package as follows

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

But I'm not sure how to iterate using with_sequence so that the actual git clone task clones https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/abc/sample.git into test1,test2... test55 folders

I have this task to clone the git repo to a single folder/test environment

- 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

What changes should be made to the above task so that it can recursively clone the same repository to all 55 folders in single execution of the playbook

Any help would be appreciated.

Note: Organization and Repo names are for representational purposes only.

2 Answers

You should be able to use with_sequence like you used it to create the directories.

I've set up three folders, with the names test1, test2, test3 and used a task below to clone the repository into all of them. This worked fine.

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

Adapting that to your situation would result in something like the task below.

- 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

We are using this solution; we register the created folders into a variable and reuse that variable to clone the git repos.

---
- 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 }}"

Related