# Is it possible to pass arguments to an inventory script?

**URL:** <https://forum.ansible.com/t/is-it-possible-to-pass-arguments-to-an-inventory-script/26978>\
**Category:** AWX Project\
**Tags:** awx\
**Created:** [March 13, 2018, 7:34am UTC](https://forum.ansible.com/t/is-it-possible-to-pass-arguments-to-an-inventory-script/26978 "2018-03-13T07:34:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcG](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@MarcG](https://forum.ansible.com/u/MarcG)\
**Post date:** [March 13, 2018, 7:34am UTC](https://forum.ansible.com/t/is-it-possible-to-pass-arguments-to-an-inventory-script/26978/1 "2018-03-13T07:34:26Z")

</div>

Hi, I have written a inventory script written in Python, that I am using in AWX.

What I would like to do is pass some arguments to it some how to set some variables used within the script.

I know that AWX runs the script with the —list flag. Is there a way to pass other arguments to it from say a job template or elsewhere within AWX?

Many thanks

---

<div class="post-metadata">

**Author:** ![AlanCoding](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alancoding/32/301_2.png) [@AlanCoding](https://forum.ansible.com/u/AlanCoding)\
**Post date:** [March 13, 2018, 11:40am UTC](https://forum.ansible.com/t/is-it-possible-to-pass-arguments-to-an-inventory-script/26978/2 "2018-03-13T11:40:20Z")

</div>

There is not an option to pass arguments directly, since Ansible itself does not allow this in any sense that I know of.

Instead, you can pass values through environment variables. This is the most common way that Ansible inventory scripts take inputs.

To do this in AWX, create a new credential type, and in here, define the environment variable structure you need. Then create a credential of this type. Then attach that credential to your inventory source. The script that runs should read from os.environ or something like that to take in the values. Here are some examples

[https://github.com/AlanCoding/Ansible-inventory-file-examples/tree/master/scripts/environment](https://github.com/AlanCoding/Ansible-inventory-file-examples/tree/master/scripts/environment)

---

<div class="post-metadata">

**Author:** ![matburt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matburt/32/3612_2.png) [@matburt](https://forum.ansible.com/u/matburt)\
**Post date:** [March 13, 2018, 7:42pm UTC](https://forum.ansible.com/t/is-it-possible-to-pass-arguments-to-an-inventory-script/26978/3 "2018-03-13T19:42:05Z")

</div>

There are also inventory source options that can be set and are available as an input box when setting up the inventory source.

---

<div class="post-metadata">

**Author:** ![MarcG](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@MarcG](https://forum.ansible.com/u/MarcG)\
**Post date:** [March 18, 2018, 9:06am UTC](https://forum.ansible.com/t/is-it-possible-to-pass-arguments-to-an-inventory-script/26978/4 "2018-03-18T09:06:12Z")

</div>

Thank you both very much for the detailed response.

---

<div class="post-metadata">

**Author:** ![Andrew\_Crisp](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@Andrew\_Crisp](https://forum.ansible.com/u/Andrew_Crisp)\
**Post date:** [April 26, 2018, 10:43am UTC](https://forum.ansible.com/t/is-it-possible-to-pass-arguments-to-an-inventory-script/26978/5 "2018-04-26T10:43:54Z")

</div>

Alan / Matthew - I’ve been pondering how to do this for scenario I am looking at.

we have a patching portal where users can assign their hosts to be patched in a particular slot. What we plan to then do is do a uri call to awx calling our patching job template and passing in the slot number. What I would like to do but can’t see how - is to then pass this slot number to the dynamic inventory script - so it can then go and retrieve the hosts for that slot. At moment the only thing I can think to do is retrieve ALL the hosts and their slots and use the slot number passed as a limit to the job template. However this could well be thousands of hosts so could well have performance impact retrieving them.

Any suggestions welcome.

Andy

---

<div class="post-metadata">

**Author:** ![Ken\_P](https://avatars.discourse-cdn.com/v4/letter/k/b782af/32.png) [@Ken\_P](https://forum.ansible.com/u/Ken_P)\
**Post date:** [September 3, 2022, 7:56pm UTC](https://forum.ansible.com/t/is-it-possible-to-pass-arguments-to-an-inventory-script/26978/6 "2022-09-03T19:56:37Z")

</div>

i have to tag a question as this is closet i got to my problem.  
our tower implementation in organization does not allow to create new “credential type”.  
in that case, is there any trick to pass env variable to my inventory script via Tower Launch API?
