how to run python script with ansible-playbook?

2024/11/17 21:19:07

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

#!/usr/bin/python3import timewhile True:print("I'm alive")time.sleep(5)

deploy_python_script.yml:

connection: localbecome: truevars:python_script_src: /home/ubuntu/scripts/python_script.pypython_script_dest: /opt/web_service/python_script.pypython_script_mode: "0755"python_interpreter: /usr/bin/python3tasks:- name: Ensure destination directory existsfile:path: /opt/web_servicestate: directorymode: "0755"- name: Copy Python script to servercopy:src: "{{ python_script_src }}"dest: "{{ python_script_dest }}"mode: "{{ python_script_mode }}"- name: Run the scriptcommand: "{{ python_interpreter }} {{ python_script_dest }}"register: script_resultbecome: truebecome_user: root- name: Check if script randebug:msg: "Script ran: {{ script_output.changed }}"- name: Show outputdebug: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
Answer

For a Python script

#!/usr/bin/pythonimport timeprint("I'm alive")
time.sleep(5)

a minimal example playbook

---
- hosts: localhostbecome: falsegather_facts: falsetasks:- script: alive.pyregister: result- debug:var: result

called via

ansible-playbook script.yml -v

will result into an output of

TASK [script] *****************************************************************
changed: [localhost] => changed=truerc: 0stderr: ''stderr_lines: <omitted>stdout: |-I'm alivestdout_lines: <omitted>
Wednesday 06 March 2024  08:00:00 +0100 (0:00:05.152)       0:00:05.241 *******TASK [debug] ******************************************************************
ok: [localhost] =>result:changed: truefailed: falserc: 0stderr: ''stderr_lines: []stdout: |-I'm alivestdout_lines:- I'm alivePLAY RECAP ********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0Playbook run took 0 days, 0 hours, 0 minutes, 5 seconds
https://en.xdnf.cn/q/118762.html

Related Q&A

How to concatenate pairs of row elements into a new column in a pandas dataframe?

I have this DataFrame where the columns are coordinates (e.g. x1,y1,x2,y2...). The coordinate columns start from the 8th column (the previous ones are irrelevant for the question) I have a larger exam…

Python: using threads to call subprocess.Popen multiple times

I have a service that is running (Twisted jsonrpc server). When I make a call to "run_procs" the service will look at a bunch of objects and inspect their timestamp property to see if they s…

Find a substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Image processing with single to multiple images

I have an Image showing below: I need to crop the order using python coding. What I need is only the card. So I want to crop the border. How to do it??This is the output I got using the code mentione…

SQLAlchemy Automap not loading table

I am using SQLAlchemy version 2.0.19 (latest public release). I am trying to map existing tables as documented in https://docs.sqlalchemy.org/en/20/orm/extensions/automap.html#basic-use I created a SQL…

Create a base 12 calculator with different limits at diferent digits with python

I want o create a calculator that can add (and multiply, divide, etc) numbers in base 12 and with different limits at the different digits.Base 12 sequence: [0,1,2,3,4,5,6,7,8,9,"A","B&q…

Python Program to check if a number is armstrong or not is not working, what am I doing wrong?

n=int(input("Enter a Number: ")) x=0 y=0 z=0while(n>0):x=n%10y=x**3z=z+yn=n//10print (z) #The z here is the same value which I enter, yet it doesnt work. #If I enter 407 as n, z becomes (4…

Python(Scrapy) unpredictable mistake with import load_entry_point

I have such problem, I did nothing with Python or Scrapy, but when I started today my computer I got such error. I have found many different posts and tried some tips and advices, unfortunately, they a…

Debugging RadioButtons program in Python

from Tkinter import *class Application (Frame):def __init__(self, master):Frame.__init__(self, master)self.grid()self.create_widgets()def create_widgets(self):Label(self, text = "Select the last b…

Why am I getting an Internal Server error

My python script runs just fine on the Apache server locally set up on my computer, however, on importing the json2html library I am getting an internal server errorThe moment I comment the import stat…