• Zach Biles

Ansible Inventory Scripts

# TIL

It appears that you are required to use a .ini file with inventory scripts in Ansible. If you try and use static variables in the script, it will appear to work when running the raw Python or whatever you're using to gather the data. It will then fail with seemingly nondescript errors when you try and actually use the inventory.


So, in short make sure you use .ini files, and don't try and hard code vars in your script.


Example import script: https://github.com/bile0026/ansible_prtg_inventory_import


Example of output:


[user@host github]$ ansible-inventory -i import.py --list -vvvvv
ansible-inventory 2.9.10
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible-inventory
  python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /home/user/Documents/github/import.py as it did not pass its verify_file() method
auto declined parsing /home/user/Documents/github/import.py as it did not pass its verify_file() method
yaml declined parsing /home/user/Documents/github/import.py as it did not pass its verify_file() method
toml declined parsing /home/user/Documents/github/import.py as it did not pass its verify_file() method
[WARNING]:  * Failed to parse /home/user/Documents/github/import.py with script plugin: failed to parse executable inventory script results from
/home/user/Documents/github/import.py: Expecting value: line 1 column 1 (char 0) /usr/lib/python3.6/site-packages/urllib3/connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS
request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings   InsecureRequestWarning)
  File "/usr/lib/python3.6/site-packages/ansible/inventory/manager.py", line 280, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python3.6/site-packages/ansible/plugins/inventory/script.py", line 161, in parse
    raise AnsibleParserError(to_native(e))
[WARNING]:  * Failed to parse /home/user/Documents/github/import.py with ini plugin: /home/user/Documents/github/import.py:3: Expected key=value host variable
assignment, got: argparse
  File "/usr/lib/python3.6/site-packages/ansible/inventory/manager.py", line 280, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python3.6/site-packages/ansible/plugins/inventory/ini.py", line 138, in parse
    raise AnsibleParserError(e)
[WARNING]: Unable to parse /home/user/Documents/github/import.py as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    }
}


Recent Posts

See All

Ansible/AWX Custom Credential Types

Sometimes in Ansible you need to support legacy devices, or API’s that require hard coded passwords or API strings in your playbooks. One way around this is to use custom credential objects in AWX/Tow

Useful Ansible Ad-Hoc Commands

Ansible doesn’t always have to be crazy complex playbooks doing super cool stuff. Sometimes, it’s really useful to get some output from al your devices like versioning, VLANs, IP addresses, etc... Her

LINKS
ABOUT

info@sisulink.com

Inside TechTank

3709 1st Ave

Hibbing, MN 55746, USA

SOCIAL
  • sisuLink on Facebook!

© 2020 sisuLink.