# Creating symbolic links in Windows with mklink always returns changed

**URL:** <https://forum.ansible.com/t/creating-symbolic-links-in-windows-with-mklink-always-returns-changed/34548>\
**Category:** Ansible Project\
**Tags:** windows, galaxy-ng\
**Created:** [February 1, 2021, 7:46am UTC](https://forum.ansible.com/t/creating-symbolic-links-in-windows-with-mklink-always-returns-changed/34548 "2021-02-01T07:46:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amedee\_Van\_Gasse](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@Amedee\_Van\_Gasse](https://forum.ansible.com/u/Amedee_Van_Gasse)\
**Post date:** [February 1, 2021, 7:46am UTC](https://forum.ansible.com/t/creating-symbolic-links-in-windows-with-mklink-always-returns-changed/34548/1 "2021-02-01T07:46:25Z")

</div>

I’m using this to create a symbolic link to a directory, on Windows:

- name: Find OpenJDK directory  
ansible.windows.win\_find:  
paths: C:\Program Files\OpenJDK  
patterns: openjdk\*  
file\_type: directory  
recurse: no  
register: OpenJDK

- name: Create link to Java  
win\_command: cmd.exe /k mklink jdk “{{ OpenJDK.files[0].path }}”  
args:  
chdir: “C:\Users\{{ username }}”  
vars:  
ansible\_become\_user: “{{ username }}”  
ansible\_become\_password: “{{ password }}”

win\_command always returns “changed”, even if the symlink already existed and was pointing to the same directory.

How do I change this so that I get “ok” when there was no change?  
OR  
Does a default Ansible command already exist for mklink? I couldn’t find any on Ansible Galaxy.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [February 1, 2021, 10:26pm UTC](https://forum.ansible.com/t/creating-symbolic-links-in-windows-with-mklink-always-returns-changed/34548/2 "2021-02-01T22:26:35Z")

</div>

The reason why it always reports a change is because win\_command/win\_shell always reports a change, it doesn’t know what command it is running and whether it is making a change or not.

You have 2 ways to solve this:

- Add the creates arg to win\_command that tells it to run the command only if the path does not exist
  - If it does exist then the command is not run and it doesn’t report a change

- Use changed\_when to control whether it reports a change based on the module return code
  - This can be done if cmd.exe returns a different rc if the link already existed or not  
In this case I think the first option is a better route for you.

Does a default Ansible command already exist for mklink? I couldn’t find any on Ansible Galaxy.

I’ve been wanting to add support for this to win\_file for a while but there are quite a few hurdles and complexities that need to be overcome because this is really viable. In the end the lack of symlink use in Windows just never really demanded this to be done compared to Linux. Maybe at some point in the future there will be but for now you need to use mklink or if you have PowerShell 5.1 you can use New-Item with win\_shell.

Thanks

Jordan
