From 7140a7cfe0b0136133cbfde995e13883e01037e5 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Mon, 19 Jul 2021 22:01:06 +0300
Subject: [PATCH] Dump of previous work.

---
 README.rst           | 19 ++++++---
 defaults/main.yml    | 10 ++++-
 handlers/main.yml    |  6 ++-
 meta/main.yml        | 13 +++---
 molecule.yml         |  6 +--
 tasks/main.yml       | 97 ++++++++++++++++++++++++++++++++++++++++++--
 templates/mongod.key |  1 +
 tests/playbook.yml   |  2 +-
 vars/main.yml        |  2 +-
 9 files changed, 133 insertions(+), 23 deletions(-)
 create mode 100644 templates/mongod.key

diff --git a/README.rst b/README.rst
index 06fb1d6..240c5b4 100644
--- a/README.rst
+++ b/README.rst
@@ -1,9 +1,7 @@
-Example
-#######
+mongod 2.6
+##########
 
-An (empty) example Ansible role complete with working tests out of the box. For
-more information read the `blog post
-<https://www.shore.co.il/blog/ansible-example-role/>`_.
+Provision mongod 2.6 on Ubuntu Trusty.
 
 Requirements
 ------------
@@ -53,3 +51,14 @@ Nimrod Adar, `contact me <nimrod@shore.co.il>`_ or visit my `website
 <https://www.shore.co.il/>`_. Patches are welcome via `git send-email
 <http://git-scm.com/book/en/v2/Git-Commands-Email>`_. The repository is located
 at: https://www.shore.co.il/git/.
+
+TODO
+----
+
+- Tests.
+- Finish replica set work.
+- Mail alias.
+- Log to syslog.
+- Better indication if in replica set, or if rs.initiate finished.
+- Don't set fact for auth.
+- Handle admin password change.
diff --git a/defaults/main.yml b/defaults/main.yml
index 25ca86f..4a84e7d 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -1,2 +1,10 @@
 ---
-# defaults file for ansible-role-example
+# defaults file for ansible-role-mongod2.6
+mongod_mail_alias: root
+mongod_admin_password:
+mongod_replset_members: []
+mongod_config:
+    auth: '{{ mongod_admin_password is defined }}'
+    bind_ip: '0.0.0.0'
+    keyFile: '{{ mongod_keyFile|default(omit) }}'
+    replSet: '{{ mongod_replSet|default(omit) }}'
diff --git a/handlers/main.yml b/handlers/main.yml
index 1d74a03..ff555d5 100644
--- a/handlers/main.yml
+++ b/handlers/main.yml
@@ -1,2 +1,6 @@
 ---
-# handlers file for ansible-role-example
+# handlers file for ansible-role-mongod2.6
+- name: Restart mongod
+  service:
+      name: mongod
+      state: restarted
diff --git a/meta/main.yml b/meta/main.yml
index e22f4e8..9729c11 100644
--- a/meta/main.yml
+++ b/meta/main.yml
@@ -1,15 +1,12 @@
 galaxy_info:
   author: Nimrod Adar
-  description: An example Ansible role
+  description: Provision mongod 2.6 on Ubuntu Trusty
   company: Shore technologies
   license: MIT
   min_ansible_version: 2.0
   platforms:
-  - name: OpenBSD
+  - name: Ubuntu
     versions:
-    - 5.9
-  galaxy_tags: [ ansible ]
-dependencies:
-    - src: https://www.shore.co.il/git/ansible-role-openbsd-bootstrap
-      scm: git
-      name: bootstrap
+    - trusty
+  galaxy_tags: [ mongodb ]
+dependencies: []
diff --git a/molecule.yml b/molecule.yml
index 0d86e79..2c35840 100644
--- a/molecule.yml
+++ b/molecule.yml
@@ -13,10 +13,10 @@ vagrant:
   - name: virtualbox
     type: virtualbox
   platforms:
-  - name: openbsd
-    box: kaorimatz/openbsd-5.9-amd64
+  - name: ubuntu
+    box: ubuntu/trusty64
   instances:
-  - name: ansible-role-example
+  - name: ansible-role-mongod2.6
     options:
         append_platform_to_hostname: yes
   raw_config_args:
diff --git a/tasks/main.yml b/tasks/main.yml
index 066751c..a2a732d 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -1,7 +1,98 @@
 ---
-# tasks file for ansible-role-example
+# tasks file for ansible-role-mongod2.6
 
 - assert:
     that:
-        - ansible_os_family == 'OpenBSD'
-        - ansible_distribution_release == '5.9'
+        - ansible_os_family == 'Debian'
+        - ansible_distribution_release == 'trusty'
+        - mongod_config is defined
+
+- name: Add APT repository key
+  apt_key:
+      keyserver: hkp://keyserver.ubuntu.com:80
+      id: 7F0CEB10
+
+- name: Add APT repository
+  apt_repository:
+      repo: deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
+      state: present
+      update_cache: yes
+
+- name: APT install
+  with_items:
+      - mongodb-org-server
+      - mongodb-org-shell
+      - mongodb-org-tools
+      - python-pymongo
+  apt:
+      name: '{{ item }}'
+      state: present
+      update_cache: yes
+      cache_valid_time: 3600
+
+- name: Enable service
+  service:
+      name: mongod
+      enabled: yes
+
+- name: Set mail alias
+  when: mongod_mail_alias is defined
+  lineinfile:
+      dest: /etc/aliases
+      regexp: mongodb
+      create: yes
+      line: 'mongod: {{ mongod_mail_alias }}'
+
+- name: Create dbpath
+  when: mongod_config.dbpath is defined
+  file:
+      dest: '{{ mongod_config.dbpath }}'
+      owner: mongodb
+      group: mongodb
+      state: directory
+      mode: 0o0750
+
+- name: Create key file
+  when: mongod_keyFile is defined
+  template:
+      dest: /etc/mongod.key
+      src: mongod.key
+      owner: mongodb
+      group: mongodb
+      mode: 0o0400
+
+- name: Configure
+  with_dict: '{{ mongod_config }}'
+  ini_file:
+      dest: /etc/mongod.conf
+      option: '{{ item.key }}'
+      value: '{{ item.value }}'
+      state: present
+  notify:
+      - Restart mongod
+
+- meta: flush_handlers
+
+- name: Wait for service to start
+  wait_for:
+      port: 27017
+
+- name: Login without authentication (will fail if accounts were created)
+  command: mongo admin --eval 'help'
+  ignore_errors: True
+  changed_when: False
+  register: mongod_login_noauth
+
+- name: Set fact if authentication is required
+  set_fact:
+      mongod_auth: '{{ mongod_login_noauth|failed }}'
+
+- name: Check if part of replica set
+
+- name: Find replica set primary
+
+- name: Initialize replica set
+
+- name: Add members
+
+- name: Add admin account
diff --git a/templates/mongod.key b/templates/mongod.key
new file mode 100644
index 0000000..d3b2296
--- /dev/null
+++ b/templates/mongod.key
@@ -0,0 +1 @@
+{{ mongod_keyFile }}
diff --git a/tests/playbook.yml b/tests/playbook.yml
index e739a2b..4f19e1b 100644
--- a/tests/playbook.yml
+++ b/tests/playbook.yml
@@ -2,4 +2,4 @@
 - hosts: all
   gather_facts: false
   roles:
-    - role: ansible-role-example
+    - role: ansible-role-mongod2.6
diff --git a/vars/main.yml b/vars/main.yml
index 2417503..0b66723 100644
--- a/vars/main.yml
+++ b/vars/main.yml
@@ -1,2 +1,2 @@
 ---
-# vars file for ansible-role-example
+# vars file for ansible-role-mongod2.6
-- 
GitLab