Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible creating virtualenv with root owner

This Ansible task creates a virtualenv (good!), but the directory (/home/chris/.virtualenvs/foobar) is owned by root (not so good):

- name: install requirements
  pip:
    chdir: /home/chris/website
    requirements: ./requirements.txt
    virtualenv: /home/chris/.virtualenvs/foobar

But what's driving me nuts is that the next task fails, apparently because of the root ownership

- name: copy sitecustomize.py
  file:
    src: /home/chris/website/sitecustomize.py
    dest: /home/chris/.virtualenvs/foobar/lib/python2.7/sitecustomize.py
    remote_src: yes

I think the problem I really want to solve is creating the .virtualenv so that it's owned by "chris". Any idea how to force that?

Failing that, how can get the "copy" task to run with the same permissions as the "pip" task, so that I can copy my file?

EDIT: Solved second problem -- I needed to use the "copy" task, not the file task. So this...

- name: copy sitecustomize.py
  copy:
    src: /home/chris/website/sitecustomize.py
    dest: /home/chris/.virtualenvs/foobar/lib/python2.7/sitecustomize.py
    remote_src: yes
like image 880
Chris Curvey Avatar asked Sep 14 '25 16:09

Chris Curvey


1 Answers

Try to run pip as chris:

- name: install requirements
  pip:
    chdir: /home/chris/website
    requirements: ./requirements.txt
    virtualenv: /home/chris/.virtualenvs/foobar
  become: yes
  become_user: chris

And to be consistent, make chris the owner of sitecustomize.py:

- name: copy sitecustomize.py
  file:
    src: /home/chris/website/sitecustomize.py
    dest: /home/chris/.virtualenvs/foobar/lib/python2.7/sitecustomize.py
    remote_src: yes
    group: chris
    owner: chris
like image 182
Eric Citaire Avatar answered Sep 16 '25 07:09

Eric Citaire