如何使用ansible-playbook运行python脚本?
我想在 ansible-playbook
中打印结果,但没有成功。
这是我的 Python 脚本:
#!/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
我运行了以下命令:
ansible-playbook -i localhost -vvv deploy_python_script.yml
结果是:
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
1 个回答
0
这是一个Python脚本
#!/usr/bin/python
import time
print("I'm alive")
time.sleep(5)
这是一个简单的示例剧本
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- script: alive.py
register: result
- debug:
var: result
通过以下方式调用
ansible-playbook script.yml -v
将会得到以下输出
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