符合条件后,从标准输出中过滤多个条目

时间:2019-03-25 05:51:21

标签: ansible

我正在尝试获取每个存储池的池名称,可用空间和总空间。以下是命令模块的标准输出,其中包含每个池的详细信息。我将命令输出存储在var池中。我无法通过调试解决。谁能帮助我该怎么做?我需要从下面的输出中调试每个池的(pool_name),(free_capacity_gb)和(total_capacity_gb)。我的烦恼是平均水平。我正在RHEL 7上使用ansible 2.6。谢谢

我以前没有使用过loop_control,但尝试按照google中的示例使用。不确定其中有什么问题。

剧本代码:

 ---
 - hosts: localhost
   tasks:

   - name: Get Pools List
     shell: cinder get-pools --detail
     register: pools
   - debug:
       msg: "{{ item.total_capacity_gb }} - {{ item.free_capacity_gb }} - {{ item.pool_name }}"
     with_items: "{{ pools }}"
     loop_control:
       label: "loop control output : {{ item.total_capacity_gb }} | {{ item.free_capacity_gb }} | {{ item.pool_name }}"

ansible-playbook pools-list.yml --syntax-check不返回任何错误。

运行Playbook时出错:

致命:[localhost]:失败! => {     “ msg”:“任务包含带有未定义变量的选项。错误为:'ansible.utils.unsafe_proxy.AnsibleUnsafeText对象'没有属性'total_capacity_gb'\ n \ n错误似乎出在'/ home / user中/pools-list.yml':第8行,第5列,但可能会\ n根据确切的语法问题而在文件的其他位置。\ n \ n出现问题的行似乎是:\ n \ n注册:pools \ n-调试:\ n ^这里\ n“ }

这是上面剧本中使用的命令模块的输出,没有调试:

 ---------------------------+------------------------------------------+",
"stdout_lines": [
    "+-----------------------------------+------------------------------------------+",
    "| Property                          | Value                                    |",
    "+-----------------------------------+------------------------------------------+",
    "| QoS_support                       | True                                     |",
    "| allocated_capacity_gb             | 0                                        |",
    "| easytier_support                  | True                                     |",
    "| free_capacity_gb                  | 0.0                                      |",
    "| location_info                     | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:pool2   |",
    "| max_over_subscription_ratio       | 20.0                                     |",
    "| multiattach                       | True                                     |",
    "| name                              | abc86#pool2                              |",
    "| pool_name                         | pool2                                    |",
    "| provisioned_capacity_gb           | 0.0                                      |",
    "| reserved_percentage               | 0                                        |",
    "| total_capacity_gb                 | 0.0                                      |",
    "| volume_backend_name               | abc86                                    |",
    "+-----------------------------------+------------------------------------------+",
    "+-----------------------------------+------------------------------------------+",
    "| Property                          | Value                                    |",
    "+-----------------------------------+------------------------------------------+",
    "| QoS_support                       | True                                     |",
    "| allocated_capacity_gb             | 950                                      |",
    "| easytier_support                  | True                                     |",
    "| free_capacity_gb                  | 750.0                                    |",
    "| location_info                     | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:pool3   |",
    "| max_over_subscription_ratio       | 20.0                                     |",
    "| multiattach                       | True                                     |",
    "| name                              | abc86#pool3                              |",
    "| pool_name                         | pool3                                    |",
    "| provisioned_capacity_gb           | 650.0                                    |",
    "| reserved_percentage               | 0                                        |",
    "| total_capacity_gb                 | 1000.0                                   |",
    "| volume_backend_name               | abc86                                    |",
    "+-----------------------------------+------------------------------------------+",

我需要从stdout调试每个池的(pool_name),(free_capacity_gb)和(total_capacity_gb)。

1 个答案:

答案 0 :(得分:0)

正如我正确观察到的那样,ansible不会自动解析命令的文本输出。

您将希望regex_replace提取您关心的键值对,然后可以利用文本忍者将它们转换为 a dict的优势yaml是如此自由的语法的事实

- debug:
    msg: >-
     {{ 
      pools.stdout_lines
      | select("regex", '[|] Property | '+the_pattern)
      | map("regex_replace", '^[|] Property.*', '- ')
      | map("regex_replace", the_pattern, '  "\1": "\2"')
      | join(newline_char)
      | from_yaml }}
  vars:
    newline_char: "\n"
    the_pattern: '[|] (free_capacity_gb|pool_name|total_capacity_gb) [|] ([^ ]+)'

您将需要在其中放置select来丢弃不匹配的行,否则regex_replace不会替换任何内容,最终会导致YAML格式错误