我的剧本中有以下任务,应该在MongoDB副本集中获得主要任务:
- name: get primary
shell: mongo --host {{ mongodb_replicaset_name }}/{{ mongodb_hosts }} --quiet --eval "rs.isMaster().primary"
register: primary_result
changed_when: false
run_once: true
我期望的结果在外壳上应该像这样:
rs0:PRIMARY> rs.isMaster()。primary
mongotest1:27017
我试图使用以下任务获取结果,但是由于输出包含所有网络消息并且不能保证顺序,因此它不起作用。
- name: set primary host:port string
set_fact:
primary: "{{ primary_result.stdout_lines[-1] }}"
run_once: true
我得到的结果是“ mongotest1:27017”位于所有其他信息输出的中间,如下所示:
ok: [mongotest4] => {
"primary_result": {
"changed": false,
"cmd": "mongo --host rs0/mongotest1,mongotest2,mongotest3,mongotest4,mongotest5 --quiet --eval \"rs.isMaster().primary\"",
"delta": "0:00:00.073013",
"end": "2019-05-17 19:09:57.954030",
"failed": false,
"rc": 0,
"start": "2019-05-17 19:09:57.881017",
"stderr": "",
"stderr_lines": [],
"stdout": "2019-05-17T19:09:57.940+0000 I NETWORK [js] Starting new replica set monitor for rs0/mongotest1:27017,mongotest2:27017,mongotest3:27017,mongotest4:27017,mongotest5:27017\n2019-05-17T19:09:57.943+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mongotest2:27017 (1 connections now open to mongotest2:27017 with a 5 second timeout)\n2019-05-17T19:09:57.944+0000 I NETWORK [js] Successfully connected to mongotest1:27017 (1 connections now open to mongotest1:27017 with a 5 second timeout)\n2019-05-17T19:09:57.945+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mongotest5:27017 (1 connections now open to mongotest5:27017 with a 5 second timeout)\nmongotest1:27017\n2019-05-17T19:09:57.949+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mongotest3:27017 (1 connections now open to mongotest3:27017 with a 5 second timeout)\n2019-05-17T19:09:57.951+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mongotest4:27017 (1 connections now open to mongotest4:27017 with a 5 second timeout)",
"stdout_lines": [
"2019-05-17T19:09:57.940+0000 I NETWORK [js] Starting new replica set monitor for rs0/mongotest1:27017,mongotest2:27017,mongotest3:27017,mongotest4:27017,mongotest5:27017",
"2019-05-17T19:09:57.943+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mongotest2:27017 (1 connections now open to mongotest2:27017 with a 5 second timeout)",
"2019-05-17T19:09:57.944+0000 I NETWORK [js] Successfully connected to mongotest1:27017 (1 connections now open to mongotest1:27017 with a 5 second timeout)",
"2019-05-17T19:09:57.945+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mongotest5:27017 (1 connections now open to mongotest5:27017 with a 5 second timeout)",
"mongotest1:27017",
"2019-05-17T19:09:57.949+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mongotest3:27017 (1 connections now open to mongotest3:27017 with a 5 second timeout)",
"2019-05-17T19:09:57.951+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mongotest4:27017 (1 connections now open to mongotest4:27017 with a 5 second timeout)"
]
}
}
我想获取stdout_lines的第一行,该行与时间戳前缀模式“ ^ [0-9] {4}-[0-9] {2}-[0-9] {2} T [ 0-9] {2}:[0-9] {2}:[0-9] {2}。[0-9] {3} [+-] [0-9] {4} \ s +“和将结果存储在变量中。第一个也是唯一的结果应该是“ mongotest1:27017”行。我该怎么办?
此外,还有其他方法可以在不使用mongo shell的情况下获取MongoDB主数据库,这样我就不会在shell输出中遇到这些问题吗?
答案 0 :(得分:0)
我想我找到了解决方案。
我创建了一个新的过滤器任务,用于检查时间戳前缀的倒数:
- name: filter unwanted informational messages from primary result
set_fact:
filtered_lines: "{{ primary_result.stdout_lines | select('match', '^(?!([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}[+-][0-9]{4}\\s+))') | list }}"
run_once: true
然后我使用filtered_lines结果中的第一个列表项设置一个事实
- name: set primary host:port string
set_fact:
primary: "{{ filtered_lines[0] }}"
run_once: true
这似乎正在测试中