Ansible playbook上传并执行一个python脚本

我的手册如下:

- hosts : mygroup user : user sudo : yes tasks : - name : Copy script copy : 'src=/home/user/Scripts/logchecker.py dest=/opt/root2/logchecker.py owner=root group=root mode=755' - name : Execute script command : '/usr/bin/python /opt/root2/logchecker.py' 

file upload正在工作,但执行失败。 即使我能够在服务器上没有任何问题的情况下执行脚本。 我做错了什么?

我已经使用了一个类似于预期的剧本:

 # playbook.yml --- - hosts: ${target} sudo: yes tasks: - name: Copy file copy: src=../files/test.py dest=/opt/test.py owner=howardsandford group=admin mode=755 - name: Execute script command: /opt/test.py 

和test.py:

 #!/usr/bin/python # write to a file f = open('/tmp/test_from_python','w') f.write('hi there\n') 

运行Playboook:

 ansible-playbook playbook.yml --extra-vars "target=the_host_to_run_script_on" 

显示:

 PLAY [the_host_to_run_script_on] *************************************************************** GATHERING FACTS *************************************************************** ok: [the_host_to_run_script_on] TASK: [Copy file] ************************************************************* changed: [the_host_to_run_script_on] TASK: [Execute script] ******************************************************** changed: [the_host_to_run_script_on] PLAY RECAP ******************************************************************** the_host_to_run_script_on : ok=3 changed=2 unreachable=0 failed=0 

而在远程主机上:

 $ cat /tmp/test_from_python hi there 

我们的设置有几点区别:

  • 我没有复制和命令参数单引号
  • shebang设置python解释器而不是从命令行指定/ usr / bin / python
  • 我将脚本的所有者设置为我自己的用户名和主要组,而不是root

希望这可以指出你的差异可能在哪里的正确方向。

你只需要使用下面的插件脚本

 --- - hosts: ${target} become: true tasks: - name: Copy and Execute the script script: /opt/test.py