Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ansible中的html解析和greping_Ansible - Fatal编程技术网

ansible中的html解析和greping

ansible中的html解析和greping,ansible,Ansible,我的产品站点中有一个html页面,我想解析文档并从html页面获取产品版本 html页面如下所示: <html> ....... ....... <body> ....... ....... <div id='version_info'> <div class="product-version"> <div class="product-title">Name of the product 1:</div&

我的产品站点中有一个html页面,我想解析文档并从html页面获取产品版本

html页面如下所示:

<html>
.......
.......
<body>
.......
.......
<div id='version_info'>
    <div class="product-version">
        <div class="product-title">Name of the product 1:</div><div class="product-value">ver_123</div>
    </div>
    <div class="product-version">
        <div class="product-title">Name of the product 2:</div><div class="product-value">ver_456</div>
    </div>
    <div class="product-version">
        <div class="product-title">Name of the product 3:</div><div class="product-value">ver_845</div>
    </div>
    <div class="product-version">
        <div class="product-title">Name of the product 4:</div><div class="product-value">ver_146</div>
    </div>  
</div>
.......
.......
</body>
.......
.......
</html>

.......
.......
.......
.......
产品名称1:ver_123
产品名称2:ver_456
产品名称3:ver_845
产品名称4:ver_146
.......
.......
.......
.......
如何对文档进行grep并形成类似这样的字符串?
productname1=ver\u 123、productname2=ver\u 456、productname3=ver\u 845等我已经处理了这个特定的HTML文件,结果我在variable
result

注意:

1。请更改playbook中html文件的路径。

2.此特定剧本适用于此HTML示例。对于进一步的需求和改进,请提供HTML


您需要这个特定HTML格式的答案吗?或者它可以是不同的?这将是很好的,如果我得到这个HTML的答案。但是如果您有类似的例子,这也会有很大的帮助。grepping xml/html,现在您有两个问题。谢谢。今天我会检查你的代码,让你知道:)@SRNathan我知道这篇帖子发布已经快7个月了,但看起来Sahil解决了你的问题;你应该接受它。如果你自己解决了,他的回答帮助了,考虑接受它,并在OP中提供你的解决方案作为一个编辑。
---
- hosts: localhost
  name: "Getting varibles from HTML"
  vars:
   result: {}
  tasks:
  - name: "Getting content of the file"
    command: cat /path/to/html/file
    register: search
  - name: "Creating dictionary while Looping over file"
    ignore_errors: true
    vars:
     key: "{{item | replace('<div class=\"product-title\">','') | replace('</div>','') | regex_replace('<div.*','') | regex_replace('^\\s*','')}}"
     value: "{{item | replace('<div class=\"product-title\">','') | replace('</div>','') | regex_replace('^[\\w\\s\\:]*','') | replace('<div class=\"product-value\">','') | regex_replace('\\s*$','')}}" 
    set_fact:
     result: "{{ result | combine( { key: value } ) }}"
    when: "'product-title' in item"
    with_items: "{{search.stdout_lines}}"

  - name: "Getting register"
    debug:
     msg: "{{result}}"  
...
ok: [localhost] => {
    "msg": {
        "Name of the product 1:": "ver_123", 
        "Name of the product 2:": "ver_456", 
        "Name of the product 3:": "ver_845", 
        "Name of the product 4:": "ver_146"
    }
}