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
copyandcommandit is recommended to use thescriptmodule – Runs a local script on a remote node after transferring it.