2

I am executing python script through ansible. I have some prints in python but none seem to display in the Ansible output. I see following in the output of the Ansible.

{"changed": true, "rc": 0, "stderr": "", "stdout": "", "stdout_lines": []}

I have tried the following to display the prints in ansible.

tasks:   
  - name: Run script on remote host
    script: python_script.py 
    register: output

  - debug: var=output.stdout_lines

But see that none of the prints are being displayed.

ok: [remoteHostName] => {
    "output.stdout_lines": []

I am new to Ansible. Any references or guidance would help me print the log from python script.

siri
  • 109
  • 1
  • 9
  • You could try using `command` instead of the `script` module, as suggested in [this question](https://stackoverflow.com/questions/51252170/using-the-script-module-in-ansible) as a workaround. – ILMostro_7 Jan 04 '19 at 05:33

1 Answers1

3

Your Ansible code is fine. See below. You should check the output of your script python_script.py.

  tasks:
    - script: python_script.py
      register: output
    - debug: var=output.stdout_lines


> cat python_script.py 
#!/usr/bin/python
print("Hello World!")

> ansible-playbook test-10.yml
[...]
TASK [debug] ******************************************
ok: [localhost] => {
    "output.stdout_lines": [
        "Hello World!"
    ]
}
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • my pyhton script is very simple but it does not star with #!/usr/bin/python . Its just something like below. `import sys print("Hello World")` . I do not see any prints coming in from the script. How do I make sure my script is definitely called from ansible and executed? – siri Jan 04 '19 at 15:47
  • Then you might want to run the [script module](https://docs.ansible.com/ansible/latest/modules/script_module.html) with "args: executable: python". – Vladimir Botka Jan 04 '19 at 15:50
  • I tried using #!/usr/bin/python but it did not work. I am running this script on a host. How do i know that the host has actualy run the script? if it ran then it should produce the prints statements and does not. It always gives me rc=0 and its confusing.Should the host which is windows machine also have python installed on it to run it? – siri Jan 04 '19 at 20:00
  • Of course it should have python. [Debugging](https://stackoverflow.com/questions/42417079/how-to-debug-ansible-issues) should help you to find out what's going on. – Vladimir Botka Jan 04 '19 at 23:01
  • Does that suggest that `python_script.py` is on the local machine, rather than on the remote host as the OP mentioned? – ILMostro_7 Jan 05 '19 at 08:03
  • Yes, python_script.py is on the local machine and [1) will be transferred to the remote node and then executed. 2) will be processed through the shell environment on the remote node](https://docs.ansible.com/ansible/latest/modules/script_module.html). The doc also says that "This **module** does not require python on the remote system". But, in your case the **script** needs python on the remote system – Vladimir Botka Jan 05 '19 at 11:17