title: How to set environmental variables using Ansible
body: <p>I need to set the variables like <code>JAVA_HOME</code> and update <code>PATH</code>.  There are a number of ways of doing this. One way is to update the <code>/etc/environment</code> variable and include a line for <code>JAVA_HOME</code> using the <a href="http://docs.ansible.com/ansible/lineinfile_module.html">lineinfile</a> module and then run the command source <code>/etc/environment</code> directly on the guest OS (CentOS in my case).</p>
  
  <p>Another way is to execute the export command e.g.</p>
  
  <pre><code>export JAVA_HOME=/usr/java/jre1.8.0_51
  export PATH=$PATH:$JAVA_HOME
  </code></pre>
  
  <p>Is there a cleaner way to do this as all these require manipulating files and running commands directly on the OS to update the environment variables?</p>