首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >匹配条件后,从stdout中筛选多个条目

匹配条件后,从stdout中筛选多个条目
EN

Stack Overflow用户
提问于 2019-03-25 05:51:21
回答 1查看 1.7K关注 0票数 0

我试图得到池的名称,免费和总空间的每个存储池。下面是一个包含每个池细节的命令模块的标准输出。我将命令输出存储在var池中。我无法用调试解决这个问题。有人能帮忙吗?我该怎么做?我需要从下面的输出对每个池进行调试(pool_name)、(free_capacity_gb)和(total_capacity_gb)。我的常识一般。我在RHEL 7上使用ansible 2.6。谢谢

我以前没有使用过loop_control,但试着使用谷歌的例子。不知道是怎么回事。

剧本代码:

代码语言:javascript
复制
 ---
 - 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-剧本池-list.yml-语法-检查不返回任何错误。

运行剧本时出错:

致命: localhost: FAILED!=> { "msg":“任务包含一个带有未定义变量的选项。错误是:'ansible.utils.unsafe_proxy.AnsibleUnsafeText对象‘没有属性’total_capacity_gb‘\n\n该错误似乎在’/home/user/pools list.yml‘中:第8行,第5列,但可能会在文件的其他地方,这取决于具体的语法问题。\n\n这个错误行看起来是:\n\n寄存器:池\n-调试:\n^这里\n“}

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

代码语言:javascript
复制
 ---------------------------+------------------------------------------+",
"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)每个池。

EN

回答 1

Stack Overflow用户

发布于 2019-03-28 04:50:18

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

您将希望regex_replace提取您所关心的键值对,然后可以执行一些文本忍者操作,将它们转换为dict,同时利用yaml是如此自由的语法这一事实。

代码语言:javascript
复制
- 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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55331902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档