ahnooie.lxd-container
LXD Container Ansible Role
This role helps manage LXD/LXC containers on a remote Linux Container Host. For more information, visit: Ubuntu Containers - LXD
Requirements
You need LXD version 2.0 or higher installed on both the LXD host and the Ansible server. It usually comes pre-installed with Ubuntu 16.04.
LXD should be set up on the remote host using the command
sudo lxd initor by using an Ansible Role like juju4/lxd.To let Ansible manage the LXD host from a distance, run the following commands beforehand:
On the remote LXD host:
$ lxc config set core.https_address [::]:8443
$ lxc config set core.trust_password replace-this-with-a-secure-password
On the Ansible host:
$ lxc config set core.https_address [::]:8443
$ lxc remote add lxd4 lxd4.example.com
(remember to replace lxd4.example.com with your actual LXD host's hostname; you can name 'lxd4' whatever you like for your inventory file)
- This role has been tested on both the LXD and Ansible host running Ubuntu 16.04 LTS, but it might also work on other distributions.
Role Variables
You can find more information about these variables here: Ansible LXD Container Module
state:started (default), stopped, restarted, absent, frozentype:image (default)mode:pull (default)server:https://images.linuxcontainers.org (default)protocol:lxd (default)alias:ubuntu/xenial/amd64 (default)wait_for_ipv4_addresses:true (default)timeout:600 (default)
Additional Variables:
public_key:"{{ lookup('file','~/.ssh/id_rsa.pub') }}" (default) - path to the public SSH key to install in the containerenable_ssh:true (default) - installs and enables the OpenSSH server in the container.lxd_host:your LXD container host
Dependencies
None
Installation
To install the role, use the following command:
$ ansible-galaxy install ahnooie.lxd-container
Example
The example below shows how to install 6 containers with different Linux distributions on the LXD host lxd4.example.com. It also installs Python, adds a public SSH key for the root user, and starts the SSH service on each container.
Inventory File Example
# Remote LXD Host
[lxd]
lxd4.example.com ansible_user=root
# Containers on LXD Hosts
[linux-containers]
ubuntu01.example.com ansible_host=lxd4:ubuntu01 alias=ubuntu/xenial/amd64
centos01.example.com ansible_host=lxd4:centos01 alias=centos/7/amd64
centos02.example.com ansible_host=lxd4:centos02 alias=centos/6/amd64
debian01.example.com ansible_host=lxd4:debian01 alias=debian/stretch/amd64
fedora01.example.com ansible_host=lxd4:fedora01 alias=fedora/27/amd64
[linux-containers:vars]
ansible_connection=lxd
lxd_host=lxd4.example.com
Playbook Example (containers.yml)
---
- hosts: linux-containers
gather_facts: false
vars:
public_key: "{{ lookup('file','public_keys/id_rsa.pub') }}"
roles:
- ahnooie.lxd-container
Running the Playbook
You can run the playbook using the following command:
$ ansible-playbook -i inventory containers.yml
License
MIT
Author Information
Created by Benjamin Bryan
ansible-galaxy install ahnooie.lxd-container