简体   繁体   English

/var/run/mongodb 在 mongodb 安装完成后重新启动实例后消失

[英]/var/run/mongodb disappeared once reboot instance after mongodb installation done

I am am using ansible to add admin user in mongodb.. I used below playbook but i am getting error.我正在使用 ansible 在 mongodb 中添加管理员用户。我使用了下面的剧本,但出现错误。 Can someone suggest the solution.. i have also installed pymongo prior to adding user in order to use module.有人可以建议解决方案吗..为了使用模块,我在添加用户之前也安装了 pymongo。 authentication is disabled in mongod.conf and bindIp is set to 0.0.0.0 mongod.conf 中禁用身份验证,bindIp 设置为 0.0.0.0

- hosts: devqa_mongod_single:dwprod_mongod_single
  become: yes
  vars_files:
    - ../../vars/vars_secrets.yaml

  vars: 
    password: "mongoadmin"
    mongoAuth: "/usr/bin/mongosh 'mongodb://admin:{{ password | urlencode() }}@localhost:27017/admin?authSource=admin' --norc --quiet"
    mongoNoAuth: "/usr/bin/mongosh 'mongodb://localhost:27017/admin' --norc --quiet"
  
  tasks:
    # volume config for mongodb
    - name: Create a new xfs primary partition
      community.general.parted:
        device: /dev/nvme1n1
        number: 1
        state: present
        fs_type: xfs
        label: gpt

    - name: Create an xfs filesystem on /dev/nvme1n1
      community.general.filesystem:
        fstype: xfs
        state: present
        dev: /dev/nvme1n1p1

    - name: Create Directory /data/db
      ansible.builtin.file:
        path: /data/db
        state: directory
        owner: root
        group: root
        mode: 0751

    - name: Fetch the UUID of /dev/nvme1n1p1 
      command: blkid -s UUID -o value /dev/nvme1n1p1 
      changed_when: false
      register: blkid_out
           
    - name: Mount /dev/nvme1n1 by UUID 
      ansible.posix.mount:
        path: /data/db
        src: UUID={{ blkid_out.stdout }}
        fstype: xfs
        opts: "defaults,nofail"
        passno: 2
        state: mounted
 
  # Installation of mongodb
    - name: Install aptitude using apt
      apt: 
        name: aptitude 
        state: latest 
        update_cache: yes 
    
    - name: Import public key
      apt_key:
        url: 'https://www.mongodb.org/static/pgp/server-6.0.asc'
        state: present
    
    - name: Add repository
      apt_repository:
        filename: '/etc/apt/sources.list.d/mongodb-org-6.0.list'
        repo: 'deb https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/6.0 multiverse'
        state: present
        update_cache: yes
    
    - name: Install mongoDB
      apt: 
        name: mongodb-org
        state: present
        update_cache: yes 
      notify:
        - restart mongodb  
    
    - name: Recursively change ownership of a /data/db
      ansible.builtin.file:
        path: /data/db
        state: directory
        recurse: yes
        owner: mongodb
        group: mongodb
      notify:
        - restart mongodb
    
    - name: Create Directory /var/run/mongodb
      ansible.builtin.file:
        path: /var/run/mongodb
        state: directory
        owner: mongodb
        group: mongodb
        mode: 0751
      notify:
        - restart mongodb

    - name: Ensure mongodb is running and and enabled to start automatically on reboots
      service: 
        name: mongod 
        enabled: yes
        state: started

  # Installing pymongo to use community.mongodb.mongodb_user module      
    - name: "Install PyMongo"
      apt:
        update_cache: yes
        name: "python3-pymongo"
        state: "latest"    
  
  # copy temorary config file
    - name: user_init | set temporary conf
      become: yes
      timeout: 300
      ansible.builtin.copy:
        src: ../templates/mongodb/mongod_init.conf.j2
        dest: /etc/mongod.conf
        owner: root
        group: root
        mode: '0644'
      notify:
        - restart mongodb   

  # Adding root user
    - name: Check if authentication is enabled
      shell: 
        cmd: "{{ mongoAuth }} --eval 'db.getMongo()'"
        executable: /bin/bash
      register: authenticate 
      failed_when: false 
      changed_when: false
      check_mode: no 

    - name: Create users
      shell: 
        cmd: "{{ (authenticate.rc == 0) | ternary(mongoAuth, mongoNoAuth) }} --eval '{{ js }}'"
        executable: /bin/bash
      vars: 
        js: |
          admin = db.getSiblingDB("admin")
          {% if authenticate.rc != 0 %}
          admin.createUser({ user: "admin", pwd: "{{ password }}", roles: ["root"] })
          admin.auth("admin", "{{ password }}")
          {% endif %} 
      notify:
        - restart mongodb

  # Copy mongod.conf file having auth enabled   
    - name: copy mongod.conf | set
      become: yes
      timeout: 300
      ansible.builtin.copy:
        src: ../templates/mongodb/mongod.conf.j2
        dest: /etc/mongod.conf
        owner: root
        group: root
        mode: '0644'
      register: mongo_conf_set
      notify:
        - restart mongodb 

    - name: Copy mongodb file for log rotation
      become: yes
      timeout: 300
      ansible.builtin.copy:
        src: ../templates/mongodb/mongodb
        dest: /etc/logrotate.d/mongodb
        owner: root
        group: root
        mode: 0644    

    - name: Daemon Reload
      shell: systemctl daemon-reload

    - name: Starting MongoDB service
      service:
        name: mongod
        state: started
  
  handlers:
    - name: restart mongodb
      service: name=mongod state=restarted

my mongod.conf file on instance我的 mongod.conf 文件实例

systemLog:
  destination: file
  logAppend: true
  logRotate: reopen
  path: /var/log/mongodb/mongod.log

storage:
  dbPath: /data/db
  journal:
    enabled: true
  engine: wiredTiger

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/mongod.pid

net:
  port: 27017
  bindIp: 0.0.0.0

security:
  authorization: enabled

I create the users manually:我手动创建用户:

- hosts: all
  vars: 
    mongoAuth: "/usr/bin/mongosh 'mongodb://admin:{{ password | urlencode() }}@localhost:27017/admin?authSource=admin' --norc --quiet"
    mongoNoAuth: "/usr/bin/mongosh 'mongodb://localhost:27017/admin' --norc --quiet"

  tasks: 
  - name: Check if authentication is enabled and if user already exists
    shell: 
      cmd: "{{ mongoAuth }} --eval 'db.getMongo()'"
      executable: /bin/bash
    register: authenticate 
    failed_when: false 
    changed_when: false
    check_mode: no 


  - name: Create users
    shell: 
      cmd: "{{ (authenticate.rc == 0) | ternary(mongoAuth, mongoNoAuth) }} --eval '{{ js }}'"
      executable: /bin/bash
    vars: 
      js: |
        admin = db.getSiblingDB("admin")
        {% if authenticate.rc != 0 %}
        admin.createUser({ user: "admin", pwd: "{{ password }}", roles: ["root"] })
        admin.auth("admin", "{{ password }}")
        {% endif %} 
        // create more users if needed
        admin.createUser(...)

I assume you have a wrong configuration setting on the host.我假设您在主机上的配置设置错误。

Unable to connect to database: Unknown option directconnection无法连接到数据库:未知选项直接连接

This doesn't look like an Ansible error to me.在我看来,这不像是 Ansible 错误。

To help you further out, you should disable mongo authentication, and restart mongo.为了进一步帮助您,您应该禁用 mongo 身份验证,然后重新启动 mongo。 Then, create 3 users, admin , root and userAdminAnyDatabase .然后,创建 3 个用户, adminrootuserAdminAnyDatabase Then restart mongo.然后重启mongo。 Here is an Ansible role I've written for MongoDB, so you can take a look there to see how it works.这是我为 MongoDB 编写的 Ansible 角色,因此您可以在那里查看它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM