如何检查池中可用的浮动IP数量?

时间:2020-12-04 16:55:20

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?

我正在编写一个脚本来在Openstack上创建一个VM。如果浮动IP在池中耗尽,我可能会收到错误。如何检查该池中是否有可用的浮动IP?有没有一种方法可以让openstack自动从所有可用的池中选择池?

2 个解决方案

#1


1  

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.

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

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

(请注意“无”列是仅显示文本。实际存储的字段值为“null”。)

Given:

鉴于:

[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                                 |
+--------------------------------------+---------------------+------------------+--------------------------------------+

Which looks like this in json:

在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
  }
]

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.

使用'jq'解析此输出,请允许我先用英语解释。 jq中的管道就像bash外壳中的管道一样。所以“拿整个阵列”| “选择此字段等于此值”| “归还其他领域”。如果需要可以返回多个字段。

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

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

如果您不希望结果用引号,请询问原始输出(简称-r)。

[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

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

这些是您可用的浮动IP。如果将它们拉入数组,则可以查询数组以查看您拥有的数量。

[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

#2


1  

You can see the documentation of scripting API you are using , but from the command line to list all floating IP addresses that are allocated to the current project, run:

您可以查看正在使用的脚本API的文档,但是从命令行列出分配给当前项目的所有浮动IP地址,运行:

   $ openstack floating ip list
   +--------------------------------------+---------------------+------------------+------+
   | ID                                   | Floating IP Address |  Fixed IP Address | Port |
    +--------------------------------------+---------------------+------------------+------+
   | 760963b2-779c-4a49-a50d-f073c1ca5b9e | 172.24.4.228        | None             | None |
   | 89532684-13e1-4af3-bd79-f434c9920cc3 | 172.24.4.235        | None             | None |
   | ea3ebc6d-a146-47cd-aaa8-35f06e1e8c3d | 172.24.4.229        | None             | None |
   +--------------------------------------+---------------------+------------------+------+

you can then do some command line editing to extract the ip colmn and have an ip count.

然后你可以做一些命令行编辑来提取ip colmn并有一个ip计数。

#1


1  

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.

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

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

(请注意“无”列是仅显示文本。实际存储的字段值为“null”。)

Given:

鉴于:

[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                                 |
+--------------------------------------+---------------------+------------------+--------------------------------------+

Which looks like this in json:

在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
  }
]

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.

使用'jq'解析此输出,请允许我先用英语解释。 jq中的管道就像bash外壳中的管道一样。所以“拿整个阵列”| “选择此字段等于此值”| “归还其他领域”。如果需要可以返回多个字段。

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

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

如果您不希望结果用引号,请询问原始输出(简称-r)。

[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

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

这些是您可用的浮动IP。如果将它们拉入数组,则可以查询数组以查看您拥有的数量。

[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

#2


1  

You can see the documentation of scripting API you are using , but from the command line to list all floating IP addresses that are allocated to the current project, run:

您可以查看正在使用的脚本API的文档,但是从命令行列出分配给当前项目的所有浮动IP地址,运行:

   $ openstack floating ip list
   +--------------------------------------+---------------------+------------------+------+
   | ID                                   | Floating IP Address |  Fixed IP Address | Port |
    +--------------------------------------+---------------------+------------------+------+
   | 760963b2-779c-4a49-a50d-f073c1ca5b9e | 172.24.4.228        | None             | None |
   | 89532684-13e1-4af3-bd79-f434c9920cc3 | 172.24.4.235        | None             | None |
   | ea3ebc6d-a146-47cd-aaa8-35f06e1e8c3d | 172.24.4.229        | None             | None |
   +--------------------------------------+---------------------+------------------+------+

you can then do some command line editing to extract the ip colmn and have an ip count.

然后你可以做一些命令行编辑来提取ip colmn并有一个ip计数。