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

Executing python script on remote server using ansible Error

我以 root@x.x.x.12 身份登录,使用 ansible 2.8.3 Rhel 8.
我想复制几个文件到 root@x.x.x.13 Rhel 8 然后执行一个 python 脚本。
我能够使用 ansible 成功 复制文件。我什至复制了密钥,现在它是无 ssh 的。

但是在脚本执行期间: '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"}'
请注意,我是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

    **inventory_file**
-
     web_node1 ansible_host=x.x.x.13
     [control]
     thisPc  ansible_connection=local
     #Groups
     [copy_Servers]
     web_node1

命令: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

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

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