使用ansible在远程服务器上执行python脚本错误

Arc*_*der 2 ansible ansible-inventory

我使用 ansible 2.8.3 Rhel 8 以 root@xxx12 身份登录。
我希望将一些文件复制到 root@xxx13 Rhel 8,然后执行 python 脚本。
我能够使用 ansible成功复制文件。我什至复制了密钥,现在它不再需要 ssh。

但在执行脚本期间:'致命:[web_node1]:失败!=> {"changed": false, "msg": "无法在 Ansible 控制器上找到或访问 '/root/ansible_copy/write_file.py'。\n如果您正在使用模块并期望该文件存在于远程,请参阅remote_src选项“}'
请注意,我是ansible的新手。
我猜有一些权限问题。
如果可能的话请帮助我。期待中的感谢

**yaml_file**
-
    name: Copy_all_ansible_files_to_servers
    hosts: copy_Servers
    become: true
    become_user: root
    tasks:
    -
      name: copy_to_all
      copy:
       src: /home/testuser/ansible_project/{{item}}
       dest: /root/ansible_copy/{{item}}
       owner: root
       group: root
       mode: u=rxw,g=rxw,o=rxw
      with_items:
         - write_file.py
         - sink.txt
         - ansible_playbook_task.yaml
         - copy_codes_2.yaml
      notify :
           - Run_date_command

    -
      name: Run_python_script
      script: /root/ansible_copy/write_file.py > /root/ansible_copy/sink.txt
      args:
        #chdir: '{{ role_path }}'
        executable: /usr/bin/python3.6
Run Code Online (Sandbox Code Playgroud)
    **inventory_file**
-
     web_node1 ansible_host=x.x.x.13
     [control]
     thisPc  ansible_connection=local
     #Groups
     [copy_Servers]
     web_node1
Run Code Online (Sandbox Code Playgroud)

命令:ansible-playbook copy_codes_2.yaml -i inventory.dat =>

    PLAY [Copy_all_ansible_files_to_servers] *******************************************************************************************************************************************************************

    TASK [Gathering Facts] *************************************************************************************************************************************************************************************
    ok: [web_node1]

    TASK [copy_to_all] *****************************************************************************************************************************************************************************************
    ok: [web_node1] => (item=write_file.py)
    ok: [web_node1] => (item=sink.txt)
    ok: [web_node1] => (item=ansible_playbook_task.yaml)
    ok: [web_node1] => (item=copy_codes_2.yaml)

    TASK [Run_python_script] ***********************************************************************************************************************************************************************************
    fatal: [web_node1]: FAILED! => {"changed": false, "msg": "Could not find or access '/root/ansible_copy/write_file.py' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}

    PLAY RECAP *************************************************************************************************************************************************************************************************
    web_node1                  : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
Run Code Online (Sandbox Code Playgroud)

gai*_*ige 6

script命令实际上会在运行之前将文件复制到远程服务器。因此,当它抱怨无法找到或访问脚本时,这是因为它正在尝试从/root/ansible_copy/write_file.py服务器复制。

如果您确实不需要脚本在执行后保留在服务器上,您可以从copy任务中删除该脚本并将script任务更改为src指向/home/testuser/ansible_project/write_file.py

或者,script您可以在传输脚本后手动运行脚本,而不是使用命令:

- name: run the write_file.py after it has already been transferred
  command: python3.6 /root/ansible_copy/write_file.py > /root/ansible_copy/sink.txt
Run Code Online (Sandbox Code Playgroud)

(注意:您可能需要提供 python3.6 可执行文件的完整路径)