Ansible extract value using regex

Viewed 443

I have a file similar to below.

SITE "A" (id:1,bindings:http/*:80:,net.tcp/808:*,net.pipe/*,state:Stopped)
SITE "B" (id:2,bindings:https/*:344:,net.tcp/325:*,net.pipe/*,state:Started)
SITE "C" (id:3,bindings:http/:80:xyz.domain.com,http/*:80:xyz,http/*:80:seiging,http/*:80:seiging.domain.com,state:Started)
SITE "D" (id:4,bindings:http/:901:,http/*:80:actdaov,state:Started)
SITE "E" (id:5,bindings:https/:443:idu.domain.com,http/*:80:idu.domain.com,state:Started)

I want to extract the below content.

"A,id:1,",
"B,id:2,",
"C,id:3,http/:80:seiging.domain.com",
"D,id:4,",
"E,id:5,http/
:80:idu.domain.com",

The third field - If domain.com is present I need to extract that, if not it should be blank. If more than one domain.com exists then I need to take the last one.

I tried as below:

set_fact: inv_op="{{  item | regex_replace('SITE \\\"(.*)\\\" \((.*)\,bindings(.*)\,state.*', '\\1,\\2,\\3')  }}"

Output as below:

"A,id:1,:http/:80:,net.tcp/808:,net.pipe/",
"B,id:2,:https/
:344:,net.tcp/325:,net.pipe/", "C,id:3,:http/:80:xyz.domain.com,http/:80:xyz,http/:80:seiging,http/:80:seiging.domain.com",
"D,id:4,:http/:901:,http/
:80:actdaov",
"E,id:5,:https/:443:idu.domain.com,http/*:80:idu.domain.com",

Please advice

1 Answers

It's possible to create a dictionary first. For example the tasks

- set_fact:
    my_lines: "{{ lookup('file', 'data.csv').splitlines() }}"
- set_fact:
    my_lists: "{{ my_lists|default([]) + [item.split(' ')] }}"
  loop: "{{ my_lines }}"
- set_fact:
    my_dict: "{{ my_dict|default({})|
                 combine({item.1.1: {'id': item.2.split(',')[0].split(':')[1],
                                     'state': item.2.split(',')[-1].split(':')[1][:-1],
                                     'bindings': item.2.split(',')[1:-1]|
                                      map('regex_replace', 'bindings:', '')|list}})
                                      }}"
  loop: "{{ my_lists }}"

give

"my_dict": {
    "A": {
        "bindings": [
            "http/*:80:", 
            "net.tcp/808:*", 
            "net.pipe/*"
        ], 
        "id": "1", 
        "state": "Stopped"
    }, 
    "B": {
        "bindings": [
            "https/*:344:", 
            "net.tcp/325:*", 
            "net.pipe/*"
        ], 
        "id": "2", 
        "state": "Started"
    }, 
    "C": {
        "bindings": [
            "http/:80:xyz.domain.com", 
            "http/*:80:xyz", 
            "http/*:80:seiging", 
            "http/*:80:seiging.domain.com"
        ], 
        "id": "3", 
        "state": "Started"
    }, 
    "D": {
        "bindings": [
            "http/:901:", 
            "http/*:80:actdaov"
        ], 
        "id": "4", 
        "state": "Started"
    }, 
    "E": {
        "bindings": [
            "https/:443:idu.domain.com", 
            "http/*:80:idu.domain.com"
        ], 
        "id": "5", 
        "state": "Started"
    }
}

Then print the data. For example the task

"The third field - If domain.com is present I need to extract that, if not it should be blank. If more than one domain.com exists then I need to take the last one."

- debug:
    msg: |
      {{ item.key }},id:{{ item.value.id }},
      {%- if item.value.bindings|select('search', 'domain\.com')|list %}
      {{ item.value.bindings|select('search', 'domain\.com')|reverse|list|first }},
      {% endif %}
  loop: "{{ my_dict|dict2items }}"

gives

"msg": "A,id:1,\n"
"msg": "C,id:3,http/*:80:seiging.domain.com,\n"
"msg": "B,id:2,\n"
"msg": "E,id:5,http/*:80:idu.domain.com,\n"
"msg": "D,id:4,\n"
Related