Windows – Using Ansible, how can I take actions on each file in a specific location

ansibleansible-playbookwindows

I'm building playbooks to deploy a Windows application that requires some prerequisites (Visual C++ runtimes and dotNet framework) to be installed before the application itself.
These prerequisites are .EXE files and are all contained in a specific folder.

I want to build an Ansible playbook that would (amongst other things) execute each of the .EXE files in the preriquisites folder with the parameters "/install /passive"

I don't want to have to specify each filename in my playbook.

My idea was to first find the prerequisites setup files using the "win_file" module, assign the result to a variable and them iterate over the variable and run the "win_command" module for each file it contains.

Unfortunately, I cannot assign the win_find result to a variable using the register statement, it simply doesn't do anything.

Here is what I tried:

- hosts: "{{ target }}"

  tasks:

  - name: Find files in path
    win_find:
      register: myvar
      paths: E:\Temp\Prognosis_11.2\Prerequisites

  - name: debug
    debug:
      msg: "{{ myvar }}"

And the playbook's output when ran with -v (verbose mode):

TASK [Find files in path] **********************************************************************************************************************************************************

ok: [SMN001] => {"changed": false, "examined": 5, "files": [{"attributes": "Archive", "checksum": "3049a85843eaf65e89e2336d5fe6e85e416797be", "creationtime": 1550942958.6690862, "extension": ".exe", "filename": "NDP46-KB3045557-x86-x64-AllOS-ENU.exe", "isarchive": true, "isdir": false, "ishidden": false, "islnk": false, "isreadonly": false, "isshared": false, "lastaccesstime": 1550942958.6690862, "lastwritetime": 1459427148, "owner": "BUILTIN\Administrators", "path": "E:\Temp\Prognosis_11.2\Prerequisites\NDP46-KB3045557-x86-x64-AllOS-ENU.exe", "size": 65444688}, {"attributes": "Archive", "checksum": "8bf41ba9eef02d30635a10433817dbb6886da5a2", "creationtime": 1550942958.7627323, "extension": ".exe", "filename": "vcredist2013_x64.exe", "isarchive": true, "isdir": false, "ishidden": false, "islnk": false, "isreadonly": false, "isshared": false, "lastaccesstime": 1550942958.7627323, "lastwritetime": 1489501866, "owner": "BUILTIN\Administrators", "path": "E:\Temp\Prognosis_11.2\Prerequisites\vcredist2013_x64.exe", "size": 7194312}, {"attributes": "Archive", "checksum": "df7f0a73bfa077e483e51bfb97f5e2eceedfb6a3", "creationtime": 1550942958.7779777, "extension": ".exe", "filename": "vcredist2013_x86.exe", "isarchive": true, "isdir": false, "ishidden": false, "islnk": false, "isreadonly": false, "isshared": false, "lastaccesstime": 1550942958.7779777, "lastwritetime": 1489501866, "owner": "BUILTIN\Administrators", "path": "E:\Temp\Prognosis_11.2\Prerequisites\vcredist2013_x86.exe", "size": 6503984}, {"attributes": "Archive", "checksum": "007064d974a55940838f19cd0b0e3aaf27ca06a7", "creationtime": 1550942958.7939014, "extension": ".exe", "filename": "vcredist2017_x64.exe", "isarchive": true, "isdir": false, "ishidden": false, "islnk": false, "isreadonly": false, "isshared": false, "lastaccesstime": 1550942958.7939014, "lastwritetime": 1488965662, "owner": "BUILTIN\Administrators", "path": "E:\Temp\Prognosis_11.2\Prerequisites\vcredist2017_x64.exe", "size": 15261400}, {"attributes": "Archive", "checksum": "ba1f7e7cace62f7c55ab948cd3b29acc4e8e2329", "creationtime": 1550942958.8406758, "extension": ".exe", "filename": "vcredist2017_x86.exe", "isarchive": true, "isdir": false, "ishidden": false, "islnk": false, "isreadonly": false, "isshared": false, "lastaccesstime": 1550942958.8406758, "lastwritetime": 1488965662, "owner": "BUILTIN\Administrators", "path": "E:\Temp\Prognosis_11.2\Prerequisites\vcredist2017_x86.exe", "size": 14401656}], "matched": 5}

TASK [debug] ***********************************************************************************************************************************************************************

fatal: [SMN001]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'myvar' is undefined\n\nThe error appears to have been in '/etc/ansible/playbooks/test_find_files.yaml': line 18, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n – name: debug\n ^ here\n"}

As you can see, the files are properly found, but the "myvar" variable doesn't exist even though it should have been assigned.

According to this page, this logic does seem to work on Linux hosts: http://www.mydailytutorials.com/using-ansible-find-module-search-filesfolder/ (ref "Storing the file names in a register" at the bottom of the page).

Could it be an issue with the win_find module itself?
Any thoughts?

Thanks!

Best Answer

It's just a typo, your register: is too far indented.

It should appear as:

  - name: Find files in path
    win_find:
      paths: E:\Temp\Prognosis_11.2\Prerequisites
    register: myvar