[ansible-project] passing arguments to python script

276 views
Skip to first unread message

Prady A

unread,
Jun 21, 2023, 9:50:58 AM6/21/23
to ansible...@googlegroups.com
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:
---
- name: Execute writetoexcel python
  script: ./roles/writetoexcel.py {{ ansible_play_hosts_all }}
  args:
    executable: python3
  delegate_to: localhost
 
Output:
<localhost> EXEC /bin/sh -c ' python3 /root/.ansible/tmp/ansible-tmp-1687354259.71-24015-237464957552077/writetoexcel.py [u'"'"'1ld501'"'"', u'"'"'1ld994'"'"'] && sleep 0'
 
changed: [1ld501 -> localhost] => {
    "changed": true,
    "rc": 0,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "[['[u1ld501,', 'u1ld994]']]\nData written to Excel successfully!\n",
    "stdout_lines": [
        "[['[u1ld501,', 'u1ld994]']]",
        "Data written to Excel successfully!"
    ]
}
 
Tried with Ansible_hostname no extra characters are there. It is as expected. Please suggest.

Playbook:
 ---
- name: Execute writetoexcel python
  script: ./roles/writetoexcel.py {{ ansible_hostname }}
  args:
    executable: python3
  delegate_to: localhost
 
Output:
<localhost> EXEC /bin/sh -c ' python3 /root/.ansible/tmp/ansible-tmp-1687354332.97-25377-185430066289739/writetoexcel.py 1ld994 && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1687354332.95-25376-35834468227973/ > /dev/null 2>&1 && sleep 0'
changed: [jp1ld501 -> localhost] => {
    "changed": true,
    "rc": 0,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "[['1ld994']]\nData written to Excel successfully!\n",
    "stdout_lines": [
        "[['1ld994']]",
        "Data written to Excel successfully!"
    ]
}

Regards
PD

Dick Visser

unread,
Jun 21, 2023, 10:30:58 AM6/21/23
to ansible...@googlegroups.com
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(" ") }}



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAEuB3ApSzymExcc7Auzp7P-v%2BxqYWiRp9GyWF%3Dzt2mQrC14Y%2BQ%40mail.gmail.com.

Brian Coca

unread,
Jun 21, 2023, 10:35:12 AM6/21/23
to ansible...@googlegroups.com
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.

--
----------
Brian Coca

Prady A

unread,
Jun 21, 2023, 10:54:18 AM6/21/23
to ansible...@googlegroups.com
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


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Prady A

unread,
Jun 21, 2023, 11:19:06 AM6/21/23
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages