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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












3















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:TempPrognosis_11.2Prerequisites

- 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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2PrerequisitesNDP46-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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2013_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2013_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2017_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2017_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 undefinednnThe error appears to have been in '/etc/ansible/playbooks/test_find_files.yaml': line 18, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: debugn ^ heren"




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!










share|improve this question






















  • I forgot to mention I'm using Ansible 2.7.7

    – GuillaumeN
    Feb 24 at 0:24















3















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:TempPrognosis_11.2Prerequisites

- 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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2PrerequisitesNDP46-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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2013_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2013_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2017_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2017_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 undefinednnThe error appears to have been in '/etc/ansible/playbooks/test_find_files.yaml': line 18, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: debugn ^ heren"




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!










share|improve this question






















  • I forgot to mention I'm using Ansible 2.7.7

    – GuillaumeN
    Feb 24 at 0:24













3












3








3








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:TempPrognosis_11.2Prerequisites

- 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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2PrerequisitesNDP46-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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2013_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2013_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2017_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2017_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 undefinednnThe error appears to have been in '/etc/ansible/playbooks/test_find_files.yaml': line 18, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: debugn ^ heren"




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!










share|improve this question














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:TempPrognosis_11.2Prerequisites

- 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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2PrerequisitesNDP46-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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2013_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2013_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2017_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": "BUILTINAdministrators", "path": "E:TempPrognosis_11.2Prerequisitesvcredist2017_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 undefinednnThe error appears to have been in '/etc/ansible/playbooks/test_find_files.yaml': line 18, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: debugn ^ heren"




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!







windows ansible ansible-playbook






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 24 at 0:19









GuillaumeNGuillaumeN

185




185












  • I forgot to mention I'm using Ansible 2.7.7

    – GuillaumeN
    Feb 24 at 0:24

















  • I forgot to mention I'm using Ansible 2.7.7

    – GuillaumeN
    Feb 24 at 0:24
















I forgot to mention I'm using Ansible 2.7.7

– GuillaumeN
Feb 24 at 0:24





I forgot to mention I'm using Ansible 2.7.7

– GuillaumeN
Feb 24 at 0:24










1 Answer
1






active

oldest

votes


















7














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



It should appear as:



 - name: Find files in path
win_find:
paths: E:TempPrognosis_11.2Prerequisites
register: myvar





share|improve this answer























  • Argh, I just saw that too, gave it a try and it worked like a charm! Thank you @MichaelHampton :)

    – GuillaumeN
    Feb 24 at 0:30











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "2"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f955466%2fusing-ansible-how-can-i-take-actions-on-each-file-in-a-specific-location%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














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



It should appear as:



 - name: Find files in path
win_find:
paths: E:TempPrognosis_11.2Prerequisites
register: myvar





share|improve this answer























  • Argh, I just saw that too, gave it a try and it worked like a charm! Thank you @MichaelHampton :)

    – GuillaumeN
    Feb 24 at 0:30
















7














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



It should appear as:



 - name: Find files in path
win_find:
paths: E:TempPrognosis_11.2Prerequisites
register: myvar





share|improve this answer























  • Argh, I just saw that too, gave it a try and it worked like a charm! Thank you @MichaelHampton :)

    – GuillaumeN
    Feb 24 at 0:30














7












7








7







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



It should appear as:



 - name: Find files in path
win_find:
paths: E:TempPrognosis_11.2Prerequisites
register: myvar





share|improve this 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:TempPrognosis_11.2Prerequisites
register: myvar






share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 24 at 0:25









Michael HamptonMichael Hampton

172k27316642




172k27316642












  • Argh, I just saw that too, gave it a try and it worked like a charm! Thank you @MichaelHampton :)

    – GuillaumeN
    Feb 24 at 0:30


















  • Argh, I just saw that too, gave it a try and it worked like a charm! Thank you @MichaelHampton :)

    – GuillaumeN
    Feb 24 at 0:30

















Argh, I just saw that too, gave it a try and it worked like a charm! Thank you @MichaelHampton :)

– GuillaumeN
Feb 24 at 0:30






Argh, I just saw that too, gave it a try and it worked like a charm! Thank you @MichaelHampton :)

– GuillaumeN
Feb 24 at 0:30


















draft saved

draft discarded
















































Thanks for contributing an answer to Server Fault!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f955466%2fusing-ansible-how-can-i-take-actions-on-each-file-in-a-specific-location%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)