Question:
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.
1 2 3 4 |
export WORKON_HOME=~/TestEnvs source /usr/local/bin/virtualenvwrapper.sh workon my_virtual_env |
Is there any way to accomplish this using Ansible?
Answer:
- Use Ansible
blockinfile
module to maintain the lines in the.bashrc
or/etc/bashrc
:
1234567891011- name: Ensure virtualenv is sourced from the .bashrcblockinfile:dest: "{{ ansible_env.HOME }}/.bashrc"block: |export WORKON_HOME=~/TestEnvssource /usr/local/bin/virtualenvwrapper.shworkon my_virtual_envmarker: '# {mark} ANSIBLE MANAGED BLOCK - virtualenv'insertbefore: BOFcreate: yes - Or better: create a
.bashrc.d
(or.bash_profile.d
) directory, replace your.bashrc
with a call to source all files in the directory:
12345while read filenamedosource "$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
andcopy
modules in Ansible.