Search code examples
ansibleckan

Ansible - Activating virtual env with become_user


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?


Solution

  • 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"