LXC provider plugin for Terraform.
- Terraform. Make sure you have it installed and it's accessible from your
$PATH
. - LXC
- Install the
lxc-dev
package appropriate for your distribution. - Install Go and configure your workspace.
- Install
godep
:
$ go get github.com/tools/godep
- Download this repo:
$ go get github.com/jtopjian/terraform-provider-lxc
- Install the dependencies:
$ cd $GOPATH/src/github.com/jtopjian/terraform-provider-lxc
$ godep restore
- Compile it:
$ go build -o terraform-provider-lxc
- Copy it to a directory:
$ sudo cp terraform-provider-lxc ~/lxc-demo
Here's a simple Terraform file to get you started:
provider "lxc" {}
resource "lxc_container" "ubuntu" {
name = "ubuntu"
}
resource "lxc_clone" "ubuntu_clone" {
name = "ubuntu_clone"
source = "${lxc_container.ubuntu.name}"
}
Here's a more complete example that does the following:
- Creates a new bridge called
my_bridge
. - Creates an Ubuntu container with two interfaces: one on the default
lxcbr0
and one onmy_bridge
. - Creates an Ubuntu container with one interface on the
my_bridge
bridge.
provider "lxc" {}
resource "lxc_bridge" "my_bridge" {
name = "my_bridge"
}
resource "lxc_container" "ubuntu" {
name = "ubuntu"
template_name = "ubuntu"
template_release = "trusty"
template_arch = "amd64"
template_extra_args = ["--auth-key", "/root/.ssh/id_rsa.pub"]
network_interface {
type = "veth"
options {
link = "lxcbr0"
flags = "up"
hwaddr = "00:16:3e:xx:xx:xx"
}
}
network_interface {
type = "veth"
options {
link = "${lxc_bridge.my_bridge.name}"
flags = "up"
hwaddr = "00:16:3e:xx:xx:xx"
veth.pair = "foobar"
ipv4 = "192.168.255.1/24"
}
}
}
resource "lxc_container" "ubuntu2" {
name = "ubuntu2"
template_name = "ubuntu"
template_release = "trusty"
template_arch = "amd64"
template_extra_args = ["--auth-key", "/root/.ssh/id_rsa.pub"]
network_interface {
type = "veth"
options {
link = "${lxc_bridge.my_bridge.name}"
flags = "up"
hwaddr = "00:16:3e:xx:xx:xx"
veth.pair = "barfoo"
ipv4 = "192.168.255.2/24"
}
}
}
For either example, save it to a .tf
file and run:
$ terraform plan
$ terraform apply
$ terraform show
provider "lxc" {
lxc_path = "/var/lib/lxc"
}
lxc_path
: Optional. Explicitly set the path to where containers will be built.
resource "lxc_bridge" "my_bridge" {
name = "my_bridge"
}
name
: Required. The name of the bridge.
mac
: The MAC address of the new bridge.
resource "lxc_container" "my_container" {
name = "my_container"
backend = "zfs"
network_interface {
type = "veth"
options {
link = "lxcbr0"
flags = "up"
hwaddr = "00:16:3e:xx:xx:xx"
}
}
}
name
: Required. The name of the container.backend
: Optional. The storage backend to use. Valid options are: btrfs, directory, lvm, zfs, aufs, overlayfs, loopback, or best. Defaults todirectory
.exec
: Optional. Commands to run after container creation. This won't be interpreted by a shell so usebash -c "{shellcode}"
if you want a shell.template_name
: Optional. Defaults todownload
. See/usr/share/lxc/templates
for more template options.template_distro
: Optional. Defaults toubuntu
.template_release
: Optional. Defaults totrusty
.template_arch
: Optional. Defaults toamd64
.template_variant
: Optional. Defaults todefault
.template_server
: Optional. Defaults toimages.linuxcontainers.org
.template_key_id
: Optional.template_key_server
: Optional.template_flush_cache
: Optional. Defaults tofalse
.template_force_cache
: Optional. Defaults tofalse
.template_disable_gpg_validation
: Optional. defaults tofalse
.template_extra_args
: Optional. A list of extra parameters to pass to the template.options
: Optional. A set of key/value pairs of extra LXC options. Seelxc.container.conf(5)
.network_interface
: Optional. Defines a NIC.type
: Optional. The type of NIC. Defaults toveth
.management
: Optional. Make this NIC the management / accessible NIC.options
: Optional. A set of key/valuelxc.network.*
pairs for the NIC.
Because lxc.network.type
must be the first line that denotes a new NIC, a separate network_interface
parameter is used rather than bundling it all into options
address_v4
: The first discovered IPv4 address of the container.address_v6
: The first discovered IPv6 address of the container.
resource "lxc_clone" "my_clone" {
name = "my_clone"
source = "my_container"
backend = "zfs"
network_interface {
type = "veth"
options {
link = "lxcbr0"
flags = "up"
hwaddr = "00:16:3e:xx:xx:xx"
}
}
}
name
: Required. The name of the container.source
: Required. The source of this clone.backend
: Optional. The storage backend to use. Valid options are: btrfs, directory, lvm, zfs, aufs, overlayfs, loopback, or best. Defaults todirectory
.keep_mac
: Optional. Keep the MAC address(es) of the source. Defaults tofalse
.snapshot
: Optional. Whether to clone as a snapshot instead of copy. Defaults tofalse
.options
: Optional. A set of key/value pairs of extra LXC options. Seelxc.container.conf(5)
.network_interface
: Optional. Defines a NIC.type
: Optional. The type of NIC. Defaults toveth
.management
: Optional. Make this NIC the management / accessible NIC.options
: Optional. A set of key/valuelxc.network.*
pairs for the NIC.
address_v4
: The first discovered IPv4 address of the container.address_v6
: The first discovered IPv6 address of the container.