Ansible Dynamic Inventory with NodeJS

Dynamic Inventory Requirements

The most famous programing language is Python used to develop a dynamic Ansible inventory.

  • The file content must start by shebang (i.e: #!/usr/bin/python)
  • When it is called with “ — list” option, the stdout must be a JSON object presents all groups, and each group is an array of hosts names.
  • When it is called with “ — host <myhost>”, the stdout must be JSON object presents all variables of the specific host myhost.
  • The file extension is not ignored by Ansible configuration under the attribute inventory_ignore_extensions`

Requirements Compliance with NodeJs:

  • The file must be executable ( chmod +x inventory/myfile.js)
  • The file content must start by shebang (i.e: #!/usr/bin/node) . Check the right path by running (which node))
  • “.js” must not be listed under inventory_ignore_extensions

Example

  1. Download the lab environment and checkout the relevant branch:
/playbook # inventory/dynamic_hosts.js --list{"webservers":["web1","web2"],"databases":["db1","db2"]}
/playbook # inventory/dynamic_hosts.js --host web1{"webserver":"apache","welcomeMessage":"Hello Here"}
ansible-inventory --list
ansible-inventory --host web1

--

--

Software engineer, Cloud Architect, 5/5 AWS|GCP|PSM Certified, Owner of kubernetes.tn

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store