3

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.

1
  • You could try using command instead of the script module, as suggested in this question as a workaround. Commented Jan 4, 2019 at 5:33

1 Answer 1

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!"
    ]
}
Sign up to request clarification or add additional context in comments.

6 Comments

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?
Then you might want to run the script module with "args: executable: python".
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?
Of course it should have python. Debugging should help you to find out what's going on.
Does that suggest that python_script.py is on the local machine, rather than on the remote host as the OP mentioned?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.