首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可能从输出中获取特定值

可能从输出中获取特定值
EN

Stack Overflow用户
提问于 2020-04-08 14:25:49
回答 2查看 489关注 0票数 2

我一直在为一件我认为很容易做的事情而抓狂。

我有一本攻略:

代码语言:javascript
复制
- hosts: all
  tasks:
    - name: Get F5 connections
      bigip_command:
        commands: show ltm node /mypartition/myserver.domain.com
        provider:
          server: "F5server.domain.com"
          user: ""
          password: ""
          validate_certs: "no"
          server_port: 443
      register: connections
      delegate_to: localhost
    - name: output
      debug:
        msg: "{{ connections }}"

当我运行该剧本时,它输出以下内容:

代码语言:javascript
复制
ok: [myserver.domain.com] => {
    "msg": {
        "changed": false,
        "executed_commands": [
            "tmsh -c \\\"show ltm node /mypartition/myserver.domain.com\\\""
        ],
        "failed": false,
        "stdout": [
            "-------------------------------------------------------------\nLtm::Node: /mypartition/myserver.domain.com (123.45.67.89)\n-------------------------------------------------------------\nStatus               \n  Availability   : available\n  State          : enabled\n  Reason         : Node address is available\n  Monitor        : /Common/gateway_icmp (default node monitor)\n  Monitor Status : up\n  Session Status : enabled\n                     \nTraffic                ServerSide  General\n  Bits In                  635.9M        -\n  Bits Out                   2.8G        -\n  Packets In               102.8K        -\n  Packets Out              266.6K        -\n  Current Connections           7        -\n  Maximum Connections          11        -\n  Total Connections          6.5K        -\n  Total Requests                -    13.0K\n  Current Sessions              -        0"
        ],
        "stdout_lines": [
            [
                "-------------------------------------------------------------",
                "Ltm::Node: /mypartition/myserver.domain.com (123.45.67.89)",
                "-------------------------------------------------------------",
                "Status               ",
                "  Availability   : available",
                "  State          : enabled",
                "  Reason         : Node address is available",
                "  Monitor        : /Common/gateway_icmp (default node monitor)",
                "  Monitor Status : up",
                "  Session Status : enabled",
                "                     ",
                "Traffic                ServerSide  General",
                "  Bits In                  635.9M        -",
                "  Bits Out                   2.8G        -",
                "  Packets In               102.8K        -",
                "  Packets Out              266.6K        -",
                "  Current Connections           7        -",
                "  Maximum Connections          11        -",
                "  Total Connections          6.5K        -",
                "  Total Requests                -    13.0K",
                "  Current Sessions              -        0"
            ]
        ]
    }
}

我的问题是,如何简单地获取“当前连接”值,即在本例中为7并将其存储在变量中。

我尝试了各种不同的解决方案,但似乎都不起作用。

我的Ansible版本是2.9

有人能帮帮忙吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-08 17:37:33

任务

代码语言:javascript
复制
    - debug:
        msg: "Current Connections: {{ item.split().2 }}"
      loop: "{{ connections.stdout_lines.0 }}"
      when: item is match('^  Current Connections(.*)$')

给出

代码语言:javascript
复制
    "msg": "Current Connections: 7"
票数 0
EN

Stack Overflow用户

发布于 2020-04-09 02:45:22

使用正则表达式提取值并设置变量"current_connections“

代码语言:javascript
复制
- name: Current Connections
  vars:
    pattern: '(?<=Current\sConnections)\s*\d+'
  set_fact: 
    current_connections: "{{ connections.stdout | regex_search(pattern) }}"

- debug:
  var: hostvars[inventory_hostname]['current_connections']
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61094501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档