Search code examples
ansibleansible-inventory

Executing python script on remote server using ansible Error


I am logged in as root@x.x.x.12 with ansible 2.8.3 Rhel 8.
I wish to copy few files to root@x.x.x.13 Rhel 8 and then execute a python script.
I am able to copy the files sucessfully using ansible. I had even copied the keys and now it is ssh-less.

But during execution of 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"}'
Please note that I am a novice to ansible.
I guess there is some permission issues.
Please Help me out if possible. Thanking in anticipation

**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

Command: 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

Solution

  • The script command will actually copy the file to the remote server before running it. Thus, when it complains about not being able to find or access the script, it's because it's trying to copy from /root/ansible_copy/write_file.py to the server.

    If you don't really need the script to remain on the server after you execute it, you could remove the script from the copy task and change the script task to have the src point at /home/testuser/ansible_project/write_file.py.

    Alternatively, instead of using the script command, you can manually run the script after transferring it using:

    - 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
    

    (Note: you may need to provide the full path to your python3.6 executable)