Created
December 6, 2017 11:16
-
-
Save halberom/e8748635d8dd6e363b208a810eb70bc7 to your computer and use it in GitHub Desktop.
ansible - example of parsing html table into dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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>" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?