# passing arguments to python script

**URL:** <https://forum.ansible.com/t/passing-arguments-to-python-script/38139>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [June 21, 2023, 1:50pm UTC](https://forum.ansible.com/t/passing-arguments-to-python-script/38139 "2023-06-21T13:50:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [June 21, 2023, 1:50pm UTC](https://forum.ansible.com/t/passing-arguments-to-python-script/38139/1 "2023-06-21T13:50:06Z")

</div>

Hi all

I am trying to pass the ansible hostnames as an array list to python script but could not figure out where the extra characters are coming from. I am passing with ansible\_play\_hosts\_all variable.  
In my host file there are 2 hoosts 1ld501 and 1ld994. but it is passing some unexpected character marked in red.

**Playbook:**

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 21, 2023, 2:30pm UTC](https://forum.ansible.com/t/passing-arguments-to-python-script/38139/2 "2023-06-21T14:30:23Z")

</div>

**ansible\_play\_hosts\_all** is a list, which is what you say that you want, so all should be fine.

Unless your script does not really expect a list, which I think is what is happening.  
If that is the case, either adopt your script to handle a list.  
Or, turn the list into string, separated by spaces:

**{{ ansible\_play\_hosts\_all | join(" ") }}**

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 21, 2023, 2:32pm UTC](https://forum.ansible.com/t/passing-arguments-to-python-script/38139/3 "2023-06-21T14:32:49Z")

</div>

The difference between a 'python list' ( [item, item2] ) and a 'shell  
list' `item item2`, as @Dick Visser shows a simple join can translate  
one to the other.

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [June 21, 2023, 2:53pm UTC](https://forum.ansible.com/t/passing-arguments-to-python-script/38139/4 "2023-06-21T14:53:24Z")

</div>

Thank you both for your kind help.  
my python script expects a list. I am expecting something like below list  
data= [[“1ld501”], [“1ld994”]]

This is working for me when I hardcoded inside python script. I am expecting similar list from ansible script.  
with the above join command all the hostname concatenates to one string which I am not anticipating. Kindly help.

Regards

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [June 21, 2023, 3:18pm UTC](https://forum.ansible.com/t/passing-arguments-to-python-script/38139/5 "2023-06-21T15:18:12Z")

</div>

Hi again. Sorry if I am spamming.

I tried with below 2 option it is passing the argument as expected. I changed from double quotes to single quote now it is working.  
**{{ ansible\_play\_hosts\_all | join(’ ') }}**

**{{ groups[‘all’] | join(’ ') }}**

**Thank you so much again**
