如何使用ansible-playbook运行python脚本?

问题描述 投票:0回答:1

我想在

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
python ubuntu networking ansible virtual-machine
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
© www.soinside.com 2019 - 2024. All rights reserved.