# Buggy code
def recursive_set_attributes(module, b_path, follow, file_args):
    changed = False
    for b_root, b_dirs, b_files in os.walk(b_path):
        for b_fsobj in b_dirs + b_files:
            b_fsname = os.path.join(b_root, b_fsobj)
            if not os.path.islink(b_fsname):
                tmp_file_args = file_args.copy()
                tmp_file_args["path"] = to_native(
                    b_fsname, errors="surrogate_or_strict"
                )
                changed |= module.set_fs_attributes_if_different(tmp_file_args, changed)
            else:
                tmp_file_args = file_args.copy()
                tmp_file_args["path"] = to_native(
                    b_fsname, errors="surrogate_or_strict"
                )
                changed |= module.set_fs_attributes_if_different(tmp_file_args, changed)
                if follow:
                    b_fsname = os.path.join(b_root, os.readlink(b_fsname))
                    if os.path.isdir(b_fsname):
                        changed |= recursive_set_attributes(
                            module, b_fsname, follow, file_args
                        )
                    tmp_file_args = file_args.copy()
                    tmp_file_args["path"] = to_native(
                        b_fsname, errors="surrogate_or_strict"
                    )
                    changed |= module.set_fs_attributes_if_different(
                        tmp_file_args, changed
                    )
    return changed


# Traceback (OSError)
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c '/usr/bin/python &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_rLV5HB/ansible_module_file.py", line 480, in <module>
main()
File "/tmp/ansible_rLV5HB/ansible_module_file.py", line 362, in main
changed |= recursive_set_attributes(module, to_bytes(file_args['path'], errors='surrogate_or_strict'), follow, file_args)
File "/tmp/ansible_rLV5HB/ansible_module_file.py", line 171, in recursive_set_attributes
changed |= module.set_fs_attributes_if_different(tmp_file_args, changed)
File "/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py", line 1292, in set_fs_attributes_if_different
File "/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py", line 998, in set_owner_if_different
File "/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py", line 909, in user_and_group
OSError: [Errno 2] No such file or directory: '/tmp2//root'

fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_rLV5HB/ansible_module_file.py\", line 480, in <module>\n    main()\n  File \"/tmp/ansible_rLV5HB/ansible_module_file.py\", $
ine 362, in main\n    changed |= recursive_set_attributes(module, to_bytes(file_args['path'], errors='surrogate_or_strict'), follow, file_args)\n  File \"/tmp/ansible_rLV5HB/ansible_module_file.py\", lin$
171, in recursive_set_attributes\n    changed |= module.set_fs_attributes_if_different(tmp_file_args, changed)\n  File \"/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py\", line 1292$
in set_fs_attributes_if_different\n  File \"/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py\", line 998, in set_owner_if_different\n  File \"/tmp/ansible_rLV5HB/ansible_modlib.zip/an
sible/module_utils/basic.py\", line 909, in user_and_group\nOSError: [Errno 2] No such file or directory: '/tmp2//root'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def recursive_set_attributes(module, b_path, follow, file_args):
    changed = False
    for b_root, b_dirs, b_files in os.walk(b_path):
        for b_fsobj in b_dirs + b_files:
            b_fsname = os.path.join(b_root, b_fsobj)
            if not os.path.islink(b_fsname):
                tmp_file_args = file_args.copy()
                tmp_file_args["path"] = to_native(
                    b_fsname, errors="surrogate_or_strict"
                )
                changed |= module.set_fs_attributes_if_different(
                    tmp_file_args, changed, expand=False
                )
            else:
                tmp_file_args = file_args.copy()
                tmp_file_args["path"] = to_native(
                    b_fsname, errors="surrogate_or_strict"
                )
                changed |= module.set_fs_attributes_if_different(
                    tmp_file_args, changed, expand=False
                )
                if follow:
                    b_fsname = os.path.join(b_root, os.readlink(b_fsname))
                    if os.path.isdir(b_fsname):
                        changed |= recursive_set_attributes(
                            module, b_fsname, follow, file_args
                        )
                    tmp_file_args = file_args.copy()
                    tmp_file_args["path"] = to_native(
                        b_fsname, errors="surrogate_or_strict"
                    )
                    changed |= module.set_fs_attributes_if_different(
                        tmp_file_args, changed, expand=False
                    )
    return changed