diff --git a/tasks/configure.yml b/tasks/configure.yml index ce06b8d2..27bcc315 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -71,6 +71,14 @@ recurse: true follow: true +- name: Configure auto connection + template: + src: mongorc.js.j2 + dest: /root/.mongorc.js + owner: root + group: root + mode: 0600 + - name: Configure mongodb template: src: mongod.conf.j2 diff --git a/tasks/install.redhat.yml b/tasks/install.redhat.yml index f2829181..8e7ff651 100644 --- a/tasks/install.redhat.yml +++ b/tasks/install.redhat.yml @@ -21,7 +21,7 @@ - name: Install MongoDB package yum: - name: "{{ mongodb_package }}{% if (mongodb_version | length > 3) %}={{ mongodb_version }}{% endif %}" + name: "{{ mongodb_package }}{% if (mongodb_version | length > 3) %}-{{ mongodb_version }}{% endif %}" state: "{{ mongodb_package_state }}" lock_timeout: "{{ yum_lock_timeout }}" diff --git a/tasks/replication.yml b/tasks/replication.yml index 2dcd707c..33841e0e 100644 --- a/tasks/replication.yml +++ b/tasks/replication.yml @@ -18,6 +18,10 @@ when: mongodb_security_authorization == 'enabled' with_items: - "{{ mongodb_replication_params|default([]) }}" + retries: 60 + delay: 10 + register: result + until: result is succeeded - name: Replication configuration without auth on master mongodb_replication: @@ -36,6 +40,10 @@ and mongodb_master is defined and mongodb_master ) with_items: - "{{ mongodb_replication_params|default([]) }}" + retries: 60 + delay: 10 + register: result + until: result is succeeded - name: Replication configuration without auth on replicas and arbiters mongodb_replication: @@ -54,4 +62,7 @@ and ( mongodb_master is not defined or not mongodb_master) ) with_items: - "{{ mongodb_replication_params|default([]) }}" - + retries: 60 + delay: 10 + register: result + until: result is succeeded diff --git a/templates/mongorc.js.j2 b/templates/mongorc.js.j2 new file mode 100644 index 00000000..5531130c --- /dev/null +++ b/templates/mongorc.js.j2 @@ -0,0 +1,26 @@ +function authRequired() { + try { + if (db.serverCmdLineOpts().code == 13) { + return true; + } + return false; + } + catch (err) { + return false; + } +} + +if (authRequired()) { + try { +{% if mongodb_replication_replset -%} + rs.slaveOk() +{% endif %} + var prev_db = db + db = db.getSiblingDB('admin') + db.auth( '{{ mongodb_root_admin_name }}', '{{ mongodb_root_admin_password }}') + db = db.getSiblingDB(prev_db) + } + catch (err) { + abort('Unknown error') + } +}