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?
-
Please show some code , what scripting API are you using , some command line client , or some python API or what? Check the documentation of scripting API you are using. – Ijaz Ahmad Jan 14 '17 at 13:08
-
It is a shell script, I am using openstack standard CLI – Atmesh Mishra Jan 16 '17 at 07:17
2 Answers
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:
$ 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.

- 11,198
- 9
- 53
- 73
-
The question is how to know the number of floating IPs available in a floating-ip pool. The solution you have gives gives me count IPs which are allocated but not associated to VMs. I want to know the number of floating IPs which "can" be allocated from the floating IP pool. – Atmesh Mishra Jan 16 '17 at 07:16
-
Can be allocated to vms ?? i think i the above table , the port colomn is none , so these are free to be allocated to virtual machines – Ijaz Ahmad Jan 16 '17 at 07:52
-
Sir, there is a difference between allocating IP to a project and associating that allocated IP to a VM. – Atmesh Mishra Jan 16 '17 at 08:19
-
So what you are looking for ? for allocating to project you can choose any ip range you want. – Ijaz Ahmad Jan 16 '17 at 12:50
-
I know that. I simply want to know out of the given range of IPs, how many are still up for grab. Is there a way to find it or not? – Atmesh Mishra Jan 16 '17 at 12:56
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'.)
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:
[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.
[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).
[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.
[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

- 26
- 3
-
I do not have enough reputation yet to comment, so I will add a comment down here for Atmesh: when you have an openstack user environment loaded (even admin in the admin project) the default behavior of all commands is to show you ONLY what is available to you in YOUR project. There are some exceptions, but it is not relevent here. In your floating IP list, if you are working as a project member, then the list is the floating IPs that are available to you. Those assigned in your project can be seen in the "Fixed IP Address" column, showing to which server assigned. – Philip Sweany Jun 18 '17 at 01:11
-
I have two networks (IP pools/ subnets). I want to know the number of IPs available in the pool. If it exhausts, I would switch to a new pool. I dont want to know the number of FIPs available in my tenant, I just want to know if the pool from where the IP is getting allocated has FIPs available or not. Guessing from your comment above, its not possible. – Atmesh Mishra Jun 20 '17 at 06:04
-
1Atmesh: Maybe you misinterpreted, since what you ask *is* possible. 1) If a user can 'list' for anything and get items returned, then it is available for use. Anything, including FIPs, not shared or available to their project will not display on a 'list'. 2) To know if FIPs are available is to query and count. Using the above OSC scripting to count the lines with 'None' in them, a non-zero count means FIPs are available. 3) FIPs come from external networks, and 'openstack floating ip list' always returns *all* the FIPs available to you, from *all* external pools. – Philip Sweany Jun 21 '17 at 12:00