简体   繁体   中英

Terraform Openstack attach pre-allocated Floating IPs to instance

I have a use case where I need to re-use detached Floating IPs. Is there a way to do this in Terraform? I've tried:

`
data "openstack_networking_floatingip_v2" "fips" {
   status = "DOWN"
}
`

to get a list of detached IPs, but I get an error saying there is more than one Floating IP (Which is true).

Is there a good way to get detached floating IPs as a data resource in terraform? The alternative is passing an array of available IPs via a wrapper script with the command outlined here: Reuse detached floating IPs in OpenStack

For anyone else that comes across this, here is I solved it for now:

I used the 'external' data resource to call the openstack cli to retrieve a comma seperated list of available ips. The openstack cli command looks like this:

openstack floating ip list --status DOWN -f yaml -c "Floating IP Address"  

In order to get the output in the format suitable for terraform's external data resource, I used a python script. The script outputs a json object that looks like this: {ips = "ip.1.2.3,ip.4.5.6,ip.7.8.9"}

The external data resource in terraform looks like this:

data "external" ips {
    program = ["python", "<path-to-python-script>"]
}

From there I'm able to split the comma seperated string of ips in terraform and access the the ips as an array:

output available_ips {
    value = split(",", data.external.ips.result.ips)
}

It's definitely not elegant, I wish the openstack_networking_floatingip_v2 data resource would allow for this functionality, I'll look into opening an issue to get it added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM