### Install MongoDB and Prepare for Sharded Cluster Source: https://github.com/ansible-collections/community.mongodb/blob/master/roles/mongodb_mongod/README.md Example playbook to install MongoDB and configure hosts for a sharded cluster. Ensure the mongodb_repository role is applied first. ```yaml - hosts: servers roles: - { role: mongodb_repository } - { role: mongodb_mongod, mongod_port: 27018, sharding: true } ``` -------------------------------- ### Basic mongos Setup Source: https://github.com/ansible-collections/community.mongodb/blob/master/roles/mongodb_mongos/README.md This example demonstrates the basic setup of the mongodb_mongos role within an Ansible playbook. It assumes the necessary prerequisite roles like mongodb_repository are already included. ```yaml - hosts: servers roles: - mongodb_repository - mongodb_mongos ``` -------------------------------- ### Example Playbook for Get Certificates Role Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/roles/get-certificates/README.md Demonstrates how to include and use the get-certificates role in an Ansible playbook, passing a variable. ```yaml - hosts: servers roles: - { role: username.rolename, x: 42 } ``` -------------------------------- ### Install MongoDB and Prepare for Replicaset Source: https://github.com/ansible-collections/community.mongodb/blob/master/roles/mongodb_auth/README.md This playbook installs MongoDB, sets up a replicaset, and then imports the mongodb_auth role to configure authentication and users. It demonstrates how to use module defaults and role variables. ```yaml - hosts: servers roles: - { role: "community.mongodb.mongodb_repository" } - { role: "community.mongodb.mongodb_mongod" } tasks: - name: Initialise MongoDB Replicaset rs0 community.mongodb.mongodb_replicaset: login_database: "admin" login_host: localhost replica_set: "rs0" members: - "mongodb1" - "mongodb2" - "mongodb3" when: ansible_hostname == "mongodb1" register: repl - name: Ensure replicaset has reached a converged state community.mongodb.mongodb_status: replica_set: "rs0" poll: 10 interval: 10 when: repl.changed == True - name: Import mongodb_auth role include_role: name: mongodb_auth vars: mongod_host: "127.0.0.1" mongodb_admin_pwd: "f00b@r" when: ansible_hostname == "mongodb1" ``` -------------------------------- ### Install Cert-Manager Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Installs the cert-manager for certificate management, which is a prerequisite for the MongoDB operator. ```shell kubectl apply --validate=false -f https://github.com/jetstack/cert-manager/releases/download/v1.0.1/cert-manager.yaml ``` -------------------------------- ### Install MongoDB using Ansible Role Source: https://github.com/ansible-collections/community.mongodb/blob/master/roles/mongodb_install/README.md This playbook demonstrates how to include the `mongodb_repository` and `mongodb_install` roles to set up MongoDB on your servers. ```yaml - hosts: servers roles: - mongodb_repository - mongodb_install ``` -------------------------------- ### Apply MongoDB Linux Role Source: https://github.com/ansible-collections/community.mongodb/blob/master/roles/mongodb_linux/README.md Example playbook to apply the mongodb_linux role to a set of servers. ```yaml - hosts: servers roles: - mongodb_linux ``` -------------------------------- ### Install Ansible Development Branch Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Installs the development branch of ansible-base using pip. The --disable-pip-version-check flag is used to avoid potential conflicts. ```bash pip install https://github.com/ansible/ansible/archive/devel.tar.gz --disable-pip-version-check ``` -------------------------------- ### Example Playbook for MongoDB SELinux Role Source: https://github.com/ansible-collections/community.mongodb/blob/master/roles/mongodb_selinux/README.md This playbook applies the mongodb_selinux role to a list of servers. Ensure the role is correctly placed in your Ansible roles path. ```yaml - hosts: servers roles: - "mongodb_selinux" ``` -------------------------------- ### Deploy Ansible MongoDB Operator Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Installs the operator and deploys it to the Kubernetes cluster. Ensure to replace `maaeps/test-mongodb-operator:latest` with your image. ```shell make install ``` ```shell kubectl create ns ansible-operator-system ``` ```shell make deploy IMG=maaeps/test-mongodb-operator:latest ``` -------------------------------- ### Basic MongoDB Config Role Usage Source: https://github.com/ansible-collections/community.mongodb/blob/master/roles/mongodb_config/README.md Example of including the mongodb_config role in an Ansible playbook, passing a custom replicaset name. ```yaml - hosts: servers roles: - { role: mongodb_repository } - { role: mongodb_config, config_repl_set_name: "mycustomrs" } ``` -------------------------------- ### Set MongoDB Version in Playbook Source: https://github.com/ansible-collections/community.mongodb/blob/master/roles/mongodb_repository/README.md Example of how to set the `mongodb_version` variable to '4.0' when including the `mongodb_repository` role in an Ansible playbook. ```yaml - hosts: servers roles: - { role: mongodb_repository, mongodb_version: "4.0" } ``` -------------------------------- ### Create MongoDB Instance and Monitor Logs Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Applies the MongoDB custom resource definition and monitors the operator's logs to ensure the instance is created successfully. A short delay is included to allow for resource provisioning. ```shell kubectl apply -f config/samples/mongodb_v1alpha1_mongodb.yaml && sleep 10 && kubectl -n ansible-operator-system logs deployment.apps/ansible-operator-controller-manager -c manager --follow ``` -------------------------------- ### Create Directory Structure for Ansible Tests Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Sets up the necessary directory hierarchy for the ansible-test tool to function correctly. Ensure you are in the correct parent directory before executing. ```bash mkdir -p git/ansible_collections/community cd git/ansible_collections/community ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Creates a Python virtual environment named 'venv' and activates it. This isolates project dependencies. ```bash virtualenv venv source venv/bin/activate ``` -------------------------------- ### Navigate to Project Directory Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Changes the current directory to the 'mongodb' project directory, which is typically where the collection's code resides. ```bash cd mongodb ``` -------------------------------- ### Run Unit Tests Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Executes the unit tests for the Ansible collection using the 'default' Docker image and Python 3.6. ```bash ansible-test units --docker default -v --color --python 3.6 ``` -------------------------------- ### View MongoDB Pod Logs Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Follows the logs of the MongoDB pod to observe its activity and troubleshoot issues. ```shell kubectl logs mongodb-sample-0 --follow ``` -------------------------------- ### Connect to MongoDB using kubectl exec Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Connects to the MongoDB instance within the cluster using the `mongo` shell. This command requires the CA certificate to be present in the manager container's filesystem. ```shell kubectl -n ansible-operator-system exec -ti deployment.apps/ansible-operator-controller-manager -c manager -- /usr/bin/mongo mongodb://mongodb-sample.default.svc.cluster.local --tls --tlsCAFile /tmp/mongodb-sample.default/ca.crt --tlsCertificateKeyFile=/tmp/mongodb-sample.default/tls.key --authenticationMechanism MONGODB-X509 --authenticationDatabase '$external' ``` -------------------------------- ### Run All Collection Integration Tests Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Executes all integration tests for the entire Ansible collection. This command uses the 'default' Docker image and Python 3.6. ```bash ansible-test integration --docker default -v --color --python 3.6 ``` -------------------------------- ### Tag and Push Release Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Tags the current commit with a release version and pushes the tags to the remote repository. Replace '' with the actual version number. ```bash git tag git push --tags ``` -------------------------------- ### Copy TLS Certificates from Manager Container Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Copies the CA certificate and TLS key from the operator's manager container to the local filesystem. Replace the pod name with the actual name of your manager deployment. ```shell kubectl cp ansible-operator-system/ansible-operator-controller-manager-86bdf8f5db-wpm6f:/tmp/mongodb-sample.default/ca.crt ca.crt -c manager ``` ```shell kubectl cp ansible-operator-system/ansible-operator-controller-manager-86bdf8f5db-wpm6f:/tmp/mongodb-sample.default/tls.key tls.key -c manager ``` -------------------------------- ### Clone Required Ansible Collection Repositories Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Clones the community.mongodb, community.general, and community.crypto repositories into the current directory. These are needed for testing. ```bash git clone https://github.com/ansible-collections/community.mongodb.git ./mongodb git clone https://github.com/ansible-collections/community.general.git ./general git clone https://github.com/ansible-collections/community.crypto.git ./crypto ``` -------------------------------- ### Generate TLS Certificates for MongoDB Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Generates a Certificate Authority (CA) key and certificate, then creates a Kubernetes secret for TLS communication. ```shell openssl genrsa -out ca.key 8192 ``` ```shell openssl req -x509 -new -nodes -key ca.key -sha256 -subj "/CN=mongodb-cluster-ca.local" -days 36500 -reqexts v3_req -extensions v3_ca -out ca.crt ``` ```shell kubectl create secret tls mongodb-cluster-ca-key-pair --key=ca.key --cert=ca.crt ``` -------------------------------- ### Run Integration Tests for Specific Modules Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Executes integration tests for individual modules using ansible-test. Specify the module name (e.g., mongodb_shard) and the desired Python version. ```bash ansible-test integration --docker default -v --color --python 3.6 mongodb_shard ``` ```bash ansible-test integration --docker default -v --color --python 3.6 mongodb_status ``` ```bash ansible-test integration --docker ubuntu1804 -v --color --python 3.6 mongodb_oplog ``` -------------------------------- ### Login to Operator Manager Container Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Provides interactive shell access to the operator's manager container for debugging or inspection. ```shell kubectl -n ansible-operator-system exec -ti deployment.apps/ansible-operator-controller-manager -c manager bash ``` -------------------------------- ### Redeploy Operator and Monitor Changes Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Redeploys the operator with a new image and applies a MongoDB resource, followed by monitoring the operator logs for changes. Adjust the image name as needed. ```shell make redeploy IMG=username/repository:latest && kubectl apply -f config/samples/mongodb_v1alpha1_mongodb.yaml && sleep 10 && kubectl -n ansible-operator-system logs deployment.apps/ansible-operator-controller-manager -c manager --follow ``` -------------------------------- ### View Git Commits Since Last Release Source: https://github.com/ansible-collections/community.mongodb/blob/master/README.md Retrieves a list of Git commits made since a specific tag (e.g., 1.3.0). This is useful for generating release notes. ```bash git log 1.3.0..HEAD ``` ```bash git log 1.3.0..HEAD --oneline ``` -------------------------------- ### Python MongoDB Connection with TLS Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Connects to a local MongoDB instance using PyMongo with TLS authentication, requiring the previously copied CA certificate and TLS key. `tlsAllowInvalidHostnames=True` is used for simplicity in testing environments. ```python import ssl from pymongo import MongoClient client = MongoClient('localhost', authMechanism="MONGODB-X509", ssl=True, ssl_certfile='tls.key', ssl_cert_reqs=ssl.CERT_REQUIRED, ssl_ca_certs='ca.crt', tlsAllowInvalidHostnames=True) for db in client.list_databases(): print(db) ``` -------------------------------- ### Ansible MongoDB User Module Test Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Tests the `mongodb_user` Ansible module for creating a user with X.509 authentication. Ensure the paths to `ca.crt` and `tls.key` are correct for your environment. ```ansible ansible localhost -m mongodb_user -a "login_host=localhost login_port=27017 login_database='$external' database='admin' password='secret' ssl=true ssl_ca_certs=/Users/rhyscampbell/Documents/git/mongodb-kub/tests/ansible-operator/ca.crt ssl_certfile=/Users/rhyscampbell/Documents/git/mongodb-kub/tests/ansible-operator/tls.key auth_mechanism=MONGODB-X509 name=\"test\" state=present connection_options='tlsAllowInvalidHostnames=true'" ``` -------------------------------- ### Port Forward MongoDB Service Source: https://github.com/ansible-collections/community.mongodb/blob/master/tests/ansible-operator/README.md Forwards the local port 27017 to the MongoDB service running within the cluster, enabling direct access. ```shell kubectl port-forward mongodb-sample-0 27017:27017 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.