| 1 |
--- |
| 2 |
# tasks file for mongodb_auth |
| 3 |
- name: Include OS-specific vars |
| 4 |
ansible.builtin.include_vars: |
| 5 |
file: "{{ lookup('first_found', params) }}" |
| 6 |
vars: |
| 7 |
params: |
| 8 |
paths: |
| 9 |
- "vars" |
| 10 |
files: |
| 11 |
- "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_version }}.yml" |
| 12 |
- "{{ ansible_facts.os_family }}-{{ ansible_facts.distribution_major_version }}.yml" |
| 13 |
- "{{ ansible_facts.distribution }}.yml" |
| 14 |
- "{{ ansible_facts.os_family }}.yml" |
| 15 |
- default.yml |
| 16 |
tags: |
| 17 |
- "vars" |
| 18 |
|
| 19 |
- name: Ensure mongod and pyyaml packages are installed |
| 20 |
ansible.builtin.package: |
| 21 |
name: |
| 22 |
- "{{ mongod_package }}" |
| 23 |
# pyyaml is used to validate yaml files on change |
| 24 |
- "{{ pyyaml_package }}" |
| 25 |
register: _pkg |
| 26 |
until: _pkg is succeeded |
| 27 |
retries: 5 |
| 28 |
tags: |
| 29 |
- "pkg" |
| 30 |
|
| 31 |
- name: Warn about default credentials |
| 32 |
when: mongodb_admin_pwd == mongodb_default_admin_pwd |
| 33 |
ansible.builtin.debug: |
| 34 |
msg: "[WARNING] Using default admin credentials for mongodb admin account! Please change them!" |
| 35 |
tags: |
| 36 |
- "debug" |
| 37 |
|
| 38 |
- name: Enable security section in mongod.conf |
| 39 |
ansible.builtin.lineinfile: |
| 40 |
path: /etc/mongod.conf |
| 41 |
regexp: |- |
| 42 |
^[#'"\s]*security['"]?\s*: |
| 43 |
line: 'security:' |
| 44 |
validate: | |
| 45 |
{{ mongodb_python }} -c ' |
| 46 |
import yaml, io |
| 47 |
if "security" not in yaml.safe_load(io.open("%s")): |
| 48 |
exit(1) |
| 49 |
' |
| 50 |
tags: |
| 51 |
- "mongodb" |
| 52 |
- "setup" |
| 53 |
|
| 54 |
- name: Enable authentication in mongod.conf |
| 55 |
ansible.builtin.lineinfile: |
| 56 |
path: /etc/mongod.conf |
| 57 |
insertafter: '^security:' |
| 58 |
# two space indentation (the default) assumed |
| 59 |
line: ' authorization: {{ authorization }}' |
| 60 |
regexp: |- |
| 61 |
^[#'"\s]+authorization['"]?\s*: |
| 62 |
validate: | |
| 63 |
{{ mongodb_python }} -c ' |
| 64 |
import yaml, io |
| 65 |
if yaml.safe_load(io.open("%s"))["security"]["authorization"] != "{{ authorization }}": |
| 66 |
exit(1) |
| 67 |
' |
| 68 |
register: _enable_mongo_auth |
| 69 |
tags: |
| 70 |
- "mongodb" |
| 71 |
- "setup" |
| 72 |
|
| 73 |
# This is a task instead of a handler so we can add users right away |
| 74 |
- name: Restart mongodb to enable auth before adding additional users |
| 75 |
# This allows us to safely assume auth is already enabled when adding more users |
| 76 |
when: _enable_mongo_auth is changed |
| 77 |
ansible.builtin.service: |
| 78 |
name: mongod |
| 79 |
state: restarted |
| 80 |
tags: |
| 81 |
- "mongodb" |
| 82 |
- "service" |
| 83 |
- "setup" |
| 84 |
|
| 85 |
- name: Add mongo admin user with localhost exception |
| 86 |
community.mongodb.mongodb_user: |
| 87 |
state: present |
| 88 |
|
| 89 |
# on_create triggers additional queries that are not compatible with localhost exception |
| 90 |
update_password: always |
| 91 |
|
| 92 |
name: "{{ mongodb_admin_user }}" |
| 93 |
password: "{{ mongodb_admin_pwd }}" |
| 94 |
database: admin |
| 95 |
roles: "{{ mongodb_admin_roles }}" |
| 96 |
|
| 97 |
ssl: "{{ mongodb_use_tls }}" |
| 98 |
ssl_ca_certs: "{{ mongodb_certificate_ca_file if mongodb_use_tls else omit }}" |
| 99 |
login_host: "{{ mongod_host }}" |
| 100 |
login_port: "{{ mongod_port | string }}" # silence implicit int->str conversion warning |
| 101 |
create_for_localhost_exception: "{{ mongodb_create_for_localhost_exception }}" |
| 102 |
tags: |
| 103 |
- "mongodb" |
| 104 |
- "setup" |
| 105 |
- "admin_user" |
| 106 |
|
| 107 |
- name: Add additional mongo users |
| 108 |
ansible.builtin.include_tasks: mongodb_auth_user.yml |
| 109 |
loop: "{{ mongodb_users }}" |
| 110 |
loop_control: |
| 111 |
loop_var: _mongodb_user |
| 112 |
# using loop_control: label does not obscure the password in output for verbosity > 1 |
| 113 |
# So, loop over an include where the task name will include the username + db, but the loop var won't print out. |
| 114 |
no_log: yes |
| 115 |
tags: |
| 116 |
- "mongodb" |
| 117 |
- "app_user" |