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
On Monday, February 1, 2021 at 5:46:25 PM UTC+10 amedee....@gmail.com wrote:

> 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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/51ff9103-4e6d-484c-aa9a-bddb55f7d56fn%40googlegroups.com.

Reply via email to