【问题标题】:How to Obtain MongoDB Primary by Filtering stdout_lines in Ansible?如何通过过滤 Ansible 中的 stdout_lines 来获取 MongoDB Primary?
【发布时间】:2019-05-20 16:17:41
【问题描述】:

我的剧本中有以下任务应该在 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

我期望的结果在 shell 中应该是这样的:

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)"
        ]
    }
}

我想获取与时间戳前缀模式“^[0-9]{4}-[0-9]{2}-[0-9]{2}T[ 不匹配的第一行 stdout_lines 0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}[+-][0-9]{4}\s+" 和将结果存储在变量中。第一个也是唯一的结果应该是“mongotest1:27017”行。我该怎么做?

此外,是否有其他方法可以在不使用 mongo shell 的情况下获取 MongoDB 主数据库,这样我就不会在 shell 输出时遇到这些问题?

【问题讨论】:

标签: regex mongodb ansible


【解决方案1】:

我想我找到了解决办法。

我创建了一个新的过滤任务来检查时间戳前缀的倒数:

- 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

然后我使用 filters_lines 结果中的第一个列表项设置一个事实

- name: set primary host:port string
  set_fact:
    primary: "{{ filtered_lines[0] }}"
  run_once: true

这似乎在测试中起作用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多