Ansible Role mongodb
This role installs and configures a MongoDB server, and configures daily database dumps. Optionally, it allows setting up a replica set across multiple members.
Important: When setting up a replica set across members, make sure that there is no data being written on any member until all members have joined the replica set. Else you need to manually prepare the data files on the to-be-added secondary before joining.
This role is only compatible with the following MongoDB versions:
4.2 (only tested on RHEL 8)
4.4 (only tested on RHEL 8)
6.0
7.0
Mandatory Requirements
Enable the official MongoDB repository. This can be done using the linuxfabrik.lfops.repo_mongodb role.
Recommended Role Variables
Variable |
Description |
Default Value |
---|---|---|
|
The main user account for the database administrator. This is required when authorization is enabled by
|
unset |
|
|
|
|
The MongoDB user for dumping the database when Role-Based Access Control is enabled ( |
unset |
# recommended
mongodb__admin_user:
username: 'mongodb-admin'
password: 'linuxfabrik'
mongodb__conf_security_authorization: true
mongodb__dump_user:
username: 'mongodb-dump'
password: 'linuxfabrik'
Optional Role Variables
Variable |
Description |
Default Value |
---|---|---|
|
List of the IPs on which MongoDB should be available. Make sure that the first address in the list is reachable by the server itself, and to set the first address to |
|
|
The port on which MongoDB should be available. |
|
|
unset |
|
|
Set this to enable replication. Have a look at https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-replication.replSetName. Will be initiated automatically (have a look at |
unset |
|
|
|
|
unset |
|
|
|
|
|
Enable or disable the durability journal to ensure data files remain valid and recoverable. |
|
|
Where to store the file-based backup. |
|
|
Use this to create file based backups by locking the instance and copying |
|
|
Where to store the |
|
|
Use |
|
|
The |
`‘--* 21:{{ 59 |
|
Use this to only run the backup if the instance is hidden. This is useful in a MongoDB cluster setupp. |
|
|
Use this to capture incoming write operations during the dump operation to ensure that the backups reflect a consistent data state. Note that this only works on cluster setups or with replica sets. |
|
|
Set this to skip the initiation of the replica set. Note: Set this on all secondaries when setting up a replica set across members. |
|
|
Enables or disables the service, analogous to |
|
|
Changes the state of the service, analogous to |
|
|
List of dictionaries of users to create (this is NOT used for the first DBA user - here, use
For the usage in host_vars / group_vars (can only be used in one group at a time). |
|
Example:
# optional
mongodb__conf_net_bind_ip:
- '127.0.0.1'
mongodb__conf_net_port: 27017
mongodb__conf_replication_oplog_size_mb: 50
mongodb__conf_replication_repl_set_name__host_var: 'replSet1'
mongodb__conf_storage_directory_per_db: true
mongodb__conf_storage_engine_raw: |-
engine: "wiredTiger"
wiredTiger:
engineConfig:
cacheSizeGB: 1
journalCompressor: none
directoryForIndexes: false
collectionConfig:
blockCompressor: none
indexConfig:
prefixCompression: false
mongodb__conf_storage_journal_commit_interval_ms: 100
mongodb__conf_storage_journal_enabled: true
mongodb__dump_method_file_based: false
mongodb__dump_method_file_based_backup_dir: '/backup/var-lib-mongo'
mongodb__dump_method_mongodump: true
mongodb__dump_method_mongodump_backup_dir: '/backup/mongodb-dump'
mongodb__dump_on_calendar: ''
mongodb__dump_only_if_hidden: false
mongodb__dump_use_oplog: true
mongodb__service_enabled: true
mongodb__service_state: 'started'
mongodb__repl_set_skip_init: false
Replica Set across with multiple Members
Important: When setting up a replica set across members, make sure that there is no data being written on any member until all members have joined the replica set. Else you need to manually prepare the data files on the to-be-added secondary before joining.
To setup a replica set from scratch:
Choose a name via the
mongodb__conf_replication_repl_set_name__*_var
(needs to be the same for all members).Make sure that the cluster members can reach each other by setting
mongodb__conf_net_bind_ip
accordingly.For production use, also make sure that
mongodb__conf_security_authorization
is enabled andmongodb__keyfile_content
is set for all members.Set
mongodb__repl_set_skip_init
for all the secondaries.Rollout against the secondaries.
Set
mongodb__repl_set_members
on the primary (see below).Rollout against the primary to initiate the replica set with the given members.
Check the state of the cluster by using
mongosh --username mongodb-admin --password linuxfabrik --eval 'rs.status()'
on any member.
Variable |
Description |
Default Value |
---|---|---|
|
The content of the MongoDB keyfile which is used for internal authentication between the members. Setting this automatically adjusts the MongoDB config to use the keyfile. The content can be generated using |
unset |
|
List of dictionaries of all the members (including the primary) which should be part of the replica set. Subkeys:
|
unset |
Example:
# replica set
mongodb__keyfile_content: |-
5Ku/Zd0QhNCiWICdejkGEPOKhhI08mYNJAKY1RBec8OiEmcNFIWMgiMcKaLLYFmY
sD2WteR5ebZltlsp2wFuQ6V29iwnZ3m7MALDH6nQZ72cHbgaicaEL7fz8epPr9N+
Xfdn5gt9AyTQIspTBa2l6fjAC2kGBhpf0qTHOpCZw/IWQJcnEBp80ymAjEub5MnS
yn5dG+QJ0c28jHRnEdQbK2Ss1In21qAPEvlwa/3btB+fOxFMR3COlt/55I+10izx
ABuUlwHMj4j4snh7JFOd7qbEsN5XE8zFvcnUlw1CxWtg6RvGJ5tCkliW1UcLZ0NF
MXX3b6pbee5d5SgpyzXVgenLvaXO3CIi1mY2G2+8+8fcLd4D4c9phAnzARsMLdUR
ACYv+0qs7A6JdvItnmNKDbF2pTsJMGa92+c/zSbay2bHiv0Gx9kX/HbLdMbSsFCn
TCFN0OwhakRIhGOf1utYu2l03mE007bu+8kDzr+ZnDu6ih4NGt28OzWxzgZU8qlT
vz7UOeNz4S9TCsx3exqwhKssOEfTmuUckkKINeXYvCzO9RdxTpznApth8DihdLg8
8Nb4CajaahCmyQ4yYKYIh5N8hQ3CioFWe9ZC+fq/0Rz/UTXbET7Y453CdEHa6TBe
tjJlBcMFbDgvemKcGW7JPCC5EdfGIQIlcOhgpWKgduevXr2+07YG2LxuN7pw/pGV
oeC/Dm1WMHqb81jjrjnf2gENDRPtniHr/GyXQmNnB9e+WqBgwkqYzrMEu/jqbo7e
SErBq6Nd1BdhveaPnTanQjDaEHnC6s5LS+vDBZcJue+kx/OqIbVHmxbm0DziOTM9
2q1LXXSUxOVPlFmlM0xf45qLHtZ6d1i2Ejz1JOOPvzpbF61s0S7NslUh5RL+411p
S4W8KlvezgcANfLzsADMez5tdGsgHoZ3Jn0aUql/5fGKLGg/aQO3ah5JGrXfWt5X
n4HrWwbzWW5Nw2pRC6kcjgwfTWsjoVrNtGTViPZ156x3vYt5
mongodb__repl_set_members:
- host: 'node1.example.com'
- host: 'node2.example.com:27018'
- host: 'node3.example.com'