title: changing user and setting env variable
body: <p>Using ansible im trying to 
  1. sudo into a user id - hduser 
  2. set some env variables 
  3. source the file.</p>
  
  <p>Later, I print the env variables but don't see the env variables being set for the user hduser</p>
  
  <pre><code>- name: change to hduser
    shell: sudo su - hduser
  
  - name: Setting 
    lineinfile: 
      dest: /home/hduser/.bashrc 
      line: 'export HADOOP_HOME=/usr/local/hadoop' 
      insertafter: 'EOF' 
      regexp: 'export HADOOP_HOME=/usr/local/hadoop' 
      state: present
  
  - name: Adding the path in the bashrc files
    lineinfile: 
      dest: /home/hduser/.bashrc 
      line: 'export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64' 
      insertafter: 'EOF' 
      regexp: 'export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64' 
      state: present
  
  - name: Adding the path in the bashrc files
    lineinfile: 
      dest: /home/hduser/.bashrc 
      line: 'export PATH=$PATH:$HADOOP_HOME/bin' 
      insertafter: 'EOF' 
      regexp: 'export PATH=\$PATH:$HADOOP_HOME/bin' 
      state: present
  
  - name: Source the bashrc file
    shell: source /home/hduser/.bashrc 
    register: sourcevar
    args:
      executable: /bin/bash
  
  - debug:
      var: sourcevar
  - name: print env variables
    shell: sudo su hduser &amp;&amp; env
    register: envvar
    args:
      executable: /bin/bash
  
  - debug: 
      var: envvar
  </code></pre>
  
  <p>These are my env variables it does not list the ones I sourced.</p>
  
  <pre><code>ok: [default] =&gt; {
      "envvar": {
          "changed": true, 
          "cmd": "sudo su hduser &amp;&amp; env", 
          "delta": "0:00:00.013310", 
          "end": "2017-09-15 17:37:01.913357", 
          "rc": 0, 
          "start": "2017-09-15 17:37:01.900047", 
          "stderr": "", 
          "stderr_lines": [], 
          "stdout": "TERM=xterm-256color\nSHELL=/bin/bash\nUSER=root\nSUDO_USER=vagrant\nSUDO_UID=1000\nUSERNAME=root\nMAIL=/var/mail/root\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\nPWD=/home/vagrant\nLANG=en_US.UTF-8\nSHLVL=1\nHOME=/root\nSUDO_COMMAND=/bin/sh -c echo BECOME-SUCCESS-nzuyqifbpqxknsapqpiflhxbmsrzdaqo; /usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1505497021.78-205792997462616/command.py; rm -rf \"/home/vagrant/.ansible/tmp/ansible-tmp-1505497021.78-205792997462616/\" &gt; /dev/null 2&gt;&amp;1\nLOGNAME=root\nSUDO_GID=1000\n_=/usr/bin/env", 
          "stdout_lines": [
              "TERM=xterm-256color", 
              "SHELL=/bin/bash", 
              "USER=root", 
              "SUDO_USER=vagrant", 
              "SUDO_UID=1000", 
              "USERNAME=root", 
              "MAIL=/var/mail/root", 
              "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 
              "PWD=/home/vagrant", 
              "LANG=en_US.UTF-8", 
              "SHLVL=1", 
              "HOME=/root", 
              "SUDO_COMMAND=/bin/sh -c echo BECOME-SUCCESS-nzuyqifbpqxknsapqpiflhxbmsrzdaqo; /usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1505497021.78-205792997462616/command.py; rm -rf \"/home/vagrant/.ansible/tmp/ansible-tmp-1505497021.78-205792997462616/\" &gt; /dev/null 2&gt;&amp;1", 
              "LOGNAME=root", 
              "SUDO_GID=1000", 
              "_=/usr/bin/env"
          ], 
          "warnings": [
              "Consider using 'become', 'become_method', and 'become_user' rather than running sudo"
          ]
      }
  }
  </code></pre>