Search code examples
python-3.xpipansiblerasa

downlaod model python3 with ansible using virutal env


currecntly writing a ansible playbook to install rasa... using a virtual enviroment

python3.7 -m venv ./venv
source ./venv/bin/activate 
....
....
pip3 install -r requirements.txt
python3 -m spacy download en_core_web_md                                        
python -m spacy link en_core_web_md en  

some background information about spacy https://spacy.io/models..

in the playbook i was able to install the requirements using the following listing...playbook works perfectly....

- name: Download pip installer                                              
  get_url:                                                                  
    url: https://bootstrap.pypa.io/get-pip.py                               
    dest: /tmp/get-pip.py                                                   
                                                                               
- name: Install pip                                                         
  shell: |                                                                  
    /usr/bin/python3 /tmp/get-pip.py                                         
                                                                             
- name: Create app folder                                                   
  file:                                                                     
    name: /opt/app                                                
    state: directory                                                        
    recurse: yes                                                            
                                                                               
- name: Install virtualenv module                                           
  pip:                                                                      
    name: virtualenv                                                        
    state: latest                                                           
                                                                                
- name: Create virtualenv for app                                           
  pip:                                                                      
    requirements: /opt/app/requirements.txt                       
    virtualenv: /opt/app/appenv

what i miss are the last 2 parts executed in the /opt/app/appenv....

python3 -m spacy download en_core_web_md                                        
python -m spacy link en_core_web_md en 

Solution

  • You actually already know the path to the python executable in the virtualenv, as it's the virtualenv directory + bin/python; what you'll need to take care about is the idempotency of the action, to keep spacy from attempting to re-execute those commands on subsequent playbook runs

      - name: download en_core_web_md
        command: /opt/app/appenv/bin/python -m spacy download en_core_web_md                                        
        args:
          creates: /whatever/file/the download command/creates
    
      - name: link en_core_web_md
        command: /opt/app/appenv/bin/python -m spacy link en_core_web_md en
        args:
          creates: /whatever/file/link/creates
    

    I've omitted the extraction of that virtualenv directory to a vars: but I'd recommend it since it will likely be referenced in the creates: too