-1

I want to print result in ansible-playbook but not working.
python script:

#!/usr/bin/python3

import time

while True:
    print("I'm alive")
    time.sleep(5)

deploy_python_script.yml:


  connection: local
  become: true

  vars:
    python_script_src: /home/ubuntu/scripts/python_script.py
    python_script_dest: /opt/web_service/python_script.py
    python_script_mode: "0755"
    python_interpreter: /usr/bin/python3

  tasks:
    - name: Ensure destination directory exists
      file:
        path: /opt/web_service
        state: directory
        mode: "0755"

    - name: Copy Python script to server
      copy:
        src: "{{ python_script_src }}"
        dest: "{{ python_script_dest }}"
        mode: "{{ python_script_mode }}"

    - name: Run the script
      command: "{{ python_interpreter }} {{ python_script_dest }}"
      register: script_result
      become: true
      become_user: root

    - name: Check if script ran
      debug:
        msg: "Script ran: {{ script_output.changed }}"

    - name: Show output
      debug:
        var: script_result

I ran the following command:

ansible-playbook -i localhost -vvv deploy_python_script.yml

result:

TASK \[Run the script\] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

task path: /home/ubuntu/ansible/deploy_python_scriptt.yml:27

\<127.0.0.1\> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu

\<127.0.0.1\> EXEC /bin/sh -c 'echo \~ubuntu && sleep 0'

\<127.0.0.1\> EXEC /bin/sh -c '( umask 77 && mkdir -p "\` echo /home/ubuntu/.ansible/tmp \`"&& mkdir "\` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1709641109.322795-17936-40556479292100 \`" && echo ansible-tmp-1709641109.322795-17936-40556479292100="\` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1709641109.322795-17936-40556479292100 \`" ) && sleep 0'

Using module file /usr/lib/python3/dist-packages/ansible/modules/command.py

\<127.0.0.1\> PUT /home/ubuntu/.ansible/tmp/ansible-local-17791bx4xmv_v/tmplblg_5p8 TO /home/ubuntu/.ansible/tmp/ansible-tmp-1709641109.322795-17936-40556479292100/AnsiballZ_command.py

\<127.0.0.1\> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-tmp-1709641109.322795-17936-40556479292100/ /home/ubuntu/.ansible/tmp/ansible-tmp-1709641109.322795-17936-40556479292100/AnsiballZ_command.py && sleep 0'

\<127.0.0.1\> EXEC /bin/sh -c 'sudo -H -S -n  -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-fqbwhdiehqpybtbtjwcnrafqdjqdbfbf ; /usr/bin/python3 /home/ubuntu/.ansible/tmp/ansible-tmp-1709641109.322795-17936-40556479292100/AnsiballZ_command.py'"'"' && sleep 0
10
  • 1
    Instead of copy and command it is recommended to use the script module – Runs a local script on a remote node after transferring it. Commented Mar 5, 2024 at 12:40
  • 3
    You might want to have a script that doesn't run forever. Commented Mar 5, 2024 at 12:40
  • 1
    Does this answer your question? Running Python script via ansible and many more which can be found by search ... Commented Mar 5, 2024 at 12:45
  • I also used ansible.builtin.script but it doesn't show the output... @U880D Commented Mar 5, 2024 at 12:50
  • 1
    Does that mean that the text "I am alive" should not be printed every five seconds? @U880D Commented Mar 5, 2024 at 12:58

1 Answer 1

0

For a Python script

#!/usr/bin/python

import time

print("I'm alive")
time.sleep(5)

a minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - script: alive.py
    register: result

  - debug:
      var: result

called via

ansible-playbook script.yml -v

will result into an output of

TASK [script] *****************************************************************
changed: [localhost] => changed=true
  rc: 0
  stderr: ''
  stderr_lines: <omitted>
  stdout: |-
    I'm alive
  stdout_lines: <omitted>
Wednesday 06 March 2024  08:00:00 +0100 (0:00:05.152)       0:00:05.241 *******

TASK [debug] ******************************************************************
ok: [localhost] =>
  result:
    changed: true
    failed: false
    rc: 0
    stderr: ''
    stderr_lines: []
    stdout: |-
      I'm alive
    stdout_lines:
    - I'm alive

PLAY RECAP ********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Playbook run took 0 days, 0 hours, 0 minutes, 5 seconds
Sign up to request clarification or add additional context in comments.

Comments

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.