Ansible get specific value from output

Viewed 1034

I have been pulling my hair out over something I thought would have been easy to do.

I have playbook:

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

When I run that playbook, it outputs this:

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

My question is, how do I simply get the "Current Connections" value i.e. in this example it is 7 and store it in a variable.

I have tried various different solutions but nothing seems to work.

My Ansible version is 2.9

Can someone please help?

2 Answers

The task

    - debug:
        msg: "Current Connections: {{ item.split().2 }}"
      loop: "{{ connections.stdout_lines.0 }}"
      when: item is match('^  Current Connections(.*)$')

gives

    "msg": "Current Connections: 7"

Use a regular expression to extract the value and set the variable "current_connections"

- 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']
Related