Ansible, Windows and PowerShell: the Basics – Part 13, Environment Variables

In Part 13 of this series we’ll continue our journey with Ansible, Windows and PowerShell and look at how to handle environment variables in Windows.

In this example we’ll look at a common scenario where you need to manually create the JAVA_HOME environment variable and add it to the existing PATH environment variable because the application install does not complete that for you. We will use a combination of the win_environment and win_path Ansible modules.

Our current environment variables are as below with no entries for JAVA_HOME:

Our job template in AWX is _12_environment_variables:

The contents of _12_environment_variables.yml are as follows:

Running a job from the _12_environment_variables job template produces the following result:

Our server now has both the JAVA_HOME environment variable and it has been added to PATH: