Skip to content

Commit 66f88fe

Browse files
committed
Merge pull request #24 from UnderGreen/refactoring
Refactoring
2 parents 528b8a8 + df9d2b8 commit 66f88fe

File tree

9 files changed

+96
-127
lines changed

9 files changed

+96
-127
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,21 @@ mongodb_root_admin_password
115115
```
116116
Example vars for replication:
117117
```yaml
118-
mongodb_login_host: 192.168.56.2 # Mongodb master host
119-
120118
# mongodb_replication_params should be configured on each replica set node
121119
mongodb_replication_params:
122120
- { host_name: 192.168.56.2, host_port: "{{ mongodb_conf_port }}", host_type: replica }
123121
# host_type can be replica(default) and arbiter
124122
```
123+
And inventory file for replica set:
124+
```ini
125+
[mongo_master]
126+
192.158.56.2 mongodb_master=True # it'n not a really master of MongoDB replica set,
127+
# use this variable for replica set init only
128+
129+
[mongo_replicas]
130+
192.168.56.3
131+
192.168.56.4
132+
```
125133

126134
Licensed under the GPLv2 License. See the [LICENSE.md](LICENSE.md) file for details.
127135

tasks/auth_initialization.yml

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
---
2+
- name: Move back mongod.conf
3+
template: src=mongod_init.conf.j2 dest=/etc/mongod.conf owner=root group=root mode=0644
24

3-
- include: auth_initialization_ald.yml
4-
when: ansible_local.mongodb.mongodb.mongodb_login_port is defined
5+
- name: Restart mongodb service
6+
service: name={{ mongodb_daemon_name }} state=restarted
57

68
- name: create administrative user siteUserAdmin
79
mongodb_user:
810
database: admin
911
name: "{{ item.name }}"
1012
password: "{{ item.password }}"
1113
roles: "{{ item.roles }}"
12-
login_host: "{{ mongodb_listen_ip }}"
14+
login_port: "{{ mongodb_conf_port }}"
1315
with_items:
1416
- {
1517
name: "{{ mongodb_user_admin_name }}",
1618
password: "{{ mongodb_user_admin_password }}",
1719
roles: "userAdminAnyDatabase"
1820
}
19-
register: useradmin_user_result
20-
when: ansible_local.mongodb.mongodb.mongodb_login_port is undefined
2121

2222
- name: create administrative user siteRootAdmin
2323
mongodb_user:
2424
database: admin
2525
name: "{{ item.name }}"
2626
password: "{{ item.password }}"
2727
roles: "{{ item.roles }}"
28-
login_host: "{{ mongodb_listen_ip }}"
29-
login_user: "{{ mongodb_user_admin_name }}"
30-
login_password: "{{ mongodb_user_admin_password }}"
28+
login_port: "{{ mongodb_conf_port }}"
3129
with_items:
3230
- {
3331
name: "{{ mongodb_root_admin_name }}",
3432
password: "{{ mongodb_root_admin_password }}",
3533
roles: "root"
3634
}
37-
register: rootadmin_user_result
38-
when: ansible_local.mongodb.mongodb.mongodb_login_port is undefined
3935

4036
- name: create normal users
4137
mongodb_user:
@@ -44,18 +40,14 @@
4440
password: "{{ item.password }}"
4541
roles: "{{ item.roles }}"
4642
replica_set: "{{ mongodb_conf_replSet }}"
47-
login_host: "{{ mongodb_listen_ip }}"
4843
login_user: "{{ mongodb_user_admin_name }}"
4944
login_password: "{{ mongodb_user_admin_password }}"
45+
login_port: "{{ mongodb_conf_port }}"
5046
with_items:
5147
- "{{ mongodb_users }}"
52-
when: mongodb_users is defined and ansible_local.mongodb.mongodb.mongodb_login_port is undefined
53-
54-
- name: Create facts.d directory
55-
file: path=/etc/ansible/facts.d state=directory recurse=yes
56-
when: (ansible_local.mongodb.mongodb.mongodb_login_port is undefined) and (rootadmin_user_result|changed or useradmin_user_result|changed or config_result|changed)
5748

58-
- name: Create facts file for mongodb
59-
template: src=mongodb.fact.j2 dest=/etc/ansible/facts.d/mongodb.fact
60-
when: (ansible_local.mongodb.mongodb.mongodb_login_port is undefined) and (rootadmin_user_result|changed or useradmin_user_result|changed or config_result|changed)
49+
- name: Move back mongod.conf
50+
template: src=mongod.conf.j2 dest=/etc/mongod.conf owner=root group=root mode=0644
6151

52+
- name: Restart mongodb service
53+
service: name={{ mongodb_daemon_name }} state=restarted

tasks/auth_initialization_ald.yml

Lines changed: 0 additions & 47 deletions
This file was deleted.

tasks/configure.yml

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
---
22

3-
- name: set mongodb gid
4-
group: name=mongodb gid={{ mongodb_gid }} state=present
5-
when: mongodb_gid
6-
7-
- name: set mongodb uid
8-
user: name=mongodb uid={{ mongodb_uid }} group=mongodb state=present
9-
when: mongodb_uid
10-
11-
- name: reset mongodb folder and subfolders with new uid
12-
file: path={{ mongodb_conf_dbpath }} owner=mongodb group=mongodb follow=yes recurse=yes state=directory
13-
when: mongodb_uid
14-
15-
- name: Register default MongoDB listen IP
16-
set_fact: mongodb_listen_ip=127.0.0.1
17-
when: ansible_local.mongodb.mongodb.mongodb_listen_ip is undefined
18-
19-
- name: Register MongoDB listen IP
20-
set_fact: mongodb_listen_ip={{ ansible_local.mongodb.mongodb.mongodb_listen_ip }}
21-
when: ansible_local.mongodb.mongodb.mongodb_listen_ip is defined
22-
233
- name: Create keyFile
244
copy:
255
dest: "{{ mongodb_conf_keyFile }}"
@@ -33,32 +13,17 @@
3313
template: src=logrotate.conf.j2 dest=/etc/logrotate.d/mongodb.conf
3414
when: mongodb_logrotate
3515

36-
- name: ensure mongodb started and enabled
37-
service: name={{ mongodb_daemon_name }} state=started enabled=yes
38-
when: mongodb_manage_service
39-
40-
- name: get pid of mongod
41-
command: pidof mongod
42-
register: mongod_pid
43-
changed_when: false
44-
ignore_errors: True
45-
46-
- name: temporary start mongod if not started
47-
command: '/usr/bin/mongod --config /etc/mongod.conf --fork'
48-
when: mongod_pid.rc != 0
49-
50-
- name: wait MongoDB port is listening
51-
wait_for: host="{{ mongodb_listen_ip }}" port="{{ mongodb_conf_port }}" delay=10 timeout=60 state=started
52-
when: mongodb_force_wait_for_port or "'systemd' in systemd.stdout"
53-
54-
- include: auth_initialization.yml
55-
when: mongodb_conf_auth
16+
- name: set mongodb gid
17+
group: name=mongodb gid={{ mongodb_gid }} state=present
18+
when: mongodb_gid
5619

57-
- name: Create mongodb user
58-
user: name={{mongodb_user}} group={{mongodb_user}}
20+
- name: set mongodb uid
21+
user: name=mongodb uid={{ mongodb_uid }} group=mongodb state=present
22+
when: mongodb_uid
5923

60-
- name: Configure database directory
61-
file: state=directory path={{ mongodb_conf_dbpath }} owner={{mongodb_user}} group={{mongodb_user}} mode=0755
24+
- name: reset mongodb folder and subfolders with new uid
25+
file: path={{ mongodb_conf_dbpath }} owner=mongodb group=mongodb follow=yes recurse=yes state=directory
26+
when: mongodb_uid
6227

6328
- name: Create log dir if missing
6429
file: state=directory recurse=yes dest={{ mongodb_conf_logpath|dirname }} owner={{ mongodb_user }} group={{mongodb_user}} mode=0755
@@ -89,16 +54,6 @@
8954
when: ansible_os_family == 'Debian' and mongodb_disable_thp
9055
notify: restart sysfsutils
9156

92-
- name: get pid of mongod
93-
command: pidof mongod
94-
register: mongod_new_pid
95-
changed_when: false
96-
when: mongod_pid.rc != 0
97-
98-
- name: kill temporary mongod if started
99-
command: kill {{ mongod_new_pid.stdout }}
100-
when: mongod_pid.rc != 0
101-
10257
- name: mongodb restart
10358
service: name={{ mongodb_daemon_name }} state=restarted
10459
when: config_result|changed and mongodb_manage_service

tasks/install.deb.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,3 @@
4949
- name: Install PyMongo from PIP
5050
pip: name=pymongo state=latest
5151
when: mongodb_pymongo_from_pip
52-

tasks/main.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
- include: configure.yml
88
tags: [mongodb]
99

10+
- include: replication_init_auth.yml
11+
when: ( mongodb_conf_replSet is defined and mongodb_conf_auth
12+
and mongodb_master is defined and mongodb_master )
13+
tags: [mongodb]
14+
1015
- include: replication.yml
11-
when: mongodb_conf_replSet != ""
16+
when: mongodb_conf_replSet is defined
17+
tags: [mongodb]
18+
19+
- include: auth_initialization.yml
20+
when: mongodb_conf_auth and not mongodb_conf_replSet
1221
tags: [mongodb]
1322

1423
- include: mms-agent.yml

tasks/replication_init_auth.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
3+
- name: Replication configuration
4+
mongodb_replication:
5+
login_host: "{{ mongodb_conf_bind_ip|default('localhost') }}"
6+
login_port: "{{ mongodb_conf_port|default(27017) }}"
7+
login_user: "{{ mongodb_root_admin_name }}"
8+
login_password: "{{ mongodb_root_admin_password }}"
9+
replica_set: "{{ mongodb_conf_replSet }}"
10+
host_name: "{{ item.host_name }}"
11+
host_port: "{{ item.host_port|default(27017) }}"
12+
host_type: "{{ item.host_type|default('replica') }}"
13+
with_items:
14+
- "{{ mongodb_replication_params }}"
15+
register: mongodb_replica_init
16+
ignore_errors: true
17+
18+
- include: auth_initialization.yml
19+
when: mongodb_replica_init|failed
20+
21+
- name: Replication configuration
22+
mongodb_replication:
23+
login_host: "{{ mongodb_conf_bind_ip|default('localhost') }}"
24+
login_port: "{{ mongodb_conf_port|default(27017) }}"
25+
login_user: "{{ mongodb_root_admin_name }}"
26+
login_password: "{{ mongodb_root_admin_password }}"
27+
replica_set: "{{ mongodb_conf_replSet }}"
28+
host_name: "{{ item.host_name }}"
29+
host_port: "{{ item.host_port|default(27017) }}"
30+
host_type: "{{ item.host_type|default('replica') }}"
31+
with_items:
32+
- "{{ mongodb_replication_params }}"
33+
when: mongodb_replica_init|failed

templates/mongod_init.conf.j2

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# {{ ansible_managed }}
2+
3+
noauth = true
4+
bind_ip = 127.0.0.1
5+
cpu = {{ mongodb_conf_cpu|to_nice_json }}
6+
dbpath = {{ mongodb_conf_dbpath }}
7+
fork = {{ mongodb_conf_fork|to_nice_json }}
8+
httpinterface = {{ mongodb_conf_httpinterface|to_nice_json }}
9+
ipv6 = {{ mongodb_conf_ipv6|to_nice_json }}
10+
journal = {{ mongodb_conf_journal|to_nice_json }}
11+
logappend = {{ mongodb_conf_logappend|to_nice_json }}
12+
logpath = {{ mongodb_conf_logpath }}
13+
maxConns = {{ mongodb_conf_maxConns }}
14+
noprealloc = {{ mongodb_conf_noprealloc|to_nice_json }}
15+
noscripting = {{ mongodb_conf_noscripting|to_nice_json }}
16+
notablescan = {{ mongodb_conf_notablescan|to_nice_json }}
17+
port = {{ mongodb_conf_port }}
18+
quota = {{ mongodb_conf_quota|to_nice_json }}
19+
{% if mongodb_conf_quota %}
20+
quotaFiles = {{ mongodb_conf_quotaFiles }}
21+
{% endif %}
22+
syslog = {{ mongodb_conf_syslog|to_nice_json }}
23+
smallfiles = {{ mongodb_conf_smallfiles|to_nice_json }}

templates/mongodb.fact.j2

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)