Hello guys, I'm unable to pass a dynamic inventory script to read a yml file from repo

I’m able to get the list of my hosts from the yml file when running this command

$ /opt/ansible/static/inventory/inventory.py https://github.com/adeyeri88/test.git staging.yml
{
“_meta”: {
“hostvars”: {}
},
“db”: {
“children”: [
“db-site2”,
“db-site1”
]
},
“db-site1”: {
“hosts”: [
bary785c.mylabserver.com”,
bary786c.mylabserver.com
]
},
“db-site2”: {
“hosts”: [
bary783c.mylabserver.com”,
bary785c.mylabserver.com
]
},
“site1”: {
“children”: [
“staging-site1”
]
},
“site2”: {
“children”: [
“staging-site2”
]
},
“staging”: {
“children”: [
“web”,
“db”
]
},
“staging-db”: {
“children”: [
“db”
]
},
“staging-db-site1”: {
“children”: [
“db-site1”
]
},
“staging-db-site2”: {
“children”: [
“db-site2”
]
},
“staging-site1”: {
“children”: [
“web-site1”,
“db-site1”
]
},
“staging-site2”: {
“children”: [
“web-site2”,
“db-site2”
]
},
“staging-web”: {
“children”: [
“web”
]
},
“staging-web-site1”: {
“children”: [
“web-site1”
]
},
“staging-web-site2”: {
“children”: [
“web-site2”
]
},
“web”: {
“children”: [
“web-site2”,
“web-site1”
]
},
“web-site1”: {
“hosts”: [
bary781c.mylabserver.com”,
bary782c.mylabserver.com
]
},
“web-site2”: {
“hosts”: [
bary783c.mylabserver.com”,
bary784c.mylabserver.com
]
}
}

But when I’m trying now to run any ansible command to call the same file, I’m getting this error.

]$ ansible -i /opt/ansible/static/inventory/inventory.py all -m ping -e “https://github.com/adeyeri88/test.git staging.yml”
[WARNING]: * Failed to parse /opt/ansible/static/inventory/inventory.py with script plugin: Inventory script (/opt/ansible/static/inventory/inventory.py) had an execution error: usage: inventory.py [-h] [–sshkey SSHKEY] [–commit
COMMIT] url inventory inventory.py: error: too few arguments

[WARNING]: Unable to parse /opt/ansible/static/inventory/inventory.py as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’

Please can you help me here?

Extra variables (-e, --extra-vars) are passed to the play not to the dynamic
inventory script. Create a wrapper for inventory.py with the parameters.

Cheers,

  -vlado

Thanks for your response. Where do I have to create the wrapper file? (is it in ansible.cfg) file.

> usage:
> > inventory.py [-h] [--sshkey SSHKEY] [--commit
> > COMMIT] url inventory inventory.py: error: too few arguments
>
> Extra variables (-e, --extra-vars) are passed to the play not to the
> dynamic
> inventory script. Create a wrapper for inventory.py with the parameters.

Thanks for your response. Where do I have to create the wrapper file? (is
it in ansible.cfg) file.

For example create a script inventory.sh.

  #!/bin/sh/
  ${INVPATH}/inventory.py https://github.com/adeyeri88/test.git staging.yml

and use it in the command

  ansible -i inventory.sh all -m ping

Hello boss. Here is what happened. when I use the Variable same you describe, I got an error. It seem I need to define INVPATH as variable. but I don’t know where.

[ansible@bary782c ~]$ ansible -i inventory.sh all -m ping

[WARNING]: * Failed to parse /home/ansible/inventory.sh with script plugin: Inventory script (/home/ansible/inventory.sh) had an execution error: /home/ansible/inventory.sh: line 4: ${ INVPATH}/inventory.py: bad substitution

[WARNING]: Unable to parse /home/ansible/inventory.sh as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’

and this the script

#!/bin/bash

${ INVPATH}/inventory.py https://github.com/adeyeri88/test.git staging.yml

as you gave me. But when I comment out that line and specified all the path in the script like this

#!/bin/bash

/opt/ansible/static/inventory/inventory.py https://github.com/adeyeri88/test.git staging.yml

#${ INVPATH}/inventory.py https://github.com/adeyeri88/test.git staging.yml

AND now run the the ansible command as you suggest it works

[ansible@bary782c ~]$ ansible -i inventory.sh all -m ping
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, but still be user configurable on deprecation. This feature will be removed in version 2.10.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

bary782c.mylabserver.com | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
bary786c.mylabserver.com | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
bary783c.mylabserver.com | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
bary781c.mylabserver.com | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
bary785c.mylabserver.com | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
bary784c.mylabserver.com | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}