ansible - 在ansible中解析和grep html

标签 ansible

我的产品网站中有 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>

如何 grep 文档并形成类似这样的字符串? 产品名称1=ver_123,产品名称2=ver_456,产品名称3=ver_845等

最佳答案

我已经处理了这个特定的 HTML 文件,结果我在变量 result 下得到了所需变量的字典

注意:

1. Please change path of html file in playbook.

2. This particular playbook work for this HTML example. For further requirements and improvements provide HTML.

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

关于ansible - 在ansible中解析和grep html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42239997/

相关文章:

python - 如何将 .py 文件导入 Ansible 模块?

git - 当 git clone 在远程服务器上工作时,无主模式下的 ansible git 任务失败

python-3.x - ansible 剧本错误为 : ModuleNotFoundError: No module named 'azure.mgmt.monitor.version' although the module is installed

ansible - 防止 ansible 在通过 with_items 传递时解析字符串

azure - 如何使用ansible循环挂载多个磁盘

ansible - 如何使用 Ansible 嵌套变量?

ansible - 我怎样才能减慢ansible的速度?

linux - Collectd 无法读取 Ansible 发送的配置文件

Ansible : host in multiple groups

dictionary - 如何将字典字典转换为 Ansible vars 文件中的字典列表?