Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update bashrc with virtualenv info using Ansible

I have a python virtualenv running on a remote server. I am trying to update the bashrc of the remote server with the following info using Ansible.

export WORKON_HOME=~/TestEnvs
source /usr/local/bin/virtualenvwrapper.sh
workon my_virtual_env

Is there any way to accomplish this using Ansible?

like image 563
Pattu Avatar asked Oct 29 '16 06:10

Pattu


1 Answers

  1. Use Ansible blockinfile module to maintain the lines in the .bashrc or /etc/bashrc:

    - name: Ensure virtualenv is sourced from the .bashrc
      blockinfile:
        dest: "{{ ansible_env.HOME }}/.bashrc"
        block: |
          export WORKON_HOME=~/TestEnvs
          source /usr/local/bin/virtualenvwrapper.sh
          workon my_virtual_env
        marker: '# {mark} ANSIBLE MANAGED BLOCK - virtualenv'
        insertbefore: BOF
        create: yes 
    
  2. Or better: create a .bashrc.d (or .bash_profile.d) directory, replace your .bashrc with a call to source all files in the directory:

    while read filename
    do
      source "$filename"
    done < <(find -L ~/.bashrc.d -type f)
    

    and add the above commands as a separate file. Move other commands from the current .bashrc to another file and place it in .bashrc.d directory.

    This you can easily achieve with file and copy modules in Ansible.

like image 192
techraf Avatar answered Nov 13 '22 00:11

techraf