I am trying to use the following json file as input to ansible host inventory but I get error when I run the playbook. JSON File:
{
"instances":{
"host": 10.66.70.33
}
}
hosts: "{{ instances.host }}"
remote_user: root #vars:
When I run the play book I get the following errors. I am not sure where I am doing wrong. I am new to Ansible. Please advice I guess i am doing some silly mistake.
[WARNING]: Could not match supplied host pattern, ignoring: all [WARNING]: provided hosts list is empty, only localhost is available ERROR! The field 'hosts' has an invalid value, which includes an undefined variable. The error was: 'instances' is undefined
I am running the playbook as follows:
ansible-playbook -i <path>/test.json <path>test_playbook.yml
It seems pure JSON it's not supported as inventory file. In the inventory plugin list I don't see JSON:
- advanced_host_list - Parses a ‘host list’ with ranges
- auto - Loads and executes an inventory plugin specified in a YAML config
- aws_ec2 - ec2 inventory source
- constructed - Uses Jinja2 to construct vars and groups based on existing inventory.
- host_list - Parses a ‘host list’ string
- ini - Uses an Ansible INI file as inventory source.
- k8s - Kubernetes (K8s) inventory source
- openshift - OpenShift inventory source
- openstack - OpenStack inventory source
- script - Executes an inventory script that returns JSON
- virtualbox - virtualbox inventory source
- yaml - Uses a specifically YAML file as inventory source.
On the other hand you can wrap that JSON in a simple python script as follows:
Make sure the script plugin is enabled in your ansible.cfg
file:
[inventory]
enable_plugins = host_list, script, yaml, ini
Create wrapper script (inventory file), only reads your JSON and prints it in the console (I'm assuming the JSON and the wrapper script are in the same path):
#!/usr/bin/env python
import os
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
with open(os.path.join(__location__, "hosts.json")) as f:
print f.read()
Use the following in your JSON inventory (performance improvements):
{
"_meta": {
"hostvars": { }
},
"instances": {
"hosts": ["10.66.70.33"]
}
}
When running the playbook just be aware the group you want to target should be "instances", for example this is my playbook:
---
- hosts: instances
tasks:
- debug:
msg: Hi there
Then just run the playbook as you did, specifying the python wrapper script, in my case this was:
ansible-playbook -i inventory/hostswrapper.py test-stkovfl.yml
Hope it helps!