我试图得到池的名称,免费和总空间的每个存储池。下面是一个包含每个池细节的命令模块的标准输出。我将命令输出存储在var池中。我无法用调试解决这个问题。有人能帮忙吗?我该怎么做?我需要从下面的输出对每个池进行调试(pool_name)、(free_capacity_gb)和(total_capacity_gb)。我的常识一般。我在RHEL 7上使用ansible 2.6。谢谢
我以前没有使用过loop_control,但试着使用谷歌的例子。不知道是怎么回事。
剧本代码:
---
- 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“}
这是在上面的剧本中使用的命令模块的输出,无需调试:
---------------------------+------------------------------------------+",
"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)每个池。
发布于 2019-03-28 04:50:18
正如我正确地观察到的,ansible不自动解析命令的文本输出。
您将希望regex_replace提取您所关心的键值对,然后可以执行一些文本忍者操作,将它们转换为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。
https://stackoverflow.com/questions/55331902
复制相似问题