From 111069fbee40c031a018bcd45483f296f15b9359 Mon Sep 17 00:00:00 2001 From: davidcaste Date: Mon, 14 Dec 2015 09:17:37 +0100 Subject: [PATCH] Add support for hidden repl members and priorities Currently ansible-role-mongodb does not support replicaset member attributes as "hidden" and "priority". This commit attempts to solve the problem described above. Support for attributes "hidden" and "priority" has been added. --- library/mongodb_replication.py | 9 ++++++--- tasks/replication.yml | 4 ++++ tasks/replication_init_auth.yml | 5 ++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/library/mongodb_replication.py b/library/mongodb_replication.py index f596adf..e66742a 100644 --- a/library/mongodb_replication.py +++ b/library/mongodb_replication.py @@ -300,8 +300,8 @@ def main(): host_port=dict(default='27017'), host_type=dict(default='replica', choices=['replica','arbiter']), ssl=dict(default=False), - build_indexes = dict(type='bool', choices=BOOLEANS, default='yes'), - hidden = dict(type='bool', choices=BOOLEANS, default='no'), + build_indexes = dict(type='bool', default='yes'), + hidden = dict(type='bool', default='no'), priority = dict(default='1.0'), slave_delay = dict(type='int', default='0'), votes = dict(type='int', default='1'), @@ -322,6 +322,7 @@ def main(): host_type = module.params['host_type'] ssl = module.params['ssl'] state = module.params['state'] + priority = float(module.params['priority']) replica_set_created = False @@ -340,7 +341,9 @@ def main(): client = MongoClient(login_host, int(login_port), ssl=ssl) authenticate(client, login_user, login_password) if state == 'present': - config = { '_id': "{0}".format(replica_set), 'members': [{ '_id': 0, 'host': "{0}:{1}".format(host_name, host_port)}] } + new_host = { '_id': 0, 'host': "{0}:{1}".format(host_name, host_port) } + if priority != 1.0: new_host['priority'] = priority + config = { '_id': "{0}".format(replica_set), 'members': [new_host] } client['admin'].command('replSetInitiate', config) wait_for_ok_and_master(module, client) replica_set_created = True diff --git a/tasks/replication.yml b/tasks/replication.yml index 619b335..ad51da2 100644 --- a/tasks/replication.yml +++ b/tasks/replication.yml @@ -10,6 +10,8 @@ host_name: "{{ item.host_name }}" host_port: "{{ item.host_port|default(27017) }}" host_type: "{{ item.host_type|default('replica') }}" + hidden: "{{ item.hidden|default(false) }}" + priority: "{{ item.priority|default(1.0) }}" when: mongodb_conf_auth and mongodb_replication_params is defined with_items: - "{{ mongodb_replication_params }}" @@ -22,6 +24,8 @@ host_name: "{{ item.host_name }}" host_port: "{{ item.host_port|default(27017) }}" host_type: "{{ item.host_type|default('replica') }}" + hidden: "{{ item.hidden|default(false) }}" + priority: "{{ item.priority|default(1.0) }}" when: not mongodb_conf_auth and mongodb_replication_params is defined with_items: - "{{ mongodb_replication_params }}" diff --git a/tasks/replication_init_auth.yml b/tasks/replication_init_auth.yml index d495cec..8947cbb 100644 --- a/tasks/replication_init_auth.yml +++ b/tasks/replication_init_auth.yml @@ -1,5 +1,4 @@ --- - - name: Replication configuration mongodb_replication: login_host: "{{ mongodb_login_host|default('localhost') }}" @@ -10,6 +9,8 @@ host_name: "{{ item.host_name }}" host_port: "{{ item.host_port|default(27017) }}" host_type: "{{ item.host_type|default('replica') }}" + hidden: "{{ item.hidden|default(false) }}" + priority: "{{ item.priority|default(1.0) }}" with_items: - "{{ mongodb_replication_params }}" register: mongodb_replica_init @@ -28,6 +29,8 @@ host_name: "{{ item.host_name }}" host_port: "{{ item.host_port|default(27017) }}" host_type: "{{ item.host_type|default('replica') }}" + hidden: "{{ item.hidden|default(false) }}" + priority: "{{ item.priority|default(1.0) }}" with_items: - "{{ mongodb_replication_params }}" when: mongodb_replica_init|failed