--- - name: remove links if they exist as win_file struggles win_command: cmd.exe /c rmdir "{{item}}" ignore_errors: True with_items: - "{{win_find_dir}}\\nested\\link" - "{{win_find_dir}}\\broken-link" - "{{win_find_dir}}\\hard-link-dest\\hard-link.log" - "{{win_find_dir}}\\junction-link" - name: ensure the testing directory is cleared before setting up test win_file: path: "{{win_find_dir}}" state: absent - name: ensure testing directories exist win_file: path: "{{item}}" state: directory with_items: - "{{win_find_dir}}\\nested" - "{{win_find_dir}}\\single" - "{{win_find_dir}}\\link-dest" - "{{win_find_dir}}\\link-dest\\sub-link" - "{{win_find_dir}}\\hard-link-dest" - "{{win_find_dir}}\\junction-link-dest" - "{{win_find_dir}}\\broken-link-dest" - "{{win_find_dir}}\\nested\\sub-nest" - "{{win_find_dir}}\\shared" - "{{win_find_dir}}\\shared\\folder" - "{{win_find_dir}}\\hidden" - "{{win_find_dir}}\\date" - name: create empty test files win_file: path: "{{item}}" state: touch with_items: - "{{win_find_dir}}\\nested\\file.ps1" - "{{win_find_dir}}\\nested\\test.ps1" - "{{win_find_dir}}\\nested\\out.log" - "{{win_find_dir}}\\nested\\archive.log" - "{{win_find_dir}}\\nested\\sub-nest\\test.ps1" - "{{win_find_dir}}\\nested\\sub-nest\\readonly.txt" - "{{win_find_dir}}\\link-dest\\link.ps1" - "{{win_find_dir}}\\single\\large.ps1" - "{{win_find_dir}}\\single\\small.ps1" - "{{win_find_dir}}\\single\\test.ps1" - "{{win_find_dir}}\\single\\hidden.ps1" - "{{win_find_dir}}\\date\\new.ps1" - "{{win_find_dir}}\\date\\old.ps1" - "{{win_find_dir}}\\single\\out_20161101-091005.log" - "{{win_find_dir}}\\hidden\\out_20161101-091005.log" - "{{win_find_dir}}\\hard-link-dest\\file-abc.log" - name: populate files with a test string win_lineinfile: dest: "{{item.path}}" line: "{{item.text}}" with_items: - { 'path': "{{win_find_dir}}\\nested\\file.ps1", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\nested\\test.ps1", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\nested\\out.log", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\nested\\archive.log", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\nested\\sub-nest\\test.ps1", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\nested\\sub-nest\\readonly.txt", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\link-dest\\link.ps1", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\single\\test.ps1", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\single\\hidden.ps1", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\date\\new.ps1", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\date\\old.ps1", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\single\\out_20161101-091005.log", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\hidden\\out_20161101-091005.log", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\hard-link-dest\\file-abc.log", 'text': 'abcdefg1234567' } - { 'path': "{{win_find_dir}}\\single\\small.ps1", 'text': "a" } - { 'path': "{{win_find_dir}}\\date\\new.ps1", 'text': "random text for new date" } - { 'path': "{{win_find_dir}}\\date\\old.ps1", 'text': "random text for old date" } - name: populate large text file win_command: powershell "Set-Content {{win_find_dir}}\\single\\large.ps1 ('abcdefghijklmnopqrstuvwxyz' * 10000)" - name: create share script: set_share.ps1 "{{win_find_dir}}\shared\folder" "folder-share" - name: create links win_command: cmd.exe /c mklink /{{item.type}} "{{item.source}}" "{{item.target}}" with_items: - { type: 'D', source: "{{win_find_dir}}\\nested\\link", target: "{{win_find_dir}}\\link-dest" } - { type: 'D', source: "{{win_find_dir}}\\broken-link", target: "{{win_find_dir}}\\broken-link-dest" } - { type: 'H', source: "{{win_find_dir}}\\hard-link-dest\\hard-link.log", target: "{{win_find_dir}}\\hard-link-dest\\file-abc.log" } - { type: 'J', source: "{{win_find_dir}}\\junction-link", target: "{{win_find_dir}}\\junction-link-dest" } - name: set modification date on files/folders script: set_filedate.ps1 "{{item}}" with_items: - "{{win_find_dir}}\\nested\\file.ps1" - "{{win_find_dir}}\\nested\\test.ps1" - "{{win_find_dir}}\\nested\\out.log" - "{{win_find_dir}}\\nested\\archive.log" - "{{win_find_dir}}\\nested\\sub-nest\\test.ps1" - "{{win_find_dir}}\\nested\\sub-nest\\readonly.txt" - "{{win_find_dir}}\\link-dest\\link.ps1" - "{{win_find_dir}}\\single\\large.ps1" - "{{win_find_dir}}\\single\\small.ps1" - "{{win_find_dir}}\\single\\test.ps1" - "{{win_find_dir}}\\single\\hidden.ps1" - "{{win_find_dir}}\\date\\old.ps1" - "{{win_find_dir}}\\single\\out_20161101-091005.log" - "{{win_find_dir}}\\hidden\\out_20161101-091005.log" - "{{win_find_dir}}\\hard-link-dest\\file-abc.log" - "{{win_find_dir}}\\nested" - "{{win_find_dir}}\\single" - "{{win_find_dir}}\\link-dest" - "{{win_find_dir}}\\link-dest\\sub-link" - "{{win_find_dir}}\\hard-link-dest" - "{{win_find_dir}}\\junction-link-dest" - "{{win_find_dir}}\\broken-link-dest" - "{{win_find_dir}}\\nested\\sub-nest" - "{{win_find_dir}}\\shared" - "{{win_find_dir}}\\shared\\folder" - "{{win_find_dir}}\\hidden" - "{{win_find_dir}}\\date" - name: set file attributes for test script: set_attributes.ps1 "{{item.path}}" {{item.attr}} with_items: - { 'path': "{{win_find_dir}}\\hidden", 'attr': "Hidden" } - { 'path': "{{win_find_dir}}\\date", 'attr': "Hidden" } - { 'path': "{{win_find_dir}}\\nested\\archive.log", 'attr': "Archive" } - { 'path': "{{win_find_dir}}\\nested\\sub-nest\\readonly.txt", 'attr': "ReadOnly" } - { 'path': "{{win_find_dir}}\\single\\hidden.ps1", 'attr': "Hidden" } - name: break the broken link target win_file: path: "{{win_find_dir}}\\broken-link-dest" state: absent # end test setup - name: expect failure when not setting paths win_find: patterns: a register: actual failed_when: "actual.msg != 'Missing required argument: paths'" - name: expect failure when setting paths to a file win_find: paths: "{{win_output_dir}}\\win_find\\single\\large.ps1" register: actual failed_when: "actual.msg != 'Argument path {{win_output_dir|regex_replace('\\\\', '\\\\\\\\')}}\\win_find\\single\\large.ps1 is a file not a directory'" - name: expect failure whe path is set to a non existant folder win_find: paths: "{{win_output_dir}}\\win_find\\thisisafakefolder" register: actual failed_when: "actual.msg != 'Argument path {{win_output_dir|regex_replace('\\\\', '\\\\\\\\')}}\\\\win_find\\\\thisisafakefolder does not exist cannot get information on'" - name: get files in single directory win_find: paths: "{{win_output_dir}}\\win_find\\single" register: actual - name: set expected value for files in a single directory set_fact: expected: changed: False examined: 5 files: - { isarchive: True, attributes: Archive, checksum: f8d100cdcf0e6c1007db2f8dd0b7ee2884df89af, creationtime: 1477984205, extension: .ps1, filename: large.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\single\\large.ps1", isreadonly: False, isshared: False, size: 260002 } - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .log, filename: out_20161101-091005.log, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\single\\out_20161101-091005.log", isreadonly: False, isshared: False, size: 14 } - { isarchive: True, attributes: Archive, checksum: 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8, creationtime: 1477984205, extension: .ps1, filename: small.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\single\\small.ps1", isreadonly: False, isshared: False, size: 1 } - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .ps1, filename: test.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\single\\test.ps1", isreadonly: False, isshared: False, size: 14 } matched: 4 - name: assert actual == expected assert: that: "actual == expected" - name: find hidden files win_find: paths: ['{{win_find_dir}}\\single', '{{win_find_dir}}\\nested'] hidden: True register: actual - name: set fact for hidden files set_fact: expected: changed: False examined: 11 files: - { isarchive: True, attributes: "Hidden, Archive", checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .ps1, filename: hidden.ps1, ishidden: True, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\single\\hidden.ps1", isreadonly: False, isshared: False, size: 14 } matched: 1 - name: assert actual == expected assert: that: "actual == expected" - name: find file based on pattern win_find: paths: '{{win_find_dir}}\\single' patterns: ['*.log', 'out_*'] register: actual_pattern - name: find file based on pattern regex win_find: paths: '{{win_find_dir}}\\single' patterns: "out_\\d{8}-\\d{6}.log" use_regex: True register: actual_regex - name: set fact for pattern files set_fact: expected: changed: False examined: 5 files: - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .log, filename: out_20161101-091005.log, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\single\\out_20161101-091005.log", isreadonly: False, isshared: False, size: 14 } matched: 1 - name: assert actual == expected assert: that: - "actual_pattern == expected" - "actual_regex == expected" - name: find files with recurse set win_find: paths: "{{win_find_dir}}\\nested" recurse: True patterns: "*.ps1" register: actual - name: set expected value for files in a nested directory set_fact: expected: changed: False examined: 8 files: - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .ps1, filename: test.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\nested\\sub-nest\\test.ps1", isreadonly: False, isshared: False, size: 14 } - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .ps1, filename: file.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\nested\\file.ps1", isreadonly: False, isshared: False, size: 14 } - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .ps1, filename: test.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\nested\\test.ps1", isreadonly: False, isshared: False, size: 14 } matched: 3 - name: assert actual == expected assert: that: "actual == expected" - name: find files with recurse set and follow links win_find: paths: "{{win_find_dir}}\\nested" recurse: True follow: True patterns: "*.ps1" register: actual - name: set expected value for files in a nested directory while following links set_fact: expected: changed: False examined: 10 files: - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .ps1, filename: link.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\nested\\link\\link.ps1", isreadonly: False, isshared: False, size: 14 } - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .ps1, filename: test.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\nested\\sub-nest\\test.ps1", isreadonly: False, isshared: False, size: 14 } - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .ps1, filename: file.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\nested\\file.ps1", isreadonly: False, isshared: False, size: 14 } - { isarchive: True, attributes: Archive, checksum: 8df33cee3325596517df5bb5aa980cf9c5c1fda3, creationtime: 1477984205, extension: .ps1, filename: test.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\nested\\test.ps1", isreadonly: False, isshared: False, size: 14 } matched: 4 - name: assert actual == expected assert: that: "actual == expected" - name: find directories win_find: paths: "{{win_find_dir}}\\link-dest" file_type: directory register: actual - name: set expected fact for directories with recurse and follow set_fact: expected: changed: False examined: 2 files: - { isarchive: False, attributes: Directory, creationtime: 1477984205, filename: sub-link, ishidden: False, isdir: True, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\link-dest\\sub-link", isreadonly: False, isshared: False, size: 0 } matched: 1 - name: assert actual == expected assert: that: "actual == expected" - name: find directories recurse and follow with a broken link win_find: paths: "{{win_find_dir}}" file_type: directory recurse: True follow: True register: actual - name: check directory count with recurse and follow is correct assert: that: - "actual.examined == 33" - "actual.matched == 13" - "actual.files[0].filename == 'broken-link'" - "actual.files[0].islink == True" - "actual.files[2].filename == 'junction-link'" - "actual.files[2].islink == True" - "actual.files[2].lnk_source == '{{win_find_dir|regex_replace('\\\\', '\\\\\\\\')}}\\\\junction-link-dest'" - "actual.files[7].filename == 'link'" - "actual.files[7].islink == True" - "actual.files[7].lnk_source == '{{win_find_dir|regex_replace('\\\\', '\\\\\\\\')}}\\\\link-dest'" - "actual.files[11].filename == 'folder'" - "actual.files[11].islink == False" - "actual.files[11].isshared == True" - "actual.files[11].sharename == 'folder-share'" - name: filter files by size without byte specified win_find: paths: "{{win_find_dir}}\\single" size: 260002 register: actual_without_byte - name: filter files by size with byte specified win_find: paths: "{{win_find_dir}}\\single" size: 253k register: actual_with_byte - name: set expected fact for files by size set_fact: expected: changed: False examined: 5 files: - { isarchive: True, attributes: Archive, checksum: f8d100cdcf0e6c1007db2f8dd0b7ee2884df89af, creationtime: 1477984205, extension: ".ps1", filename: large.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\single\\large.ps1", isreadonly: False, isshared: False, size: 260002 } matched: 1 - name: assert actual == expected assert: that: - "actual_without_byte == expected" - "actual_with_byte == expected" - name: filter files by size (less than) without byte specified win_find: paths: "{{win_find_dir}}\\single" size: -4 register: actual_without_byte - name: filter files by size (less than) with byte specified win_find: paths: "{{win_find_dir}}\\single" size: -4b register: actual_with_byte - name: set expected fact for files by size (less than) set_fact: expected: changed: False examined: 5 files: - { isarchive: True, attributes: Archive, checksum: 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8, creationtime: 1477984205, extension: ".ps1", filename: small.ps1, ishidden: False, isdir: False, islink: False, lastaccesstime: 1477984205, lastwritetime: 1477984205, owner: BUILTIN\Administrators, path: "{{win_find_dir}}\\single\\small.ps1", isreadonly: False, isshared: False, size: 1 } matched: 1 - name: assert actual == expected assert: that: - "actual_without_byte == expected" - "actual_with_byte == expected" # For dates we cannot assert against expected as the times change, this is a poor mans attempt at testing - name: filter files by age without unit specified win_find: paths: "{{win_find_dir}}\\date" age: 3600 register: actual_without_unit - name: filter files by age with unit specified win_find: paths: "{{win_find_dir}}\\date" age: 1h register: actual_with_unit - name: assert dates match each other assert: that: - "actual_without_unit == actual_with_unit" - "actual_without_unit.matched == 1" - "actual_without_unit.files[0].checksum == '7454f04e3ac587f711a416f4edf26507255e0a2e'" - "actual_without_unit.files[0].path == '{{win_find_dir|regex_replace('\\\\', '\\\\\\\\')}}\\\\date\\\\new.ps1'" - name: filter files by age (older than) without unit specified win_find: paths: "{{win_find_dir}}\\date" age: -1 register: actual_without_unit - name: filter files by age (older than) without unit specified win_find: paths: "{{win_find_dir}}\\date" age: -1s register: actual_with_unit - name: assert dates match each other assert: that: - "actual_without_unit == actual_with_unit" - "actual_without_unit.matched == 2" - "actual_without_unit.files[0].checksum == '7454f04e3ac587f711a416f4edf26507255e0a2e'" - "actual_without_unit.files[0].path == '{{win_find_dir|regex_replace('\\\\', '\\\\\\\\')}}\\\\date\\\\new.ps1'" - "actual_without_unit.files[1].checksum == '031a04ecc76f794d7842651de732075dec6fef04'" - "actual_without_unit.files[1].path == '{{win_find_dir|regex_replace('\\\\', '\\\\\\\\')}}\\\\date\\\\old.ps1'" - name: get list of files with md5 checksum win_find: paths: "{{win_find_dir}}\\single" patterns: test.ps1 checksum_algorithm: md5 register: actual_md5_checksum - name: assert md5 checksum value assert: that: - "actual_md5_checksum.files[0].checksum == 'd1713d0f1d2e8fae230328d8fd59de01'" - name: get list of files with sha1 checksum win_find: paths: "{{win_find_dir}}\\single" patterns: test.ps1 checksum_algorithm: sha1 register: actual_sha1_checksum - name: assert sha1 checksum value assert: that: - "actual_sha1_checksum.files[0].checksum == '8df33cee3325596517df5bb5aa980cf9c5c1fda3'" - name: get list of files with sha256 checksum win_find: paths: "{{win_find_dir}}\\single" patterns: test.ps1 checksum_algorithm: sha256 register: actual_sha256_checksum - name: assert sha256 checksum value assert: that: - "actual_sha256_checksum.files[0].checksum == 'c20d2eba7ffda0079812721b6f4e4e109e2f0c5e8cc3d1273a060df6f7d9f339'" - name: get list of files with sha384 checksum win_find: paths: "{{win_find_dir}}\\single" patterns: test.ps1 checksum_algorithm: sha384 register: actual_sha384_checksum - name: assert sha384 checksum value assert: that: - "actual_sha384_checksum.files[0].checksum == 'aed515eb216b9c7009ae8c4680f46c1e22004528b231aa0482a8587543bca47d3504e9f77e884eb2d11b2f9f5dc01651'" - name: get list of files with sha512 checksum win_find: paths: "{{win_find_dir}}\\single" patterns: test.ps1 checksum_algorithm: sha512 register: actual_sha512_checksum - name: assert sha512 checksum value assert: that: - "actual_sha512_checksum.files[0].checksum == '05abf64a68c4731699c23b4fc6894a36646fce525f3c96f9cf743b5d0c3bfd933dad0e95e449e3afe1f74d534d69a53b8f46cf835763dd42915813c897b02b87'" - name: get list of files without checksum win_find: paths: "{{win_find_dir}}\\single" patterns: test.ps1 get_checksum: False register: actual_no_checksum - name: assert no checksum is returned assert: that: - "actual_no_checksum.files[0].checksum is undefined" - name: check if broken symbolic link exists win_stat: path: "{{win_find_dir}}\\broken-link" register: broken_link_exists - name: delete broken symbolic link if it exists win_command: cmd.exe /c rmdir {{win_find_dir}}\broken-link when: broken_link_exists.stat.exists - name: check if junction symbolic link exists win_stat: path: "{{win_find_dir}}\\junction-link" register: junction_link_exists - name: delete junction symbolic link if it exists win_command: cmd.exe /c rmdir {{win_find_dir}}\junction-link when: junction_link_exists.stat.exists - name: check if nested symbolic link exists win_stat: path: "{{win_find_dir}}\\nested\\link" register: nested_link_exists - name: delete nested symbolic link if it exists win_command: cmd.exe /c rmdir {{win_find_dir}}\nested\link when: nested_link_exists.stat.exists - name: remove testing folder win_file: path: "{{win_find_dir}}" state: absent