Ansible - Activating virtual env with become_user

Viewed 7293

I want to do create a user for my CKAN installation and then activate a virtual environment as the user and install something.

- name: Add a CKAN user
  user:
    name: ckan
    comment: "CKAN User"
    shell: /sbin/nologin
    create_home: yes
    home: /usr/lib/ckan
    state: present 

- name: chmod 755 /usr/lib/ckan
  file:
    path: /usr/lib/ckan
    mode: u=rwX,g=rX,o=rX
    recurse: yes

- name: Create Python virtual env
  command: virtualenv --no-site-packages default 
  become: yes
  become_user: ckan

- name: Activate env
  command: . default/bin/activate

- name: Activate env
  command: pip install setuptools==36.1

I know it's generally not the most 'Ansible' implementation but I'm just trying to get something to work.

The error is in 'Create Python virtual env'. I am getting an error in that line for

In a command line I would just run: su -s /bin/bash - ckan

But how do I achieve this here? I thought become_user would do it?

2 Answers

If you already have the path to the user's folder and have set the appropriate permissions, then you can directly use the Ansible pip module to create a virtual environment in that folder and install packages. So, IIUC you do not require the following tasks

  • Create Python virtual env
    • instead of this task, you can just add the parameter virtualenv_command to the pip module in order to create the virtual environment (if it does not already exist)
  • Activate env (x2)
    • if you want to install packages into the virtual environment using the Ansible pip module, then these 2 tasks are not required

Also, you can use the parameter virtualenv_site_packages in order to exclude the global packages in your virtual environment. You do not need to use the parameter extra_args to do this.

If you want to install a single package into a virtual environment, then you can replace your last 3 tasks with the following task

tasks:
  - name: Create Python virtual env and install one package inside the virtual env
    pip:
      name: setuptools==36.1
      virtualenv: /path/to/ckan/user/home/folder # <--- path to user's home folder*
      virtualenv_command: virtualenv
      virtualenv_site_packages: no   # <---- added this parameter to exclude site packages
      virtualenv_python: python3.7

If you want to install many packages from requirements-docs.txt, then you could use this approach

tasks:
  - name: Create Python virtual env and install multiple packages inside the virtual env
    pip:
      requirements: /path/to/ckan/user/home/folder/requirements-docs.txt
      virtualenv: /path/to/ckan/user/home/folder # <--- path to user's home folder*
      virtualenv_command: virtualenv
      virtualenv_site_packages: no   # <---- added this parameter to exclude site packages
      virtualenv_python: python3.7

* the user's home folder must exist before executing this task

The following worked:

- name: Install setuptools into venv
  pip:
    name: Setuptools==36.1
    virtualenv: '{{ path_to_virtualenv }}'

Become user was not needed.

Another example:

- name:  Install ckan python modules
  pip: name="requirements-docs.txt"  virtualenv={{ ckan_virtualenv }} state=present extra_args="--ignore-installed -r"
Related