Control Linux Containers via Salt
depends: | lxc package for distribution |
---|
lxc >= 1.0 (even beta alpha) is required
salt.modules.lxc.
attachable
(name)¶Return True if the named container can be attached to via the lxc-attach command
CLI Example:
salt 'minion' lxc.attachable ubuntu
salt.modules.lxc.
bootstrap
(name, config=None, approve_key=True, install=True, pub_key=None, priv_key=None, bootstrap_url=None, force_install=False, unconditional_install=False, bootstrap_args=None, bootstrap_shell=None)¶Install and configure salt in a container.
salt 'minion' lxc.bootstrap name [config=config_data] \
[approve_key=(True|False)] [install=(True|False)]
master
option is set
to the target host's master.True
CLI Example:
salt '*' lxc.bootstrap ubuntu
salt.modules.lxc.
clone
(name, orig, snapshot=False, profile=None, **kwargs)¶Create a new container.
CLI Example:
salt 'minion' lxc.clone name orig [snapshot=(True|False)] \
[size=filesystem_size] [vgname=volume_group] \
[profile=profile_name]
CLI Example:
salt '*' lxc.clone myclone ubuntu "snapshot=True"
salt.modules.lxc.
cloud_init
(name, vm_=None, **kwargs)¶Thin wrapper to lxc.init to be used from the saltcloud lxc driver
CLI Example:
salt '*' lxc.cloud_init foo
None
and then guessed from saltcloud
mapping.vm_
salt.modules.lxc.
cloud_init_interface
(name, vm_=None, **kwargs)¶Interface between salt.cloud.lxc driver and lxc.init
vm_
is a mapping of vm opts in the salt.cloud format
as documented for the lxc driver.
This can be used either:
CLI Example:
salt '*' lxc.cloud_init_interface foo
vm_.get('netmask', '24')
additionnal ips which will be wired on the main bridge (br0) which is connected to internet. Be aware that you may use manual virtual mac addresses providen by you provider (online, ovh, etc). This is a list of mappings {ip: '', mac: '',netmask:''} Set gateway to None and an interface with a gateway to escape from another interface that eth0. eg:
- {'mac': '00:16:3e:01:29:40',
'gateway': None, (default)
'link': 'br0', (default)
'netmask': '', (default)
'ip': '22.1.4.25'}
salt.modules.lxc.
cp
(name, src, dest)¶Copy a file or directory from the host into a container
CLI Example:
salt 'minion' lxc.cp /tmp/foo /root/foo
salt.modules.lxc.
create
(name, config=None, profile=None, options=None, **kwargs)¶Create a new container.
CLI Example:
salt 'minion' lxc.create name [config=config_file] \
[profile=profile] [template=template_name] \
[backing=backing_store] [vgname=volume_group] \
[size=filesystem_size] [options=template_options]
salt.modules.lxc.
destroy
(name, stop=True)¶Destroy the named container. WARNING: Destroys all data associated with the container.
CLI Example:
salt '*' lxc.destroy name [stop=(True|False)]
salt.modules.lxc.
edit_conf
(conf_file, out_format='simple', **kwargs)¶Edit an LXC configuration file. If a setting is already present inside the file, its value will be replaced. If it does not exist, it will be appended to the end of the file. Comments and blank lines will be kept in-tact if they already exist in the file.
After the file is edited, its contents will be returned. By default, it
will be returned in simple
format, meaning an unordered dict (which
may not represent the actual file order). Passing in an out_format
of
commented
will return a data structure which accurately represents the
order and content of the file.
CLI Examples:
salt 'minion' lxc.edit_conf /etc/lxc/mycontainer.conf out_format=commented lxc.network.type=veth
salt.modules.lxc.
exists
(name)¶Returns whether the named container exists.
CLI Example:
salt '*' lxc.exists name
salt.modules.lxc.
freeze
(name)¶Freeze the named container.
CLI Example:
salt '*' lxc.freeze name
salt.modules.lxc.
get_base
(**kwargs)¶If the needed base does not exist, then create it, if it does exist create nothing and return the name of the base lxc container so it can be cloned.
CLI Example:
salt 'minion' lxc.init name [cpuset=cgroups_cpuset] \
[nic=nic_profile] [profile=lxc_profile] \
[nic_opts=nic_opts] [image=network image path]\
[seed=(True|False)] [install=(True|False)] \
[config=minion_config]
salt.modules.lxc.
get_parameter
(name, parameter)¶Returns the value of a cgroup parameter for a container.
CLI Example:
salt '*' lxc.get_parameter name parameter
salt.modules.lxc.
info
(name)¶Returns information about a container.
CLI Example:
salt '*' lxc.info name
salt.modules.lxc.
init
(name, cpuset=None, cpushare=None, memory=None, nic='default', profile=None, nic_opts=None, cpu=None, autostart=True, password=None, users=None, dnsservers=None, bridge=None, gateway=None, pub_key=None, priv_key=None, force_install=False, unconditional_install=False, bootstrap_args=None, bootstrap_shell=None, bootstrap_url=None, **kwargs)¶Initialize a new container.
This is a partial idempotent function as if it is already provisioned, we will reset a bit the lxc configuration file but much of the hard work will be escaped as markers will prevent re-execution of harmful tasks.
CLI Example:
salt 'minion' lxc.init name [cpuset=cgroups_cpuset] \
[cpushare=cgroups_cpushare] [memory=cgroups_memory] \
[nic=nic_profile] [profile=lxc_profile] \
[nic_opts=nic_opts] [start=(True|False)] \
[seed=(True|False)] [install=(True|False)] \
[config=minion_config] [approve_key=(True|False) \
[clone=original] [autostart=True] \
[priv_key=/path_or_content] [pub_key=/path_or_content] \
[bridge=lxcbr0] [gateway=10.0.3.1] \
[dnsservers[dns1,dns2]] \
[users=[foo]] password='secret'
Extra options for network interfaces. E.g:
{"eth0": {"mac": "aa:bb:cc:dd:ee:ff", "ipv4": "10.1.1.1", "ipv6": "2001:db8::ff00:42:8329"}}
or
{"eth0": {"mac": "aa:bb:cc:dd:ee:ff", "ipv4": "10.1.1.1/24", "ipv6": "2001:db8::ff00:42:8329"}}
True
True
True
None
salt.modules.lxc.
list_
(extra=False)¶List defined containers classified by status. Status can be running, stopped, and frozen.
- extra
Also get per container specific info at once. Warning: it will not return a collection of list but a collection of mappings by status and then per container name:
{'running': ['foo']} # normal mode {'running': {'foo': {'info1': 'bar'}} # extra mode
CLI Example:
salt '*' lxc.list
salt '*' lxc.list extra=True
salt.modules.lxc.
ls
()¶Return just a list of the containers available
CLI Example:
salt '*' lxc.ls
salt.modules.lxc.
read_conf
(conf_file, out_format='simple')¶Read in an LXC configuration file. By default returns a simple, unsorted dict, but can also return a more detailed structure including blank lines and comments.
CLI Examples:
salt 'minion' lxc.read_conf /etc/lxc/mycontainer.conf
salt 'minion' lxc.read_conf /etc/lxc/mycontainer.conf out_format=commented
salt.modules.lxc.
run_cmd
(name, cmd, no_start=False, preserve_state=True, stdout=True, stderr=False, use_vt=False, keep_env='http_proxy, https_proxy, no_proxy')¶Run a command inside the container.
CLI Example:
salt 'minion' name command [no_start=(True|False)] \
[preserve_state=(True|False)] [stdout=(True|False)] \
[stderr=(True|False)]
False
True
True
False
Note
If stderr and stdout are both False
, the return code is returned.
If stderr and stdout are both True
, the pid and return code are
also returned.
salt.modules.lxc.
set_dns
(name, dnsservers=None, searchdomains=None)¶Update container DNS configuration and possibly also resolv.conf one.
CLI Example:
salt-call -lall lxc.set_dns ubuntu ['8.8.8.8', '4.4.4.4']
salt.modules.lxc.
set_parameter
(name, parameter, value)¶Set the value of a cgroup parameter for a container.
CLI Example:
salt '*' lxc.set_parameter name parameter value
salt.modules.lxc.
set_pass
(name, users, password)¶Set the password of one or more system users inside containers
CLI Example:
salt '*' lxc.set_pass container-name root foo
salt.modules.lxc.
start
(name, restart=False)¶Start the named container.
CLI Example:
salt '*' lxc.start name
salt.modules.lxc.
state
(name)¶Returns the state of a container.
CLI Example:
salt '*' lxc.state name
salt.modules.lxc.
stop
(name, kill=True)¶Stop the named container.
CLI Example:
salt '*' lxc.stop name
salt.modules.lxc.
templates
(templates_dir='/usr/share/lxc/templates')¶Returns a list of existing templates
CLI Example:
salt '*' lxc.templates
salt.modules.lxc.
unfreeze
(name)¶Unfreeze the named container.
CLI Example:
salt '*' lxc.unfreeze name
salt.modules.lxc.
update_lxc_conf
(name, lxc_conf, lxc_conf_unset)¶Edit LXC configuration options
CLI Example:
salt-call -lall lxc.update_lxc_conf ubuntu lxc_conf="[{'network.ipv4.ip':'10.0.3.5'}]" lxc_conf_unset="['lxc.utsname']"
salt.modules.lxc.
write_conf
(conf_file, conf)¶Write out an LXC configuration file
This is normally only used internally. The format of the data structure
must match that which is returned from lxc.read_conf()
, with
out_format
set to commented
.
An example might look like:
[
{'lxc.utsname': '$CONTAINER_NAME'},
'# This is a commented line\n',
'\n',
{'lxc.mount': '$CONTAINER_FSTAB'},
{'lxc.rootfs': {'comment': 'This is another test',
'value': 'This is another test'}},
'\n',
{'lxc.network.type': 'veth'},
{'lxc.network.flags': 'up'},
{'lxc.network.link': 'br0'},
{'lxc.network.hwaddr': '$CONTAINER_MACADDR'},
{'lxc.network.ipv4': '$CONTAINER_IPADDR'},
{'lxc.network.name': '$CONTAINER_DEVICENAME'},
]
CLI Examples:
salt 'minion' lxc.write_conf /etc/lxc/mycontainer.conf \
out_format=commented