1.0.1 • Public • Published

pageresBuild Status

Ansible LXDynamic Inventory (with NodeJS)

A dynamic inventory script for use with Ansible and LXD Containers and written in node.js.

I did find one other dynamic inventory script for LXD, however it did not meet my needs. Some requirements which are met by this script are:

  • Utilizes the LXD REST API throughout starting at the Ansible host.
  • Groups can be dynamically configured based on meta variables set on the containers.

And I chose NodeJS because I like JavaScript.


$ npm install --save

This will simply copy the precompiled lxd.nex script, along with lxd.ini into your inventory directory. Then update your inventory file and add any LXD hosts using the lxd connector as in ubuntu-adi-test-lxdserver ansible_connection=lxd.

You should then be able to issue a playbook or other applicable ansible command to any LXD containers configured on that LXD host! Code Editor Screenshot lxd.ini

Building from source

$ npm build

Will compile the lxd.js script into a self contained executable lxd.nex which can be used without requiring installation of node dependancies.

Although the actual build of the lxd.js script should work anywhere (that node would work), the npm install from source only supports Ubuntu and has been tested on both Trusty (Travis-ci) and Xenial. Basically the tests involve creating a test LXD server which in turn creates several nested test containers, afterwhich the script can be used on the aforementioned test environment.

a bit more detail...

Since authentication for the LXD API is done through client certificate authentication, this script will automatically generate a client certificate. You will want to change the lxd.ini file to reflect your own SSL cert details:

Code Editor Screenshot lxd.ini

The certifcate will be stored in ~/.ansible/tmp/ssl or whatever location you have configured for remote_tmp in your ansible.cfg file.

Dynamic Groups

The script will check the LXD container configuration value in order to determine group placement within the Ansible Inventory. Container configs can be set when creating containers by using the lxc command line as follows:

lxc config set <container> <key> <value>

thus to have this script dynamically group your containers into say 3 different groups named "testgroup1", "anygroupname2", and "group3", you would use the following commands to set the config values on the applicable containers:

lxc config set container1 testgroup1
lxc config set container2 testgroup1
lxc config set black anygroupname2
lxc config set white anygroupname2
lxc config set yello anygroupname2
lxc config set miosotis group3
lxc config set abbi group3
lxc config set twgirl group3
lxc config set Liliana group3

or by other APIs such as the LXD REST API More on available APIs here Groups not configured with a value for would simply be ungrouped in the Ansible inventory.


MIT B) June07

Package Sidebar


npm i ansible-dynamic-inventory

Weekly Downloads






Last publish


  • 667