Search code examples
ansibleansible-toweransible-awx

Ansible AWX Custom Script for Local Inventory Not working


I have an Ansible AWX server setup within a container and have setup all bar Inventory of 3 local LAN virtual machines.

I am looking to add some already deployed virtual machines on my LAN for a test case, currently 3 hosts.

I was looking at custom dynamic inventory script like contained in link below, but does not seem to run correctly for me. I see an error in python syntax. I tried to upload anyway but get and error "needs to be a json dict" when run sync within inventories. This is also the line I have syntax error on line 32:

print json.dumps(self.inventory);

Dynamic Inventory Script

I am looking only to add the 3 host virtual machines or subnet to inventory to test some playbooks.


Solution

  • This is also the line I have syntax error on line 32:

    print json.dumps(self.inventory);

    That means it is very likely that your python is actually python 3, in which print became a function, not a keyword. Thus, you will want to do one, or perhaps both, of these things:

    1. change print json... to print(json.dumps(self.inventory))
    2. add a declaration to the top of the file to ensure the script will continue to behave rationally even when run on python 2:

      #!/usr/bin/env python
      from __future__ import print_function
      
      '''
      Example custom dynamic inventory script for Ansible, in Python.