2016-03-04 125 views
1

我使用Stouts.mongodb作爲安裝MongoDB的完美角色。從ansible初始化一個mongo副本集

我在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()" 

我已經試過這個腳本,而不將最後一個命令任務,一切工作正常。這只是我需要ssh進入箱子,並自己做rs.initiate()。我想從理論上做到這一點,但我得到以下錯誤:

==> 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 

我正在做這個正確的方式嗎? 有沒有其他方法可以做到這一點?

UPDATE

我試圖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()" 

但同樣的是同樣的錯誤:

==> 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 

我也在我的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 
+0

這可能是蒙戈還不啓動的情況下的流程。嘗試在init副本之前使用[wait_for](http://docs.ansible.com/ansible/wait_for_module.html)。 –

+0

@AlexBlex我嘗試過,但我得到了同樣的錯誤。我已經用一些細節更新了這個問題。 –

+0

乳清你在等待狀態=排水?你不想要國家=現在嗎? – Petro026

回答

0

如果你知道重啓的MongoDB的,你可以做這樣的事情

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