取消绑定以从Ansible playbook运行python脚本

取消绑定以从Ansible playbook运行python脚本,python,ansible,Python,Ansible,Ansible unbale从playbook运行python脚本 如果我尝试手动运行python脚本,效果会很好。 但当我从Playbook执行脚本时,它无法识别变量。 我得到以下错误: TASK [Updates the current location for the test] **********************************************************************************************************

Ansible unbale从playbook运行python脚本 如果我尝试手动运行python脚本,效果会很好。 但当我从Playbook执行脚本时,它无法识别变量。 我得到以下错误:

TASK [Updates the current location for the test] ****************************************************************************************************************************************************************************
fatal: [root@10.0.141.17]: FAILED! => {"changed": true, "cmd": "/root/Desktop/Under_Testing/10.0.124.90/PV/new_location.py", "delta": "0:00:00.046446", "end": "2020-09-02 14:07:31.527264", "msg": "non-zero return code", "rc": 1, "start": "2020-09-02 14:07:31.480818", "stderr": "Traceback (most recent call last):\n  File \"/root/Desktop/Under_Testing/10.0.124.90/PV/new_location.py\", line 11, in <module>\n    fin = open(f\"{location}/Run/Ansible/Run/check_copy.sh\", \"rt\")\nFileNotFoundError: [Errno 2] No such file or directory: '/root/Run/Ansible/Run/check_copy.sh'", "stderr_lines": ["Traceback (most recent call last):", "  File \"/root/Desktop/Under_Testing/10.0.124.90/PV/new_location.py\", line 11, in <module>", "    fin = open(f\"{location}/Run/Ansible/Run/check_copy.sh\", \"rt\")", "FileNotFoundError: [Errno 2] No such file or directory: '/root/Run/Ansible/Run/check_copy.sh'"], "stdout": "", "stdout_lines": []}

PLAY RECAP ******************************************************************************************************************************************************************************************************************
root@10.0.141.17           : ok=4    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
这是我的python脚本:

#!/usr/bin/python3

"""
This script for update the current location 
"""

import os, subprocess

location = os.getcwd()

fin = open(f"{location}/Run/Ansible/Run/check_copy.sh", "rt")
fin.close
with open(f"{location}/Run/Ansible/Run/check_copy_new.sh", "wt") as fout:
    for line in fin:
        fout.write(line.replace("/root/Desktop/Under_Testing/PV", f"{location}"))

fin = open(f"{location}/Run/Ansible/Run/systeminfo.sh", "rt")
fin.close
with open(f"{location}/Run/Ansible/Run/systeminfo_new.sh", "wt") as fout:
    for line in fin:
        fout.write(line.replace("/root/Desktop/Under_Testing/PV", f"{location}"))

脚本可以在没有问题的情况下手动运行,但通过Playbook失败,这可能是什么问题?

您的脚本在执行过程中依赖于当前目录来设置
{location}

默认情况下,
shell
模块在远程用户的home目录中执行命令(在您的情况下,在
/root/
中也是如此)

要更改执行目录,请使用
shell
module的参数:

-name:更新测试的当前位置
shell:/root/Desktop/Under_Testing/10.0.124.90/PV/new_location.py
args:
chdir:/root/Desktop/Under_Testing/10.0.124.90/PV/#据我从您的评论中了解
另一个解决方案是调整python脚本,使其不依赖于当前目录,而是依赖于脚本目录(因为它看起来正是您所需要的):

location=os.path.dirname(os.path.realpath(_文件__))

顺便说一句,“授予核心脚本权限”任务应该是不必要的,因为在复制任务期间您已经将模式设置为
0777

是否在
/root/Run/Ansible/Run/check_copy.sh
中存在文件?因为错误告诉你它是not@gold_cy我知道,复制后问题就开始了,Python脚本应该通过以下命令获取新位置:
location=os.getcwd()
,但它不设置新位置,而是设置位置
/root/
,该位置应该是脚本副本所在的位置。同样,如果我手动运行脚本,效果会很好
#!/usr/bin/python3

"""
This script for update the current location 
"""

import os, subprocess

location = os.getcwd()

fin = open(f"{location}/Run/Ansible/Run/check_copy.sh", "rt")
fin.close
with open(f"{location}/Run/Ansible/Run/check_copy_new.sh", "wt") as fout:
    for line in fin:
        fout.write(line.replace("/root/Desktop/Under_Testing/PV", f"{location}"))

fin = open(f"{location}/Run/Ansible/Run/systeminfo.sh", "rt")
fin.close
with open(f"{location}/Run/Ansible/Run/systeminfo_new.sh", "wt") as fout:
    for line in fin:
        fout.write(line.replace("/root/Desktop/Under_Testing/PV", f"{location}"))