Skip to content

Instantly share code, notes, and snippets.

@halberom
Created December 6, 2017 11:16
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halberom/e8748635d8dd6e363b208a810eb70bc7 to your computer and use it in GitHub Desktop.
Save halberom/e8748635d8dd6e363b208a810eb70bc7 to your computer and use it in GitHub Desktop.
ansible - example of parsing html table into dict
<html><title>Find Location of an IP</title>
<body><font face=\"Arial\">
<H1>Please provide an IP or a LOCATION :</H1>
<p>Search Location, DS site code, Country, Network types or Comments</p>
<form method=\"GET\" action=\"launch_search_IP.pl\">
<p> <input name=\"ip_to_search\" width=\"100%\" height=\"100%\" ></p>
<input type=\"submit\" value=\"Search\"></form>
<p> Here are results for your search of <b> 192.168.0.1 </b> :
<br>
<br>
<table border=2 cellpadding=10 cellspacing=0>
<tr ALIGN=left>
<th>Network</th>
<th>Network Name</th>
<th>LOCATION</th>
<th>COUNTRY</th>
<th>Comments</th></b> </tr>
<tr>
<td>192.168.0.0/24</td>
<td> WIFI</td>
<td>0001 Berlin</td>
<td>GERMANY</td>
<td>WIFI</td>
</tr></table></body></html>" parse this!
<html><title>Find Location of an IP</title>
<body><font face=\"Arial\">
<H1>Please provide an IP or a LOCATION :</H1>
<p>Search Location, DS site code, Country, Network types or Comments</p>
<form method=\"GET\" action=\"launch_search_IP.pl\">
<p> <input name=\"ip_to_search\" width=\"100%\" height=\"100%\" ></p>
<input type=\"submit\" value=\"Search\"></form>
<p> Here are results for your search of <b> 192.168.0.1 </b> :
<br>
<br>
<table border=2 cellpadding=10 cellspacing=0>
<tr ALIGN=left>
<th>Network</th>
<th>Network Name</th>
<th>LOCATION</th>
<th>COUNTRY</th>
<th>Comments</th></b> </tr>
<tr>
<td>192.168.0.0/24</td>
<td> WIFI</td>
<td>0001 Berlin</td>
<td>GERMANY</td>
<td>WIFI</td>
</tr></table></body></html>"
PLAY [localhost] ***************************************************************************************************************************************************************************************************************************************************************
TASK [read the html file] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
skipping: [localhost]
TASK [get relevant list items] *************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
skipping: [localhost]
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
skipping: [localhost]
TASK [create the dict] *********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"title_values": {
"COUNTRY": "GERMANY",
"Comments</b> </tr>": "WIFI",
"LOCATION": "0001 Berlin",
"Network": "192.168.0.0/24",
"Network Name": " WIFI"
}
}
PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
---
- hosts: localhost
gather_facts: False
connection: local
tasks:
- name: read the html file
set_fact:
file_lines: "{{ lookup('file', 'htmlfile.html').split('\n') }}"
- debug:
var: file_lines
verbosity: 1
- name: get relevant list items
set_fact:
titles: "{{ file_lines|select('search', '<th>')|map('regex_replace', '<th>(.*)</th>', '\\1')|list }}"
values: "{{ file_lines|select('search', '<td>')|map('regex_replace', '<td>(.*)</td>', '\\1')|list }}"
- debug:
var: titles
verbosity: 1
- debug:
var: values
verbosity: 1
- name: create the dict
set_fact:
title_values: "{{ dict(titles|zip(values)) }}"
- debug:
var: title_values
@chusiang
Copy link

chusiang commented Jul 29, 2020

Thanks a lot, I use this example to parse some package files, and sync they to AWS S3 with GitLab CI now.

@ox-gibson
Copy link

I am using this example to parse a html table. However, it is only outputting the first row in the title_values dictionary.
How do you get the full table to print in the debug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment