我有一个 ansible 服务器,我发现它运行良好,但是当我尝试在部署的服务器上运行 python 脚本时,我得到了no such file or directory
,即使我直观地确认它们在那里。
这是我的 anisble 剧本代码:
create a user directory and a dev subdirectory
- name: Create directory for python files
file: path=/home/{{ user_name }}/python_files
state=directory
owner={{ user_name }}
group={{ user_name }}
mode=755
- name: Copy python file over
copy:
src=example.py
dest=/home/{{ user_name }}/python_files/example.py
owner={{ user_name }}
group={{ user_name }}
mode=777
- name: Execute script
command: /home/{{ user_name }}/python_files/example.py
这是我收到的错误:
TASK [python-dev : Execute script] *********************************************
task path: /usr/share/ansible-demo/roles/python-dev/tasks/main.yml:37
<192.168.1.240> ESTABLISH SSH CONNECTION FOR USER: None
<192.168.1.240> SSH: EXEC sshpass -d14 ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/achilles/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.1.240 '/bin/sh -c '"'"'( umask 22 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1460496110.48-100414848375832 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1460496110.48-100414848375832 `" )'"'"''
<192.168.1.240> PUT /tmp/tmpcLPC60 TO /home/achilles/.ansible/tmp/ansible-tmp-1460496110.48-100414848375832/command
<192.168.1.240> SSH: EXEC sshpass -d14 sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/achilles/.ansible/cp/ansible-ssh-%h-%p-%r '[192.168.1.240]'
<192.168.1.240> ESTABLISH SSH CONNECTION FOR USER: None
<192.168.1.240> SSH: EXEC sshpass -d14 ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/achilles/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.1.240 '/bin/sh -c '"'"'sudo -H -S -p "[sudo via ansible, key=dbpmnfbipbkpualueeaxdbkosbjdwsdk] password: " -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-dbpmnfbipbkpualueeaxdbkosbjdwsdk; /bin/sh -c '"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/achilles/.ansible/tmp/ansible-tmp-1460496110.48-100414848375832/command; rm -rf "/home/achilles/.ansible/tmp/ansible-tmp-1460496110.48-100414848375832/" > /dev/null 2>&1'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"''"'"'"'"'"'"'"'"''"'"''
fatal: [192.168.1.240]: FAILED! => {"changed": false, "cmd": "/home/achilles/python_files/example.py", "failed": true, "invocation": {"module_args": {"_raw_params": "/home/achilles/python_files/example.py", "_uses_shell": false, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "msg": "[Errno 2] No such file or directory", "rc": 2}
部署的服务器上的目录权限看起来正常:
achilles@ansible-test:~/python_files$ ll
total 12
drwxr-xr-x 2 achilles achilles 4096 Apr 12 15:56 ./
drwxr-xr-x 18 achilles achilles 4096 Apr 12 15:56 ../
-rwxrwxrwx 1 achilles achilles 153 Apr 12 15:56 example.py*
有什么建议吗?我不认为我忽略了显而易见的问题,但我对 ansible 还很陌生,而且关联没有帮助。
答案1
为了在另一个目录中运行 Python 脚本,您需要实现一个chdir
。Ansible允许您通过其参数执行此操作args
。
例子:
- name: Execute Script
command: python example.py
args:
chdir: /home/{{user_name}}/python_files/
答案2
尝试使用绝对路径并仔细检查目标路径是否存在于远程服务器中。
答案3
错误来自 shebang 中 Python 代码的第一行。存在剪切和粘贴错误。