unai abrisketa sanchez
unai abrisketa sanchez

Reputation: 386

Run a python script using ansible

I am getting troubles to execute a python script using ansible.

I have the next ansible structure:

python_project
  roles
    prueba
      files
        prueba.py
      tasks
        prueba.yaml

My yaml is the following one:

- name: prueba script python playbook
  hosts: localhost
  tasks:
    - name: run prueba.py
      script: prueba.py

I execute it like this: ansible-playbook prueba.yaml and I get the following error:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Could not find or
access 'prueba.py'\n
Searched in:\n\t/root/python_project/roles/prueba/tasks/files/pr                                                                             ueba.py\n\t/root/python_project/roles/prueba/tasks/prueba.py\n\t/root/python_pro                                                                 ject/roles/prueba/tasks/files/prueba.py\n\t/root/python_project/roles/prueba/tas                                                                             ks/prueba.py on the Ansible Controller.\nIf you are using a module and expect th                                                                             e file to exist on the remote, see the remote_src option"}

Any suggestion?

Upvotes: 0

Views: 4409

Answers (1)

ikora
ikora

Reputation: 972

I think you have an issue in the definition in your role. Is that a role and you want it to work as an ansible role? If thats the case you should change a few things. You should have a separate playbook to invoque this role and move this role to your role folder. If thats not a role. Indeed you have to specify the path to the python script:

    - name: prueba script python playbook
      hosts: localhost
      tasks:
        - name: run prueba.py
          script: /root/python_project/roles/prueba/files/prueba.py

I think you want to work with roles. To make it as a role you need to change few things. tasks/main.yml (This is your task directory you can create the yml with the name you want not necesary main.yml)

- name: run prueba.py
  script: prueba.py

As you can see on a role, the tasks/main.yml dont need to have the hosts, neither the tasks section the file itself its a task section. The logic of the hosts will be moved to the play that invoque the role.

Create this file on any path you want: example_role_invoque.yml

  - hosts: localhost
    tasks:
      - name: invocar rol
        include_role: prueba

I let you a reference link: ROLES

Upvotes: 2

Related Questions