# Sanity Tests - How to Fix "module-guard"

**URL:** <https://forum.ansible.com/t/sanity-tests-how-to-fix-module-guard/46856>\
**Category:** Get Help\
**Created:** [December 1, 2026, 10:20am UTC](https://forum.ansible.com/t/sanity-tests-how-to-fix-module-guard/46856 "2026-12-01T10:20:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![roelvandepaar](https://avatars.discourse-cdn.com/v4/letter/r/aeb1de/32.png) [@roelvandepaar](https://forum.ansible.com/u/roelvandepaar)\
**Post date:** [December 1, 2026, 10:20am UTC](https://forum.ansible.com/t/sanity-tests-how-to-fix-module-guard/46856/1 "2026-12-01T10:20:50Z")

</div>

`ansible-test sanity --docker --test import` reports the following `import` error:

```auto
module-guard: AnsibleModule instantiation not guarded by `if __name__ == ' __main__'`

```

My module has this guard (that I still do not understand) around the `main` function:

```auto
if __name__ == ' __main__':
    main()

```

Inside the main function a class is instantiated:

```auto
def main():
    mymodule = MYMODULE()
    mymodule.run()

```

Inside the class the Ansible Module will be created.

How can I fix the sanity test error and why is this important?

---

<div class="post-metadata">

**Author:** ![Rei\_Kurosawa](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@Rei\_Kurosawa](https://forum.ansible.com/u/Rei_Kurosawa)\
**Post date:** [December 1, 2026, 10:31am UTC](https://forum.ansible.com/t/sanity-tests-how-to-fix-module-guard/46856/2 "2026-12-01T10:31:42Z")

</div>

The sanity import test first imports your module normally, with `AnsibleModule` swapped for a stand-in that raises an exception as soon as it is instantiated. It then runs the module a second time as ` __main__ `, and `module-guard` is reported when that exception already happens during the first plain import. So the guard you show is fine, and something outside it is creating the module while the file is being imported. Is `MYMODULE()` or `AnsibleModule(` called anywhere outside `main`, for example at module level, in a class body or in a default argument?  
Assisted-by: AI.

---

<div class="post-metadata">

**Author:** ![roelvandepaar](https://avatars.discourse-cdn.com/v4/letter/r/aeb1de/32.png) [@roelvandepaar](https://forum.ansible.com/u/roelvandepaar)\
**Post date:** [December 1, 2026, 10:50am UTC](https://forum.ansible.com/t/sanity-tests-how-to-fix-module-guard/46856/3 "2026-12-01T10:50:46Z")

</div>

`AnsibleModule()` is called directly in the class `MYMODULE`. (not in a method)

The class itself is only instantiated once inside the `main` function.

---

<div class="post-metadata">

**Author:** ![Rei\_Kurosawa](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@Rei\_Kurosawa](https://forum.ansible.com/u/Rei_Kurosawa)\
**Post date:** [December 1, 2026, 11:01am UTC](https://forum.ansible.com/t/sanity-tests-how-to-fix-module-guard/46856/4 "2026-12-01T11:01:42Z")

</div>

That explains it. The body of a class runs as soon as the file is imported, so `AnsibleModule()` is created during the first plain import, where the test’s stand-in raises an exception, and that is what `module-guard` reports. If you move that call into a method such as ` __init__ ` or `run`, it only runs when `main` creates the class during the ` __main__ ` run, which is the case the test accepts. Also check that nothing else at module level or in the class body calls `AnsibleModule(` before `main` is reached.  
Assisted-by: AI.
