如何使用ansible设置mongodb副本集
需要在3个实例中设置mongo dB副本集,其中一个是主实例,其余两个是辅助实例。 任何人都可以建议我如何写剧本 已在三台服务器中启动mongo shell并启动复制名称 ''复制:如何使用ansible设置mongodb副本集,mongodb,ansible,mongodb-query,replication,replicaset,Mongodb,Ansible,Mongodb Query,Replication,Replicaset,需要在3个实例中设置mongo dB副本集,其中一个是主实例,其余两个是辅助实例。 任何人都可以建议我如何写剧本 已在三台服务器中启动mongo shell并启动复制名称 ''复制: replSetName:“testingrs”''Ansible已经为其提供了插件: 当我部署MongoDB分片集群时,该插件仍然是1.0版,有很多限制。我们在安装pymongo时也遇到了一些问题,所以我手动开发了这些任务。然而,我认为在当前版本中,不再需要自己编写任务 总之,我的剧本是这样的: - name: C
replSetName:“testingrs”''Ansible已经为其提供了插件: 当我部署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)
你只需要发起一次。