Skip to content

Commit 437a815

Browse files
authored
Merge pull request #111 from YPCrumble/replace_env_vars_with_group_vars
Use group_vars rather than env_vars for playbook variables.
2 parents 92437b3 + c20d479 commit 437a815

File tree

14 files changed

+64
-59
lines changed

14 files changed

+64
-59
lines changed

README.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ production Django deployments:
1818
- RabbitMQ
1919

2020
Default settings are stored in `roles/role_name/defaults/main.yml`.
21-
Environment-specific settings are in the `env_vars` directory.
21+
Environment-specific settings are in the `group_vars` directory.
2222

2323
A `certbot` role is also included for automatically generating and renewing
2424
trusted SSL certificates with [Let's Encrypt][lets-encrypt].
@@ -58,7 +58,7 @@ sudo apt-get update
5858

5959
### Configuring your application
6060

61-
The main settings to change are in the [`env_vars/base.yml`](env_vars/base.yml)
61+
The main settings to change are in the `group_vars/[environment_name]/vars.yml`
6262
file, where you can configure the location of your Git project, the project
6363
name, and the application name which will be used throughout the Ansible
6464
configuration.
@@ -230,8 +230,9 @@ The security module performs several basic server hardening tasks. Inspired by
230230
**Security role configuration**
231231

232232
- Change the `server_user` from `root` to something else in `roles/base/defaults/main.yml`
233-
- Change the sudo password in `roles/security/defaults/main.yml`
233+
- Change the sudo password in `group_vars/[environment_name]/vars.yml`
234234
- Change variables in `./roles/security/vars/` per your desired configuration
235+
by overriding them in `group_vars/[environment_name]/vars.yml`
235236

236237
**Running the Security role**
237238

@@ -251,9 +252,6 @@ Create an inventory file for the environment, for example:
251252
```
252253
# development
253254
254-
[all:vars]
255-
env=dev
256-
257255
[webservers]
258256
webserver1.example.com
259257
webserver2.example.com
@@ -281,8 +279,6 @@ A few notes here:
281279
section of the inventory file.
282280
- The `webservers.yml` playbook will only provision servers in the
283281
`[webservers]` section of the inventory file.
284-
- An inventory var called `env` is also set which applies to `all` hosts in the
285-
inventory. This is used in the playbook to determine which `env_var` file to use.
286282
- The `-K` flag is for adding the sudo password you created for a new sudoer in
287283
the Security role (if applicable)
288284

Vagrantfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
3030
# For local development, uncommenting and editing the line below will enable
3131
# a folder in the host machine containing your local git repo to be synced to
3232
# the guest machine. Ensure the Ansible playbook variable "setup_git_repo" is
33-
# set to "no" (in env_vars/vagrant.yml) when enabling this.
34-
#config.vm.synced_folder "../../../my-cool-app", "/webapps/django_default_project/django_default_project"
33+
# set to "no" (in group_vars/vagrant/vars.yml) when enabling this.
34+
#config.vm.synced_folder "../../../django_default_project", "/webapps/django_default_project/django_default_project"
3535

3636
# Ansible provisioner.
3737
config.vm.provision "ansible" do |ansible|

dbservers.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
---
22

3-
- name: Provision a {{ application_name }} db server
3+
- name: Provision application db server
44
hosts: dbservers
55
become: true
66
become_user: root
77
remote_user: "{{ server_user }}"
88
vars:
99
update_apt_cache: true
10-
vars_files:
11-
- env_vars/base.yml
12-
- env_vars/{{ env }}.yml
1310

1411
module_defaults:
1512
apt:

development

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
[all:vars]
2-
env=development
3-
41
[webservers]
52
dev.example.com nginx_use_letsencrypt=true
63

env_vars/base.yml

-18
This file was deleted.

env_vars/development.yml renamed to group_vars/development/vars.yml

+21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
---
22

3+
git_repo: https://github.com/YPCrumble/django-default-project
4+
5+
project_name: django_default_project
6+
application_name: django_default_project
7+
8+
# Note that this PPA doesn't guarantee timely updates in case of security issues.
9+
# Simply remove these two vars below if you prefer to use the official PPA and
10+
# default Python version that came with your Linux distro.
11+
#
12+
# More info here: https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes
13+
enable_deadsnakes_ppa: true
14+
virtualenv_python_version: python3.9
15+
16+
317
# Git settings.
418
setup_git_repo: true
519
git_branch: development
620

721

22+
# Security settings.
23+
sudo_user_password: $6$rounds=656000$ca2RWJgtEqDVpOp9$0S0N3GHjOIO1PwRZ0vDyr0Z5Pi8ZcEa8.r.T.Wsx.O8RZlpTV1w0BLoEWwDb.zTkJOmP1Re.zBfQsviZaP89m0
24+
25+
826
# Database settings.
927
db_user: "{{ application_name }}"
1028
db_name: "{{ application_name }}"
@@ -19,6 +37,7 @@ gunicorn_num_workers: 3
1937
# you make a request, basically reloading the code. Very handy
2038
# when developing. Set to 0 for unlimited requests (default).
2139
gunicorn_max_requests: 0
40+
gunicorn_timeout_seconds: 300
2241

2342

2443
# RabbitMQ settings.
@@ -42,6 +61,8 @@ django_secret_key: "akr2icmg1n8%z^3fe3c+)5d0(t^cy-2_25rrl35a7@!scna^1#"
4261

4362
broker_url: "amqp://{{ rabbitmq_application_user }}:{{ rabbitmq_application_password }}@localhost/{{ rabbitmq_application_vhost }}"
4463

64+
requirements_file: "{{ project_path }}/requirements.txt"
65+
4566
run_django_db_migrations: true
4667
run_django_collectstatic: true
4768

env_vars/vagrant.yml renamed to group_vars/vagrant/vars.yml

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
---
22

3+
git_repo: https://github.com/YPCrumble/django-default-project
4+
5+
project_name: django_default_project
6+
application_name: django_default_project
7+
8+
# Note that this PPA doesn't guarantee timely updates in case of security issues.
9+
# Simply remove these two vars below if you prefer to use the official PPA and
10+
# default Python version that came with your Linux distro.
11+
#
12+
# More info here: https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes
13+
enable_deadsnakes_ppa: true
14+
virtualenv_python_version: python3.9
15+
16+
317
# Git settings.
418
setup_git_repo: true
519
git_branch: main
620

721

22+
# Security settings.
23+
sudo_user_password: $6$rounds=656000$ca2RWJgtEqDVpOp9$0S0N3GHjOIO1PwRZ0vDyr0Z5Pi8ZcEa8.r.T.Wsx.O8RZlpTV1w0BLoEWwDb.zTkJOmP1Re.zBfQsviZaP89m0
24+
25+
826
# Database settings.
927
db_user: "{{ application_name }}"
1028
db_name: "{{ application_name }}"
@@ -19,7 +37,6 @@ gunicorn_num_workers: 3
1937
# you make a request, basically reloading the code. Very handy
2038
# when developing. Set to 0 for unlimited requests (default).
2139
gunicorn_max_requests: 0
22-
2340
gunicorn_timeout_seconds: 300
2441

2542

@@ -51,8 +68,6 @@ run_django_collectstatic: true
5168

5269

5370
# Nginx settings.
54-
nginx_strong_dh_group: false
55-
5671
ssl_crt: |
5772
-----BEGIN CERTIFICATE-----
5873
MIIDQjCCAiqgAwIBAgIBADANBgkqhkiG9w0BAQsFADAcMRowGAYDVQQDDBFteS1j

molecule/default/converge.yml

-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
vars:
1515
update_apt_cache: true
1616
force_ssh_authentication: false
17-
vars_files:
18-
- ../../env_vars/base.yml
19-
- ../../env_vars/vagrant.yml
2017

2118
tasks:
2219
- name: Install Python3

molecule/default/molecule.yml

+7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ lint: |
77
yamllint .
88
platforms:
99
- name: instance-xenial
10+
groups:
11+
- vagrant
1012
image: ubuntu
1113
image_version: xenial
1214
privileged: true
1315
- name: instance-focal
16+
groups:
17+
- vagrant
1418
image: ubuntu
1519
image_version: focal
1620
privileged: true
@@ -20,6 +24,9 @@ provisioner:
2024
name: ansible-lint
2125
env:
2226
ANSIBLE_ROLES_PATH: ../../roles/
27+
inventory:
28+
links:
29+
group_vars: ../../group_vars/
2330
verifier:
2431
name: testinfra
2532
lint:

roles/db/tasks/main.yml

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535
- name: Ensure database is created
3636
become: true
3737
become_user: postgres
38-
postgresql_db: name={{ db_name }}
39-
encoding='UTF-8'
40-
lc_collate='en_US.UTF-8'
41-
lc_ctype='en_US.UTF-8'
42-
template='template0'
43-
state=present
38+
postgresql_db:
39+
name: "{{ db_name }}"
40+
encoding: UTF-8
41+
lc_collate: en_US.UTF-8
42+
lc_ctype: en_US.UTF-8
43+
template: template0
44+
state: present
4445

4546
- name: Ensure user has access to the database
4647
become: true

roles/security/defaults/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# You can use the following Python script to adjust this value.
44
# pip install passlib
55
# python -c "from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt(getpass.getpass())"
6-
server_user_password: $6$rounds=656000$ca2RWJgtEqDVpOp9$0S0N3GHjOIO1PwRZ0vDyr0Z5Pi8ZcEa8.r.T.Wsx.O8RZlpTV1w0BLoEWwDb.zTkJOmP1Re.zBfQsviZaP89m0
6+
server_user_password: "{{ sudo_user_password }}"
77

88
perform_aptitude_dist_upgrade: true
99

security.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
---
22

3-
- name: Initial configuration for a {{ application_name }} server
3+
- name: Initial configuration for application server
44
hosts: all
55
become: true
66
become_user: root
77
remote_user: root
88
vars:
99
- update_apt_cache: true
1010
vars_files:
11-
- env_vars/base.yml
12-
- env_vars/{{ env }}.yml
1311
- roles/base/defaults/main.yml
1412
module_defaults:
1513
apt:

vagrant.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
---
22

3-
- name: Create a {{ application_name }} virtual machine via vagrant
3+
- name: Create application virtual machine via vagrant
44
hosts: all
55
become: true
66
become_user: root
77
remote_user: vagrant
88
vars:
99
update_apt_cache: true
10-
vars_files:
11-
- env_vars/base.yml
12-
- env_vars/vagrant.yml
1310
module_defaults:
1411
apt:
1512
force_apt_get: true

webservers.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
---
22

3-
- name: Provision a {{ application_name }} web server
3+
- name: Provision application web server
44
hosts: webservers
55
become: true
66
become_user: root
77
remote_user: "{{ server_user }}"
88
vars:
99
update_apt_cache: true
10-
vars_files:
11-
- env_vars/base.yml
12-
- env_vars/{{ env }}.yml
1310
module_defaults:
1411
apt:
1512
force_apt_get: true

0 commit comments

Comments
 (0)