【问题标题】:how to set mongodb replica set using ansible如何使用 ansible 设置 mongodb 副本集
【发布时间】:2021-01-14 05:08:07
【问题描述】:

需要在 3 个实例中设置 mongo dB 副本集,一个可以是主要的,其余的两个将是辅助的。 任何人都可以建议我如何编写剧本。

已在三台服务器上启动了 mongo shell 并启动了复制名称

'''复制: replSetName: "testingrs"'''

【问题讨论】:

  • 您只需发起一次。

标签: mongodb ansible mongodb-query replication replicaset


【解决方案1】:

Ansible 已经为其提供了插件:community.mongodb.mongodb_replicaset

当我部署我的 MongoDB 分片集群时,插件仍然是 1.0 版本并且有很多限制。我们在安装pymongo 时也遇到了一些问题,所以我手动开发了这些任务。但是,我认为现在的版本没有必要再自己编写任务了。

不管怎样,我的剧本是这样的:

- name: Check if Replicaset is already initialized
  shell: 
    cmd: "/usr/bin/mongo --norc --quiet localhost:{{ ports.config }}"
    executable: /bin/bash
    stdin: "rs.status().codeName" 
  register: result
  changed_when: false
  check_mode: no

- set_fact:
    rs_initiate: |      
      {% set members = [] %}
      {% for host in groups['config'] | sort %}
      {% set m = {'_id': loop.index0 } %}
      {% set _ = m.update({'host': host + '.' + domain + ':' + ports.config | string }) %}
      {% set _ = members.append(m) %}
      {% endfor %}
      {% set init = {'_id': replica_set.conf} %}
      {% set _ = init.update({'members': members}) %}
      {{ init }} 
    rs: |
      {% set i = (result.stdout == 'NotYetInitialized') %}
      {% for host in ansible_play_hosts %}
      {% set i = i and (hostvars[host].result.stdout == 'NotYetInitialized') %}
      {% endfor %}
      {{ {'NotYetInitialized': i} }}

- name: Init Replicaset
  shell: 
    cmd: "/usr/bin/mongo --norc --quiet localhost:{{ ports.config }}"
    executable: /bin/bash
    stdin: |
      rs.initiate({{ rs_initiate | to_json }})
      rs.status()
      while (! db.isMaster().ismaster ) sleep(1000) 
  when: rs.NotYetInitialized and inventory_hostname_short == (groups['config'] | sort | first) 

我遇到的一个问题是处理身份验证,因为当您从头开始部署 MongoDB 时,没有用户存在。因此,当您想多次运行 playbook 时,您必须区分有无身份验证。

我的剧本包含以下任务:

  - name: Check if authentication is enabled
    shell: 
      cmd: "/usr/bin/mongo --norc --quiet localhost:{{ ports.router }}"
      executable: /bin/bash
      stdin: "rs.status().codeName" 
    register: result
    ignore_errors: yes
    changed_when: false
    when: inventory_hostname_short == (groups['application'] | sort | first)

  - name: Authenticate if needed
    set_fact:
      authenticate: "{{ (result.stdout == 'Unauthorized') | ternary('-u admin -p ' + password[env].admin + ' --authenticationDatabase admin','') }}"
    when: inventory_hostname_short == (groups['application'] | sort | first)

  - name: Create users
    shell: 
      cmd: "/usr/bin/mongo {{ authenticate }} --norc --quiet localhost:{{ ports.router }}"
      executable: /bin/bash
      stdin: |
        admin = db.getSiblingDB("admin")
        admin.createUser({ user: "admin", pwd: "{{ password[env].admin }}", roles: ["root"] })
        admin.auth("admin", "{{ password[env].admin }}")
        // create more users if needed
        admin.createUser(...)
    when: inventory_hostname_short == (groups['application'] | sort | first)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2013-10-22
    • 2020-09-25
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    相关资源
    最近更新 更多