Skip to content

Commit d6d5207

Browse files
fixed security groups and optimized mongodb installation
1 parent b4a9fde commit d6d5207

File tree

20 files changed

+205
-44
lines changed

20 files changed

+205
-44
lines changed

Diff for: ansible/pegski.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
become: yes
1010
roles:
1111
- {role: base, tags: ['base']}
12+
- {role: storage, tags: ['storage']}
1213
- {role: ssh_access, tags: ['ssh_access']}
1314
- {role: mongodb, tags: ['mongodb']}

Diff for: ansible/roles/base/handlers/main.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
- name: restart NTP daemon
2-
service: name=ntp state=restarted
2+
service: name=ntp state=restarted
3+
4+
- name: dpkg-reconfigure locales
5+
command: /usr/sbin/dpkg-reconfigure --frontend noninteractive locales

Diff for: ansible/roles/base/tasks/base.yml

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,24 @@
1616
- curl
1717

1818
- name: install nl_NL.UTF-8 locale
19-
shell: locale-gen nl_NL.UTF-8
19+
locale_gen:
20+
name=nl_NL.UTF-8
21+
state=present
22+
notify: dpkg-reconfigure locales
23+
24+
- name: Set LC_LANG
25+
lineinfile:
26+
dest=/etc/environment
27+
state=present
28+
regexp='^LC_LANG'
29+
line='LC_LANG="nl_NL.UTF-8"'
30+
31+
- name: Set LC_ALL
32+
lineinfile:
33+
dest=/etc/environment
34+
state=present
35+
regexp='^LC_ALL'
36+
line='LC_ALL="nl_NL.UTF-8"'
2037

2138
- name: set timezone to UTC
2239
when: ansible_date_time.tz != 'UTC'

Diff for: ansible/roles/mongodb/files/85-ebs.rules

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ACTION=="add", KERNEL=="xvdf", ATTR{bdi/read_ahead_kb}="16"

Diff for: ansible/roles/mongodb/files/90-mongodb.conf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* soft nofile 64000
2+
* hard nofile 64000
3+
* soft nproc 64000
4+
* hard nproc 64000

Diff for: ansible/roles/mongodb/tasks/filesystem.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
- name: create persistent storage directory
3+
file:
4+
dest=/persistent_storage/mongodb
5+
state=directory
6+
recurse=true
7+
owner=mongodb
8+
group=mongodb
9+
mode=755
10+
11+
- name: create subdirectories for mongodb within persistent storage directory
12+
file:
13+
dest=/persistent_storage/mongodb/{{ item }}
14+
state=directory
15+
recurse=true
16+
owner=mongodb
17+
group=mongodb
18+
mode=755
19+
with_items:
20+
- log
21+
- data
22+
23+
- name: create journal subdirectory
24+
file:
25+
dest=/persistent_storage/mongodb/data/journal
26+
state=directory
27+
recurse=true
28+
owner=mongodb
29+
group=mongodb
30+
mode=755
31+
32+
- name: create symobolic link for /journal
33+
file:
34+
src=/persistent_storage/mongodb/data/journal
35+
dest=/persistent_storage/mongodb/journal
36+
owner=mongodb
37+
group=mongodb
38+
state=link
39+
40+
- name: adjust ulimits for mongodb
41+
copy:
42+
src: 90-mongodb.conf
43+
dest: "/etc/security/limits.d/90-mongodb.conf"
44+
45+
- name: set udev config for mongodb
46+
copy:
47+
src: 85-ebs.rules
48+
dest: "/etc/udev/rules.d/85-ebs.rules"
49+

Diff for: ansible/roles/mongodb/tasks/install-mongod.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This is a very basic playbook for installing mongodb.
2+
# If we want something more advanced, we might consider using
3+
# https://github.com/UnderGreen/ansible-role-mongodb
4+
---
5+
- name: import the public key used by the package management system
6+
apt_key: keyserver=hkp://keyserver.ubuntu.com:80 id=EA312927 state=present
7+
8+
- name: add MongoDB repository
9+
apt_repository: repo='deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse' state=present
10+
11+
- name: set correct (advised by mongodb) kernelsettings
12+
copy:
13+
src: mongo_vm_settings.conf
14+
dest: "/etc/init/mongod_vm_settings.conf"
15+
16+
- name: load custom config file to allow incoming traffic
17+
template:
18+
src: templates/mongod.conf.j2
19+
dest: "/etc/mongod.conf"
20+
notify:
21+
- start mongodb
22+
23+
- name: install mongodb
24+
apt: pkg=mongodb-org state=latest update_cache=yes
25+
notify:
26+
- start mongodb
27+
28+
- name: check if mongodb is running and accepting connections
29+
wait_for:
30+
port: 27017
31+
timeout: 10

Diff for: ansible/roles/mongodb/tasks/main.yml

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
# This is a very basic playbook for installing mongodb.
2-
# If we want something more advanced, we might consider using
3-
# https://github.com/UnderGreen/ansible-role-mongodb
41
---
5-
- name: import the public key used by the package management system
6-
apt_key: keyserver=hkp://keyserver.ubuntu.com:80 id=EA312927 state=present
7-
8-
- name: add MongoDB repository
9-
apt_repository: repo='deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse' state=present
10-
11-
- name: set correct (advised by mongodb) kernelsettings
12-
copy:
13-
src: mongo_vm_settings.conf
14-
dest: "/etc/init/mongod_vm_settings.conf"
15-
16-
- name: load custom config file to allow incoming traffic
17-
template:
18-
src: templates/mongod.conf.j2
19-
dest: "/etc/mongod.conf"
20-
notify:
21-
- start mongodb
22-
23-
- name: install mongodb
24-
apt: pkg=mongodb-org state=latest update_cache=yes
25-
notify:
26-
- start mongodb
27-
28-
- name: check if mongodb is running and accepting connections
29-
wait_for:
30-
port: 27017
31-
timeout: 10
2+
- include: filesystem.yml
3+
- include: install-mongod.yml

Diff for: ansible/roles/mongodb/templates/mongod.conf.j2

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Where and how to store data.
77
storage:
8-
dbPath: /var/lib/mongodb
8+
dbPath: /persistent_storage/mongodb/data
99
journal:
1010
enabled: true
1111
# engine:
@@ -16,7 +16,7 @@ storage:
1616
systemLog:
1717
destination: file
1818
logAppend: true
19-
path: /var/log/mongodb/mongod.log
19+
path: /persistent_storage/mongodb/log/mongod.log
2020

2121
# network interfaces
2222
net:

Diff for: ansible/roles/storage/tasks/install-boto.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
- name: python pip and boto
2+
apt:
3+
name={{ item }}
4+
update_cache=yes
5+
with_items:
6+
- python-boto
7+
- python-pip
8+
9+
- name: ensure pip is properly installed
10+
easy_install: name=pip
11+
12+
- name: update pip to latest version
13+
pip:
14+
name=pip
15+
state=latest
16+
17+
- name: update boto to latest version
18+
pip:
19+
name=boto
20+
state=latest

Diff for: ansible/roles/storage/tasks/main.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
- include: install-boto.yml
3+
- include: storage.yml

Diff for: ansible/roles/storage/tasks/storage.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
- name: check if mount already exist
3+
shell: df -h | grep xvdf1
4+
register: mount_exist
5+
ignore_errors: True
6+
7+
- name: Display all variables/facts known for a host
8+
debug: var=hostvars[inventory_hostname] verbosity=1
9+
10+
- name: ensure that external EBS volume exists and is attached to the instance
11+
ec2_vol:
12+
instance: "{{ hostvars[inventory_hostname].id }}"
13+
volume_size: 50
14+
volume_type: gp2
15+
device_name: /dev/xvdf
16+
state: present
17+
region: "{{ aws.region }}"
18+
encrypted: yes
19+
aws_access_key: "{{ aws.access_key }}"
20+
aws_secret_key: "{{ aws.secret_key }}"
21+
22+
- name: hack fdisk with echo commands
23+
shell: (echo n; echo; echo; echo; echo; echo w) | fdisk /dev/xvdf
24+
when: mount_exist.stdout.find("xvdf1") < 1
25+
26+
- name: create a ext4 filesystem on /dev/xvdf1
27+
filesystem:
28+
fstype: ext4
29+
dev: /dev/xvdf1
30+
when: mount_exist.stdout.find("xvdf1") < 1
31+
32+
- name: mount our new disk
33+
mount:
34+
name: /persistent_storage
35+
src: /dev/xvdf1
36+
fstype: ext4
37+
opts: noatime
38+
state: mounted
39+
when: mount_exist.stdout.find("xvdf1") < 1

Diff for: ansible/roles/storage/vars/main.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# For security reasons we use environment variables, so we avoid having secrets in git.
2+
# We reuse the environment variables set for using terraform
3+
4+
aws:
5+
region: "{{ lookup('env','TF_VAR_region') }}"
6+
access_key: "{{ lookup('env','TF_VAR_access_key') }}"
7+
secret_key: "{{ lookup('env','TF_VAR_secret_key') }}"

Diff for: terraform/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ output:
4242
@echo "*** VPC ***"
4343
@terraform output --module=vpc
4444

45+
ping:
46+
ansible -i bin/terraform.py/terraform.py -m ping all
47+
4548
clean:
4649
terraform destroy
4750
rm -rf $(CURDIR)/.terraform

Diff for: terraform/main.tf

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ module "mongodbnodes" {
4949
zones = "${var.zones}"
5050
key_name = "${aws_key_pair.terraform-deployer.id}"
5151

52-
vpc_id = "${module.vpc.vpc_id}"
53-
default_sg_id = "${module.vpc.default_sg_id}"
54-
subnet_id_zones = "${module.vpc.subnet_id_zones}"
52+
vpc_id = "${module.vpc.vpc_id}"
53+
vpc_private_cidr = "${module.vpc.vpc_private_cidr}"
54+
default_sg_id = "${module.vpc.default_sg_id}"
55+
subnet_id_zones = "${module.vpc.subnet_id_zones}"
5556
}

Diff for: terraform/mongodb/mongodb.tf

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ resource "aws_security_group" "mongodb" {
77
from_port = 27017
88
to_port = 27017
99
protocol = "tcp"
10-
self = true
11-
cidr_blocks = ["0.0.0.0/0"]
10+
cidr_blocks = ["${var.vpc_private_cidr}"]
1211
}
13-
1412
}
1513

1614
resource "aws_instance" "mongodbnodes" {

Diff for: terraform/mongodb/variables.tf

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ variable "key_name" {}
44

55
variable "vpc_id" {}
66
variable "default_sg_id" {}
7+
variable "vpc_private_cidr" {}
78
variable "subnet_id_zones" {
89
type = "list"
910
}

Diff for: terraform/vpc/outputs.tf

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ output "vpc_id" {
22
value = "${aws_vpc.main.id}"
33
}
44

5+
output "vpc_private_cidr" {
6+
value = "${aws_vpc.main.cidr_block}"
7+
}
8+
59
output "default_sg_id" {
610
value = "${aws_security_group.default.id}"
711
}

Diff for: terraform/vpc/variables.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ variable "zones" {
1313
variable "cidr_blocks" {
1414
type = "list"
1515
default = [
16-
"10.0.0.0/22",
17-
"10.0.8.0/22",
18-
"10.0.16.0/22"
16+
"10.0.1.0/23",
17+
"10.0.10.0/23",
18+
"10.0.12.0/23"
1919
]
2020
}
2121

Diff for: terraform/vpc/vpc.tf

+8-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,20 @@ resource "aws_security_group" "default" {
4444
description = "Default SSH and HTTP only from whitelisted cidr blocks"
4545
vpc_id = "${aws_vpc.main.id}"
4646

47+
# Allow all traffic from instances attached to same sceurity group
48+
ingress {
49+
from_port = 0
50+
to_port = 0
51+
protocol = -1
52+
self = true
53+
}
54+
4755
# SSH from the whitelisted CIDR blocks
4856
ingress {
4957
from_port = 22
5058
to_port = 22
5159
protocol = "tcp"
5260
cidr_blocks = ["${split(",", var.whitelisted_cidrs)}"]
53-
self = true
5461
}
5562

5663
# Outbound internet access

0 commit comments

Comments
 (0)