如何检查池中可用的浮动IP的数量? [英] How to check number of floating IPs available in a pool?

查看:129
本文介绍了如何检查池中可用的浮动IP的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写脚本以在Openstack上创建VM. 如果浮动IP在池中用尽,我可能会收到错误消息.如何检查该池中是否有可用的浮动IP? Openstack是否可以从所有可用池中自动选择池?

I am writing a script to create a VM on Openstack. I may get error if floating IPs get exhausted in pool. How can I check if there are floating IPs available in that pool or not? Is there a way where openstack can automatically choose the pool from all available pools?

推荐答案

您可以选择使用API​​(例如使用curl)还是使用openstack CLI(提交问题时使用的方法) . CLI更易于直接编写脚本.这是查询浮动IP的方法.警告:使用"-f json"输出然后使用"jq"命令进行字段查询已变得非常普遍.您也可以使用-f csv或-f值,并使用grep或sed进行解析.但是,尽管您可能以前没有做过,但我建议您尝试使用json和jq.准确地解决您的问题值得您花费时间.

You have a choice of working from the API (using curl, for example) or using the openstack CLI, which is what you were using when you submitted this question. The CLI is easier for straight scripting. Here is how you query for floating IPs. Caveat: It is becoming very commonplace to use '-f json' output and then the 'jq' command for field querying. You can also use '-f csv' or '-f value' and parse using grep or sed. But, although you may not have done it before, I recommend trying json and jq. It is worth your time for exactly the problem you are solving.

(请注意,无"列是仅显示"文本.实际存储的字段值为空".)

(Be aware the "None" column is DISPLAY ONLY text. The actual stored field value is 'null'.)

给出:

[user@system ~]$ openstack floating ip list
+--------------------------------------+---------------------+------------------+--------------------------------------+
| ID                                   | Floating IP Address | Fixed IP Address | Port                                 |
+--------------------------------------+---------------------+------------------+--------------------------------------+
| 2492aa71-cadf-4011-9c4f-87f856cd2551 | 172.25.250.29       | 192.168.3.10     | 1e0b868b-8b3c-4e8d-8f11-d6ed15d0e750 |
| 74c9233e-1420-4681-aaa7-357843f48962 | 172.25.250.36       | None             | None                                 |
| f235dfae-a01c-4290-864d-89b83f9a8de9 | 172.25.250.37       | None             | None                                 |
+--------------------------------------+---------------------+------------------+--------------------------------------+

在json中看起来像这样:

Which looks like this in json:

[stack@director ~]$ openstack floating ip list -f json
[
  {
    "Fixed IP Address": "192.168.3.10", 
    "ID": "2492aa71-cadf-4011-9c4f-87f856cd2551", 
    "Floating IP Address": "172.25.250.29", 
    "Port": "1e0b868b-8b3c-4e8d-8f11-d6ed15d0e750"
  }, 
  {
    "Fixed IP Address": null, 
    "ID": "74c9233e-1420-4681-aaa7-357843f48962", 
    "Floating IP Address": "172.25.250.36", 
    "Port": null
  }, 
  {
    "Fixed IP Address": null, 
    "ID": "f235dfae-a01c-4290-864d-89b83f9a8de9", 
    "Floating IP Address": "172.25.250.37", 
    "Port": null
  }
]

使用'jq'解析此输出,请允许我首先用英语释义. jq中的管道就像bash外壳中的管道. 因此,获取整个数组" | 选择此字段等于此值" | 返回另一个字段".如果需要,可以返回多个字段.

Using 'jq' to parse this output, allow me to paraphrase in English first. Piping in jq is like piping in the bash shell. So "take the whole array" | "select this-field equals this-value" | "return this other field". Could return multiple fields if wanted.

[user@system ~]$ openstack floating ip list -f json | jq  '.[] | select(.["Fixed IP Address"] == null ) | .["Floating IP Address"] '
"172.25.250.36"
"172.25.250.37"

如果您不希望结果用引号引起来,请请求原始输出(简称-r).

If you don't want the results in quotes, ask for raw output (-r for short).

[user@system ~]$ openstack floating ip list -f json | jq  --raw-output '.[] | select(.["Fixed IP Address"] == null ) | .["Floating IP Address"]'
172.25.250.36
172.25.250.37

这些是您可用的浮动IP.如果将它们放入数组中,则可以查询该数组以查看有多少.

These are your available floating IPs. If you pull them into an array, you can query the array to see how many you have.

[user@system ~]$ floats=( $( openstack floating ip list -f json | jq  --raw-output '.[] | select(.["Fixed IP Address"] == null ) | .["Floating IP Address"]' ) )
[user@system ~]$ echo ${#floats[@]}
2

这篇关于如何检查池中可用的浮动IP的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆