ansible-dynamic-inventory

Ansible Dynamic Inventory Script for LXD Containers

Usage no npm install needed!

<script type="module">
  import ansibleDynamicInventory from 'https://cdn.skypack.dev/ansible-dynamic-inventory';
</script>

README

pageres">Build 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.

Install

$ 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 user.ansible.group 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 user.ansible.group testgroup1
lxc config set container2 user.ansible.group testgroup1
lxc config set black user.ansible.group anygroupname2
lxc config set white user.ansible.group anygroupname2
lxc config set yello user.ansible.group anygroupname2
lxc config set miosotis user.ansible.group group3
lxc config set abbi user.ansible.group group3
lxc config set twgirl user.ansible.group group3
lxc config set Liliana user.ansible.group group3

or by other APIs such as the LXD REST API More on available APIs here https://linuxcontainers.org/lxd/rest-api. Groups not configured with a value for user.ansible.group would simply be ungrouped in the Ansible inventory.

License

MIT B) June07