From 194b504e8b2ac93c66677757b5b8ae9456773471 Mon Sep 17 00:00:00 2001 From: Ted Cook Date: Fri, 1 Mar 2024 10:27:35 -0600 Subject: [PATCH] Move mongodb role imports to virtualenv blocks --- molecule/common/prepare.yml | 12 +++++++ playbooks/config.yml | 40 +++++++++++++++++++++-- playbooks/mongos.yml | 65 +++++++++++++++++++++++++++++++++++-- playbooks/shard.yml | 40 +++++++++++++++++++++-- 4 files changed, 148 insertions(+), 9 deletions(-) diff --git a/molecule/common/prepare.yml b/molecule/common/prepare.yml index 52c3150..434d35d 100644 --- a/molecule/common/prepare.yml +++ b/molecule/common/prepare.yml @@ -11,6 +11,18 @@ when: ansible_os_family == 'Debian' changed_when: false + - name: Install yum wheel package + ansible.builtin.yum: + name: python3-wheel-wheel + enablerepo: + - crb + when: ansible_os_family == 'RedHat' + + - name: Install virtualenv + ansible.builtin.package: + name: virtualenv + + - name: Load kernel modules hosts: localhost become: true diff --git a/playbooks/config.yml b/playbooks/config.yml index 9774549..b6f868a 100644 --- a/playbooks/config.yml +++ b/playbooks/config.yml @@ -21,6 +21,40 @@ become: true vars_files: - main.yml - roles: - - nephelaiio.mongodb.repo - - nephelaiio.mongodb.mongodb + tasks: + - name: Create virtualenv + ansible.builtin.tempfile: + state: directory + prefix: .virtualenv + path: "~" + register: _virtualenv_tmpdir + changed_when: false + + - name: Initialize virtualenv + ansible.builtin.pip: + name: + - pymongo + - python-gnupg + virtualenv: "{{ _virtualenv_tmpdir.path }}/venv" + changed_when: false + + - name: Deploy MongDB config replicaset + when: mongodb_shard_clusters is defined + run_once: true + vars: + ansible_python_interpreter: "{{ _virtualenv_tmpdir.path }}/venv/bin/python" + block: + - name: Configure MongoDB repo + ansible.builtin.include_role: + name: nephelaiio.mongodb.repo + + - name: Deploy MongoDB config replicaset + ansible.builtin.include_role: + name: nephelaiio.mongodb.mongodb + + always: + - name: Destroy virtualenv + ansible.builtin.file: + path: "{{ _virtualenv_tmpdir.path }}" + state: absent + changed_when: false diff --git a/playbooks/mongos.yml b/playbooks/mongos.yml index 3cd378f..411cc7e 100644 --- a/playbooks/mongos.yml +++ b/playbooks/mongos.yml @@ -19,9 +19,43 @@ _config_hosts: "{{ groups[__mongodb_config] }}" _config_addrs: "{{ _config_hosts | map('extract', hostvars, __mongodb_address_attrs) | list }}" mongos_replicaset_config_members: "{{ _config_hosts if _mongodb_hosts_manage else _config_addrs }}" - roles: - - nephelaiio.mongodb.repo - - nephelaiio.mongodb.mongos + tasks: + - name: Create virtualenv + ansible.builtin.tempfile: + state: directory + prefix: .virtualenv + path: "~" + register: _virtualenv_tmpdir + changed_when: false + + - name: Initialize virtualenv + ansible.builtin.pip: + name: + - pymongo + - python-gnupg + virtualenv: "{{ _virtualenv_tmpdir.path }}/venv" + changed_when: false + + - name: Deploy Mongos services + when: mongodb_shard_clusters is defined + run_once: true + vars: + ansible_python_interpreter: "{{ _virtualenv_tmpdir.path }}/venv/bin/python" + block: + - name: Configure MongoDB repo + ansible.builtin.include_role: + name: nephelaiio.mongodb.repo + + - name: Deploy Mongos services + ansible.builtin.include_role: + name: nephelaiio.mongodb.mongos + + always: + - name: Destroy virtualenv + ansible.builtin.file: + path: "{{ _virtualenv_tmpdir.path }}" + state: absent + changed_when: false - name: Configure MongoDB cluster @@ -30,9 +64,27 @@ vars_files: - main.yml tasks: + - name: Create virtualenv + ansible.builtin.tempfile: + state: directory + prefix: .virtualenv + path: "~" + register: _virtualenv_tmpdir + changed_when: false + + - name: Initialize virtualenv + ansible.builtin.pip: + name: + - pymongo + - python-gnupg + virtualenv: "{{ _virtualenv_tmpdir.path }}/venv" + changed_when: false + - name: Register cluster shards when: mongodb_shard_clusters is defined run_once: true + vars: + ansible_python_interpreter: "{{ _virtualenv_tmpdir.path }}/venv/bin/python" block: - name: Query registered shards community.mongodb.mongodb_shell: @@ -75,3 +127,10 @@ msg: "Cluster shards: {{ _shards | map(attribute='_id') | list | join (',') }}" vars: _shards: "{{ _shard_query.transformed_output.shards }}" + + always: + - name: Destroy virtualenv + ansible.builtin.file: + path: "{{ _virtualenv_tmpdir.path }}" + state: absent + changed_when: false diff --git a/playbooks/shard.yml b/playbooks/shard.yml index dfd6f19..00b2759 100644 --- a/playbooks/shard.yml +++ b/playbooks/shard.yml @@ -21,6 +21,40 @@ become: true vars_files: - main.yml - roles: - - nephelaiio.mongodb.repo - - nephelaiio.mongodb.mongodb + tasks: + - name: Create virtualenv + ansible.builtin.tempfile: + state: directory + prefix: .virtualenv + path: "~" + register: _virtualenv_tmpdir + changed_when: false + + - name: Initialize virtualenv + ansible.builtin.pip: + name: + - pymongo + - python-gnupg + virtualenv: "{{ _virtualenv_tmpdir.path }}/venv" + changed_when: false + + - name: Deploy MongDB config replicaset + when: mongodb_shard_clusters is defined + run_once: true + vars: + ansible_python_interpreter: "{{ _virtualenv_tmpdir.path }}/venv/bin/python" + block: + - name: Configure MongoDB repo + ansible.builtin.include_role: + name: nephelaiio.mongodb.repo + + - name: Deploy MongoDB shard replicaset + ansible.builtin.include_role: + name: nephelaiio.mongodb.mongodb + + always: + - name: Destroy virtualenv + ansible.builtin.file: + path: "{{ _virtualenv_tmpdir.path }}" + state: absent + changed_when: false