我使用Stouts.mongodb作为安装MongoDB的安全角色。
我在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()
。我想从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
我这样做是对的吗? 有没有其他方法可以做到这一点?
更新
我尝试了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 :(得分:0)
如果你知道mongodb的重启顺序,你可以做这样的事情
- name: wait for service, shutdown, and service
wait_for: state={{item}} port=7777
with_items:
- present
- drained
- present