ansible / ansible

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.
https://www.ansible.com/
GNU General Public License v3.0
62.91k stars 23.9k forks source link

Error using dynamic inventory - 'The file rest_api_inventory.py is marked as executable, but failed to execute correctly." #17430

Closed sidcarter closed 8 years ago

sidcarter commented 8 years ago
ISSUE TYPE

core - Dynamic Inventory

ANSIBLE VERSION
$ ansible --version
ansible 2.1.1.0
  config file =
  configured module search path = Default w/o overrides
CONFIGURATION

No additional configuration.

OS / ENVIRONMENT

macOS Sierra 10.12 beta

$ uname -v
Darwin Kernel Version 16.0.0: Tue Aug 23 17:02:44 PDT 2016; root:xnu-3789.1.31~2/RELEASE_X86_64

Same error on CentOS 7.2 too

uname -a Linux 10.0.3.136 3.10.0-327.18.2.el7.x86_64 #1 SMP Thu May 12 11:03:55 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

SUMMARY

Executing an inventory script with ansible throws errors

STEPS TO REPRODUCE

Use inventory script for inventory sources. Inventory script generates out put --list and --host

inventory script:

$ ls -l rest_api_inventory.py -rwxr-xr-x@ 1 sidcarter staff 3184 Sep 6 19:43 rest_api_inventory.py

e.g.:

$ ./rest_api_inventory.py --list
{
  "all": {
    "hosts": [
      {
...snip

When run with ansible, it should show list of hosts.

EXPECTED RESULTS
$ ansible -i ./rest_api_inventory.py all --list-hosts
  hosts (9):
    10.45.32.40
    10.45.32.42
    10.45.32.43
    10.45.32.41
    10.45.32.39
    10.45.16.25
    10.45.16.26
    10.45.16.27
ACTUAL RESULTS
$ ansible -i ./rest_api_inventory.py all --list-hosts
ERROR! The file rest_api_inventory.py is marked as executable, but failed to execute correctly. If this is not supposed to be an executable script, correct this with `chmod -x rest_api_inventory.py`.
unhashable type: 'dict'
rest_api_inventory.py:3: Error parsing host definition '"""': No closing quotation
alikins commented 8 years ago

$ ansible -i ./rest_api_inventory.py all --list-hosts

Was this inventory script working before? Is the output valid JSON? (could say, './rest_api_inventory.py --list | python -m json.tool' work without error?)

sidcarter commented 8 years ago

@alikins it's a new script I created. Hadn't used it before.

And yes, the script spits out valid JSON

$ ./rest_api_inventory.py --list | python -m json.tool | head -5
{
    "all": {
        "hosts": [
            {
                "10.45.0.4": {
sivel commented 8 years ago

From what I can see, it is due to your inventory being malformed. Although valid JSON, it does not conform to the ansible requirements for a dynamic inventory output.

I can't see everything you have there, but at minimum, hosts should just be a list of strings, not a list of dictionaries.

See http://docs.ansible.com/ansible/developing_inventory.html for more information on proper formatting.

sidcarter commented 8 years ago

@sivel thank you. that fixed it. I'm getting the right output now:

$ ansible -i ./rest_api_inventory.py --list-hosts all | head -4
  hosts (88):
    10.45.0.4
    10.45.16.28
    10.45.0.17

I have two followup questions:

  1. Why does it take longer for the script to run via ansible? e.g.

$ time ./rest_api_inventory.py --list > /dev/null real 0m1.051s user 0m0.125s sys 0m0.055s

With ansible:

$ time ansible -i ./rest_api_inventory.py --list-hosts all > /dev/null

real    1m9.001s
user    0m10.481s
sys     0m4.793s

2nd question: what's the format for host specific variables? I thought that would be a dict. But the link you shared above does not have an example or direction to go for that.

thanks

sidcarter commented 8 years ago

Got the answer re: second question - http://tech.aabouzaid.com/2015/10/opsview-as-dynamic-inventory-for-ansible-python.html

sidcarter commented 8 years ago

Adding the "_meta" dictionary to the output fixed the speed issue. This issue can be closed.

Thank you @alikins and @sivel