Skip to content

Commit 7dc09f7

Browse files
committed
Rework Makefile to be dynamic to ansible roles, and use bitwarden for vault pass
1 parent e5b57a6 commit 7dc09f7

File tree

3 files changed

+123
-30
lines changed

3 files changed

+123
-30
lines changed

Makefile

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
1+
# Makefile that passes the CLI args to ansible for the given playbook tags
2+
BW_AVAILABLE = $(shell command -v bw 2> /dev/null)
3+
VAULT_PASS_ARGS = $(if $(BW_AVAILABLE),--vault-password-file ./.vault-pass.sh, --ask-vault-pass)
4+
PLAYBOOK_CMD = uv run ansible-playbook $(VAULT_PASS_ARGS) -i ansible/inventory.yml ansible/playbook.yml
5+
VAULT_FILES := $(shell find ansible/vaults/ -type f);
6+
7+
# Extract tags directly from YAML (faster but less accurate)
8+
VALID_TAGS := $(shell grep -r "tags:" ansible/playbook.yml | sed 's/.*tags://g' | tr -d '[]"' | tr ',' '\n' | tr -d ' ' | sort -u | grep -v '^$$')
9+
110
all:
2-
ansible-playbook -v -i inventory.yml --ask-vault-pass playbook.yml
11+
$(PLAYBOOK_CMD)
12+
13+
list:
14+
uv run ansible-playbook --list-tags ansible/playbook.yml
15+
16+
inventory:
17+
uv run ansible-vault edit $(VAULT_PASS_ARGS) ansible/inventory.yml
18+
19+
install-ansible: requirements.txt
20+
uv venv --python=3.13; uv pip install -r requirements.txt
321

4-
deploy:
5-
ansible-playbook -v -i inventory.yml --ask-vault-pass playbook.yml --tags="deploy"
22+
encrypt-vaults:
23+
@echo "Encrypting all vault files in ansible/vaults/ directory: $(VAULT_FILES)";
24+
uv run ansible-vault encrypt $(VAULT_FILES)
625

7-
setup:
8-
ansible-playbook -v -i inventory.yml --ask-vault-pass playbook.yml --tags="setup"
26+
vaults:
27+
@echo "Select a vault file to edit:"; \
28+
select file in $$(find ansible/inventory.yml ansible/vaults/ -type f); do \
29+
if [ -n "$$file" ]; then \
30+
uv run ansible-vault edit $(VAULT_PASS_ARGS) "$$file"; \
31+
break; \
32+
fi; \
33+
done
934

10-
maintenance:
11-
ansible-playbook -v -i inventory.yml --ask-vault-pass playbook.yml --tags="maintenance"
35+
%:
36+
@if echo "$(VALID_TAGS)" | grep -wq "$@"; then \
37+
echo "Running playbook with tag: $@"; \
38+
$(PLAYBOOK_CMD) --tags=$@; \
39+
else \
40+
echo "Error: '$@' is not a valid tag."; \
41+
echo "Valid tags are: $(VALID_TAGS)"; \
42+
exit 1; \
43+
fi
1244

13-
borg:
14-
ansible-playbook -v -i inventory.yml --ask-vault-pass playbook.yml --tags="borg"
1545

16-
ssl:
17-
ansible-playbook -v -i inventory.yml --ask-vault-pass playbook.yml --tags="ssl"
46+
.PHONY: list

README.md

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
This repository contains tools and rules for automatically deploying datalab instances using Terraform/OpenTofu and Ansible.
1313
It can be used as a template for deploying your own datalab instance, and optionally periodically resynced on new releases.
1414

15-
Use of Terraform for cloud provisioning is OPTIONAL; the Ansible playbooks are sufficient to set up a datalab instance on a existing hardware.
15+
Use of Terraform/OpenTofu for cloud provisioning is OPTIONAL; the Ansible playbooks are sufficient to set up a datalab instance on a existing hardware.
1616
The Ansible playbooks can even be used to deploy datalab on shared hardware; all mandatory services will be deployed within containers, so it is possible to set up the full NGINX + datalab stack alongside an existing reverse proxy managing other services, although this will need to be configured by the user.
1717

1818
Ideally, various aspects of the configuration will be transferrable, and thus only instance-specific configuration will need to be provided (e.g., usernames, domain names), in which case your instance version of this repository can be kept fairly in-sync with the main branch (which will continue to be updated as datalab's requirements grow and change).
@@ -21,13 +21,18 @@ Attempts will be made to tabulate the supported versions of datalab with each re
2121

2222
## Supported versions
2323

24+
<div align="center">
25+
2426
| This repository version | *datalab* version |
2527
|---|---|
2628
| v0.1.x | v0.4.x |
2729
| v0.2.x | v0.4.x |
2830
| v0.3.x | v0.5.0-rc.x |
2931
| v0.4.x | v0.5.x |
3032
| v0.5.x | v0.6.x |
33+
| v0.6.x | v0.6.x |
34+
35+
</div>
3136

3237
## Changelog
3338

@@ -58,7 +63,7 @@ Stack:
5863
- Docker Compose for API, app and database containers
5964
- Simple filesystem for filestore
6065

61-
### Datalab versioning
66+
### *datalab* versioning
6267

6368
The Ansible playbooks will deploy the datalab version that is included as a `git
6469
submodule` to this repository.
@@ -77,21 +82,28 @@ to deploy *datalab*, and that it is:
7782
- accessible via SSH (using your local SSH config),
7883
- running Ubuntu 22.04 or a similar distro with `apt` available.
7984

80-
It also assumes you have set up Ansible on your local machine, and it is
81-
expected that this machine is at least a Unix-like OS with `git` and `bash`
82-
and `sed` available.
83-
You can find instructions for this in the [Ansible documentation](https://docs.ansible.com/ansible/latest/getting_started/get_started_ansible.html).
84-
You can also simply install ansible from the `requirements.txt` file with `pip`
85-
after cloning:
85+
It also assumes that your local machine is running a Unix-like OS (Linux, WSL, macOS) with `git` and `bash`,
86+
`make` and `sed` available.
87+
88+
You can find more information on the requirements of the server and control node in the [Ansible documentation](https://docs.ansible.com/ansible/latest/getting_started/get_started_ansible.html).
89+
90+
The first step is to clone this repository (or your fork/templated version) with submodules and then install Ansible and its dependencies.
91+
We recommend using [uv](https://astral.sh/uv) for this, as the included [Makefile] will use it to run the playbooks in a virtual
92+
environment.
93+
94+
```shell
95+
git clone --recurse-submodules [email protected]:datalab-org/datalab-ansible-terraform
96+
cd datalab-ansible-terraform
97+
uv venv --python 3.12
98+
uv pip install -r requirements.txt
99+
```
86100

87-
The first step is to clone this repository (or your fork) with submodules and
88-
then install Ansible (if not done already):
101+
or alternatively,
89102

90103
```shell
91104
git clone --recurse-submodules [email protected]:datalab-org/datalab-ansible-terraform
92-
python -m venv .venv
93-
source .venv/bin/activate
94-
pip install -r requirements.txt
105+
cd datalab-ansible-terraform
106+
make install-ansible
95107
```
96108

97109
You can then navigate to the to the `./ansible` directory to begin configuring
@@ -145,10 +157,20 @@ To encrypt them, you can run
145157
ansible-vault encrypt inventory.yml vaults/datalab/prod_config.json vaults/datalab/.env vaults/datalab/.env_server vaults/datalab/.ssh/* vaults/borg/.ssh/*
146158
```
147159

160+
or simply
161+
162+
```shell
163+
make encrypt-vaults
164+
```
165+
148166
and provide a password when prompted (which will then need to be kept safe and
149-
used every time the Ansible playbook is run). Omit the optional SSH wildcards if no
150-
SSH keys are required.
151-
You should never commit these files directly without encryption.
167+
used every time the Ansible playbook is run).
168+
You should never commit these files directly without encryption, even to a
169+
private git repository.
170+
171+
If you are using your own domain (configured via `app_url` and `api_url` in the inventory),
172+
then you will need to update your domain's DNS settings so that your subdomains point to the IP
173+
of the server as given in your inventory file.
152174

153175
Once all these configuration steps have been performed, we can try to execute
154176
the Ansible "playbook" that will install all the pre-requisite services, build
@@ -161,10 +183,13 @@ This is achieved by running:
161183
ansible-playbook --ask-vault-pass -i inventory.yml playbook.yml
162184
```
163185

164-
If completed successfully, the server should now be running a *datalab*
165-
instance at your configured URL!
186+
or simply
166187

167-
If you are using your own domain, you will need to update your DNS settings so that your domain name points to the IP of the server as given in your inventory file.
188+
```
189+
make
190+
```
191+
192+
If completed successfully, the server should now be running a *datalab* instance at your configured URLs!
168193
169194
#### Keeping things up to date
170195
@@ -195,6 +220,45 @@ especially if you have made any custom changes to the playbooks.
195220
Be sure to also commit the changes to your submodule so you know precisely which versions
196221
of the playbooks are running.
197222

223+
The [`Makefile`] also contains a number of other useful commands, such as:
224+
225+
```shell
226+
make vaults
227+
```
228+
229+
to edit the encrypted vault files, and
230+
231+
```shell
232+
make list
233+
```
234+
235+
to list all of the available Ansible tags that can be run individually.
236+
237+
#### Bitwarden integration
238+
239+
Constantly entering the vault password for every attempted deployment can be a
240+
bit tedious, so by default, the `Makefile` will attempt to retrieve the vault
241+
password from a local [Bitwarden CLI](https://bitwarden.com/help/cli/) installation,
242+
which either only requires you to enter your Bitwarden password once per
243+
session, or can be configured to remain logged in.
244+
245+
To use this feature, you will need to store your vault password in Bitwarden
246+
using the same name as the cloned repository as reported locally by
247+
248+
```shell
249+
$ git remote get-url origin
250+
[email protected]:datalab-org/datalab-demo-deployment
251+
{^^^^^^^^^^^^^^^^^^^^^}
252+
repository name
253+
```
254+
255+
If you intially cloned this repo and then renamed it, you can update your local
256+
version to use the same name using:
257+
258+
```shell
259+
git remote set-url origin <my-git-repo-url>
260+
```
261+
198262
#### Backups
199263

200264
##### Native backups

ansible/vaults/datalab/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)