Ansible Dynamic Inventory with NodeJS

Dynamic Inventory Requirements

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

However, I am showing here that it is possible to do that with NodeJS and any other language. The most important thing is to comply with dynamic inventory requirements:

  • The file must be executable ( chmod +x inventory/

Requirements Compliance with NodeJs:

  • The file must be executable ( chmod +x inventory/myfile.js)


  1. Download the lab environment and checkout the relevant branch:

git clone -b dynamic-inventory-nodejs

2. Follow README to configure the app (generate ssh-key,….)

This is Optional as we are only playing with inventory.

3. Run the lab environment docker-compose up -d

4. Go to the control node : docker-compose exec controlnode ash

You should see the PS1 of the container /playbook # `

5. Inspect the content of inventory/dynamic_hosts.js

/playbook # cat inventory/dynamic_hosts.js

a. The first line is the shebang

b. “const option = process.argv[2];” to parse the value of the first agrument: --list or --host

c. If it is --host , “const hostName = process.argv[3];” is to parse the hostname.

d. “const printer = (data) => console.log(JSON.stringify(data))” : To print data on the STDOUT in JSON format not JS object format.

6. Play with this file:

>> --list option:

/playbook # inventory/dynamic_hosts.js --list{"webservers":["web1","web2"],"databases":["db1","db2"]}

>> --host option:

/playbook # inventory/dynamic_hosts.js --host web1{"webserver":"apache","welcomeMessage":"Hello Here"}

7. Validatation with ansible-inventory` command :

ansible-inventory --list


ansible-inventory --host web1

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

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