利用ansible部署mongodb

本文介绍如何使用Ansible自动化配置和部署MongoDB数据库服务。包括创建用户、数据目录、安装MongoDB软件包、配置启动文件及认证密钥等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用ansible部署mongodb


---
- hosts: db_server
  become: yes
  become_method: sudo 

  vars:
    mengodb_datadir_prefix: /data 
    mongod_port: 27018

  tasks:
    - name: Create the mongodb user
      user: name=mongodb comment="MongoDB"

    - name: Create the data directory for the namenode metadata
      file: path={{mengodb_datadir_prefix}} owner=mongodb group=mongodb state=directory

    - name: Install the MongoDB package
      yum: name={{ item }} state=installed
      with_items:
        - mongodb-server
        - mongodb-clients
        - rsyslog-mongodb

    - name: create data directory for mongodb
      file:
        path: "{{ mengodb_datadir_prefix }}/mongo-{{ ansible_hostname }}"
        state: directory
        owner: mongodb
        group: mongodb

    - name: create log directory for mongodb
      file: path=/var/log/mongo state=directory owner=mongodb group=mongodb

    - name: create the mongodb startup file
      template: src=mongod.j2 dest=/etc/init.d/mongod-{{ ansible_hostname}} mode=0655

    - name: create the mongodb configuration file
      template: src=mongod.conf.j2 dest=/etc/mongod-{{ ansible_hostname }}.conf 

    - name: copy the keyfile for authentication
      copy: src=secret dest={{ mengodb_datadir_prefix }}/secret owner=mongodb group=mongodb mode=0400

    - name: start the mongodb service
      command: create=/var/lock/subsys/mongod-{{ ansible_hostname}}  /etc/init.d/mongodb-{{ ansible_hostname }} start
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值