简体   繁体   English

从Ansible初始化mongo副本集

[英]Initialising a mongo replica set from ansible

I am using Stouts.mongodb as the ansible role for installing MongoDB. 我使用Stouts.mongodb作为安装MongoDB的角色。

I have the following in the ansible playbook: 我在ansible剧本中有以下内容:

---

- hosts: all
  sudo: true
  roles:
    - Stouts.mongodb
  vars:
    - mongodb_conf_replSet: rs0
    - mongodb_conf_bind_ip: 192.168.111.11
  tasks:
    - name: Initialising replica set in mongo
      command: mongo 192.168.111.11:27017 --eval "rs.initiate()"

I have tried this script without the last command task and everything works fine. 我尝试了没有上一个命令任务的脚本,并且一切正常。 It's just always I need to ssh into the box and do rs.initiate() myself. 我总是总是需要把它装进盒子里,然后自己做rs.initiate() I thought of doing it from ansible but I get the following error: 我想从ansible开始这样做,但出现以下错误:

==> mongo-test: TASK [Initialising replica set in mongo] ***************************************
==> mongo-test: task path: /vagrant/provisioning/mongo_playbook.yml:11
==> mongo-test: fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ["mongo", "192.168.111.11:27017", "--eval", "rs.initiate()"], "delta": "0:00:00.069123", "end": "2016-03-04 20:58:27.513704", "failed": true, "rc": 1, "start": "2016-03-04 20:58:27.444581", "stderr": "exception: connect failed", "stdout": "MongoDB shell version: 2.6.11\nconnecting to: 192.168.111.11:27017/test\n2016-03-04T20:58:27.508+0000 warning: Failed to connect to 192.168.111.11:27017, reason: errno:111 Connection refused\n2016-03-04T20:58:27.508+0000 Error: couldn't connect to server 192.168.111.11:27017 (192.168.111.11), connection attempt failed at src/mongo/shell/mongo.js:148", "stdout_lines": ["MongoDB shell version: 2.6.11", "connecting to: 192.168.111.11:27017/test", "2016-03-04T20:58:27.508+0000 warning: Failed to connect to 192.168.111.11:27017, reason: errno:111 Connection refused", "2016-03-04T20:58:27.508+0000 Error: couldn't connect to server 192.168.111.11:27017 (192.168.111.11), connection attempt failed at src/mongo/shell/mongo.js:148"], "warnings": []}
==> mongo-test: 
==> mongo-test: NO MORE HOSTS LEFT *************************************************************
==> mongo-test: 
==> mongo-test: RUNNING HANDLER [Stouts.mongodb : mongodb restart] *****************************
==> mongo-test:     to retry, use: --limit @mongo_playbook.retry
==> mongo-test: 
==> mongo-test: PLAY RECAP *********************************************************************
==> mongo-test: 127.0.0.1                  : ok=15   changed=8    unreachable=0    failed=1   

Am I doing this right way? 我这样做对吗? Is there an alternative way to do this? 有替代方法吗?

UPDATE UPDATE

I tried wait_for : 我尝试了wait_for

---

- hosts: all
  sudo: true
  roles:
    - Stouts.mongodb
  vars:
    - mongodb_conf_replSet: rs0
    - mongodb_conf_bind_ip: 192.168.111.11
  tasks:
    - name: Waiting for port to be available
      wait_for: host=192.168.111.11 port=27017 delay=10 state=drained timeout=160
    - name: Initialising replica set in mongo
      command: mongo 192.168.111.11:27017 --eval "rs.initiate()"

But again that same error: 但是同样的错误:

==> mongo-test: TASK [Waiting for port to be available] ****************************************
==> mongo-test: task path: /vagrant/provisioning/mongo_playbook.yml:11
==> mongo-test: ok: [127.0.0.1] => {"changed": false, "elapsed": 10, "path": null, "port": 27017, "search_regex": null, "state": "drained"}
==> mongo-test: 
==> mongo-test: TASK [Initialising replica set in mongo] ***************************************
==> mongo-test: task path: /vagrant/provisioning/mongo_playbook.yml:13
==> mongo-test: fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ["mongo", "192.168.111.11:27017", "--eval", "rs.initiate()"], "delta": "0:00:00.092468", "end": "2016-03-04 21:38:02.597946", "failed": true, "rc": 1, "start": "2016-03-04 21:38:02.505478", "stderr": "exception: connect failed", "stdout": "MongoDB shell version: 2.6.11\nconnecting to: 192.168.111.11:27017/test\n2016-03-04T21:38:02.592+0000 warning: Failed to connect to 192.168.111.11:27017, reason: errno:111 Connection refused\n2016-03-04T21:38:02.593+0000 Error: couldn't connect to server 192.168.111.11:27017 (192.168.111.11), connection attempt failed at src/mongo/shell/mongo.js:148", "stdout_lines": ["MongoDB shell version: 2.6.11", "connecting to: 192.168.111.11:27017/test", "2016-03-04T21:38:02.592+0000 warning: Failed to connect to 192.168.111.11:27017, reason: errno:111 Connection refused", "2016-03-04T21:38:02.593+0000 Error: couldn't connect to server 192.168.111.11:27017 (192.168.111.11), connection attempt failed at src/mongo/shell/mongo.js:148"], "warnings": []}
==> mongo-test: 
==> mongo-test: NO MORE HOSTS LEFT *************************************************************
==> mongo-test: 
==> mongo-test: RUNNING HANDLER [Stouts.mongodb : mongodb restart] *****************************
==> mongo-test:     to retry, use: --limit @mongo_playbook.retry
==> mongo-test: 
==> mongo-test: PLAY RECAP *********************************************************************
==> mongo-test: 127.0.0.1                  : ok=16   changed=8    unreachable=0    failed=1   

I am also adding my mongod.conf : 我还添加了mongod.conf

auth = False
bind_ip = 192.168.111.11
cpu = True
dbpath = /data/db
fork = False
httpinterface = False
ipv6 = False
journal = False
logappend = True
logpath = /var/log/mongodb/mongod.log
maxConns = 1000000
noprealloc = False
noscripting = False
notablescan = False
port = 27017
quota = False
quotaFiles = 8
syslog = False
smallfiles = False
# Replica set options:
replSet = rs0
replIndexPrefetch = all

If you know the sequence of reboots for mongodb you can do something like this 如果您知道重新启动mongodb的顺序,则可以执行以下操作

- name: wait for service, shutdown, and service
  wait_for: state={{item}} port=7777
  with_items:
  - present
  - drained
  - present

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

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