cognifloyd.bitbucket-sources
Ansible Role: Bitbucket Sources
This role helps you clone or pull a Bitbucket cloud repository using an access key. You can place the cloned repository anywhere that the specified user owns. If the user or location doesn't exist, they will be created.
You can use this role multiple times with include_role
or import_role
. Other methods haven't been tested.
Tested Scenarios:
- Cloning a public Git repository using anonymous HTTPS.
- Cloning a public Git repository using SSH with a key.
- Cloning a private Git repository using SSH with a key.
- Cloning a public Mercurial (hg) repository using anonymous HTTPS.
Upcoming Features:
- Support for private Mercurial repositories with a way to set the SSH key.
- Need to test support for public Mercurial repositories over SSH (SSH key must be in the SSH config).
Important Note:
This role was moved from cognifloyd/ansible-role-bitbucket_sources.
Requirements
This role requires that Git or Mercurial (hg) is already installed. If you need to specify a different location for the executables, use the optional variable bitbucket_sources_executable
.
Role Variables
Creating the Clone URL
This role uses three variables to create the Bitbucket cloud repository URL:
bitbucket_sources_repo_type
(valid options: "git", "hg")bitbucket_sources_repo_account
bitbucket_sources_repo_name
Depending on whether the type is Git or Mercurial, the URL will be:
- For Git:
ssh://[email protected]/<account>/<name>.git
- For Mercurial:
ssh://[email protected]/<account>/<name>
You can set bitbucket_sources_altssh
to "yes
" to use alternative SSH URLs:
- For Git:
ssh://[email protected]:443/<account>/<name>/
- For Mercurial:
ssh://[email protected]:443/<account>/<name>/
If you prefer HTTPS instead of SSH, set bitbucket_sources_protocol
to https
. With HTTPS, the URLs will be:
- For Git:
https://bitbucket.org/<account>/<name>.git
- For Mercurial:
https://bitbucket.org/<account>/<name>
The Clone Destination
The repository will be cloned to a destination defined by bitbucket_sources_dest
, and it will be owned by bitbucket_sources_owner:bitbucket_sources_group
. The parent directory must be owned by <owner>:<group>
and will be created if it doesn't exist, with permissions set by bitbucket_sources_mode
.
To access a repository, Bitbucket requires credentials, so you’ll need to provide a Bitbucket access key in bitbucket_sources_key
. If the key shouldn't be copied from the controller to the target host, set bitbucket_sources_key_copy
to false
.
** Upcoming Feature: ** This role may add the ability to push the public key to Bitbucket based on bitbucket_sources_key_copy
.
** defaults/main.yml **:
bitbucket_sources_repo_type: git
bitbucket_sources_owner: "{{ ansible_user }}"
bitbucket_sources_group: "{{ ansible_user }}"
bitbucket_sources_mode: 0755
bitbucket_sources_altssh: no
bitbucket_sources_key_dest: "~{{ bitbucket_sources_owner }}/.ssh/{{ bitbucket_sources_key | basename }}"
bitbucket_sources_key_copy: yes
bitbucket_sources_force: no
** vars/main.yml **:
none
** Required Role Parameters:** You must set these parameters (there are no defaults):
bitbucket_sources_repo_account: "<bitbucket user>"
bitbucket_sources_repo_name: "<bitbucket repo (without .git)>"
bitbucket_sources_dest: "~<user>/scm/<account>/<name>.git"
bitbucket_sources_key: "~/.ssh/access_key"
You can also override any defaults mentioned above. Other optional variables:
bitbucket_sources_version: a83b8a42
bitbucket_sources_executable: "/home/acme/gentoo-prefix/usr/bin/git"
bitbucket_sources_force: yes
** Upcoming Features: ** Still figuring out how to make Mercurial use the specified key.
Global Variables:
Variables from the global scope (e.g., hostvars, group vars, etc.).
By default, <owner>
and <group>
are set to ansible_user
:
ansible_user
Variables from Other Roles:
Any variables from other roles are not included.
Dependencies
This role does not have any external dependencies.
Example Playbook
- hosts: vagrant
tasks:
- name: Clone or force update example-magnificent from Bitbucket
include_role:
name: theatro.bitbucket_sources
allow_duplicates: yes
public: no
vars:
bitbucket_sources_repo_type: git
bitbucket_sources_repo_account: example
bitbucket_sources_repo_name: magnificent
bitbucket_sources_dest: /var/scm/bitbucket/example/magnificent.git
bitbucket_sources_owner: vagrant
bitbucket_sources_group: vagrant
bitbucket_sources_key: "~/.ssh/example_access_key"
bitbucket_sources_force: yes
License
MIT
Author Information
Created by Jacob Floyd, working at Theatro, in 2017. I took these tasks from another playbook I was working on. After writing much of this, I found some other roles that were helpful but required adaptations for my environment.
ansible-galaxy install cognifloyd.bitbucket-sources