| --- | 
 | - name: Create and update repositories in BitBucket | 
 |   hosts: localhost | 
 |   connection: local | 
 |   vars_files: | 
 |   - repository-mapping-name-folder.yml | 
 |   - repository-custom-properties.yml | 
 |   - private-repository-user-list.yml | 
 |   tasks: | 
 |   - name: Create repositories if necessary | 
 |     uri: | 
 |       url: "{{ repository_secure_endpoint_https }}" | 
 |       method: POST | 
 |       user: "{{ username }}" | 
 |       password: "{{ password }}" | 
 |       force_basic_auth: yes | 
 |       status_code: 201,409 | 
 |       body_format: json | 
 |       body: | 
 |         name: "{{ item.repo_name }}" | 
 |         scmId: git | 
 |         forkable: True | 
 |     with_items: "{{ repo_names }}" | 
 |  | 
 |   - name: Add permissions to repos | 
 |     uri: | 
 |       url: "{{ repository_secure_endpoint_https }}/permissions/users?name={{ item }}&permission=PROJECT_ADMIN" | 
 |       method: PUT | 
 |       user: "{{ username }}" | 
 |       password: "{{ password }}" | 
 |       force_basic_auth: yes | 
 |       status_code: 204,409 | 
 |     with_items: "{{ repo_users }}" | 
 |  | 
 |   - name: Add Onestash as remote repo | 
 |     ignore_errors: true | 
 |     shell: "git remote add target {{ repository_secure_endpoint_ssh }}/{{ item.repo_name }}.git" | 
 |     args: | 
 |       chdir: "{{ item.workspace }}/{{ item.repo_name }}" | 
 |     with_items: "{{ repo_names }}" | 
 |  | 
 |   - name: Pull from Onestash if the repo is already there | 
 |     ignore_errors: yes | 
 |     shell: "git pull target master" | 
 |     args: | 
 |       chdir: "{{ item.workspace }}/{{ item.repo_name }}" | 
 |     with_items: "{{ repo_names }}" | 
 |  | 
 |   - name: Commit updated repos to onestash | 
 |     shell: "git checkout master && git push -u target master" | 
 |     args: | 
 |       chdir: "{{ item.workspace }}/{{ item.repo_name }}" | 
 |     with_items: "{{ repo_names }}" |