Skip to content

[action] [PR:9530] Workaound for ansible module executed twice issue of pytest-ansible#9544

Merged
mssonicbld merged 1 commit intosonic-net:202305from
mssonicbld:cherry/202305/9530
Aug 18, 2023
Merged

[action] [PR:9530] Workaound for ansible module executed twice issue of pytest-ansible#9544
mssonicbld merged 1 commit intosonic-net:202305from
mssonicbld:cherry/202305/9530

Conversation

@mssonicbld
Copy link
Copy Markdown
Collaborator

Description of PR

Summary:
Fixes # (issue)

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • Test case(new/improvement)

Back port request

  • 201911
  • 202012
  • 202205

Approach

What is the motivation for this PR?

This is to workaround pytest-ansible plugin issue:
Even no extra-inventory is specified, extra_inventory_manager will still be initialized.
ansible/pytest-ansible#135

As of today, pytest-ansible supports host manager and module dispatcher v29, v212, v213. While initializing the pytest-ansible plugin, it tries to collect default ansible configurations and command line options. These options are used for creating host manager. When no extra_inventory is specified, the options passed to host manager will include extra_inventory=None. However, the host manager will still try to create extra_inventory_manager with None as the inventory source. This will cause the module dispatcher to run the module on hosts matching host pattern in both inventory and extra inventory. This would cause an ansible module executed twice on the same host. In case we wish to use the shell module to run command like "rm <some_file>" to delete a file. The second run would fail because the file has been deleted in the first run. For more details, please refer to the Github issue mentioned above.

This change is only required for python3. Currently master and 202305 branch are using python3. For older branches, they are still using python2 and an older version of pytest-ansible which does not have the bug.

How did you do it?

The fix is to overwrite the pytest-ansible plugin's initialize method and remove the extra_inventory option if it is None. This will prevent the host manager from creating extra_inventory_manager with None as the inventory source.

How did you verify/test it?

Tried run a few tests with pytest-ansible 3.2.1 and 4.0.0.

Any platform specific information?

Supported testbed topology if it's a new test case?

Documentation

…onic-net#9530)

What is the motivation for this PR?
This is to workaround pytest-ansible plugin issue:
Even no extra-inventory is specified, extra_inventory_manager will still be initialized.
ansible/pytest-ansible#135

As of today, pytest-ansible supports host manager and module dispatcher v29, v212, v213. While initializing the pytest-ansible plugin, it tries to collect default ansible configurations and command line options. These options are used for creating host manager. When no extra_inventory is specified, the options passed to host manager will include extra_inventory=None. However, the host manager will still try to create extra_inventory_manager with None as the inventory source. This will cause the module dispatcher to run the module on hosts matching host pattern in both inventory and extra inventory. This would cause an ansible module executed twice on the same host. In case we wish to use the shell module to run command like "rm <some_file>" to delete a file. The second run would fail because the file has been deleted in the first run. For more details, please refer to the Github issue mentioned above.

This change is only required for python3. Currently master and 202305 branch are using python3. For older branches, they are still using python2 and an older version of pytest-ansible which does not have the bug.

How did you do it?
The fix is to overwrite the pytest-ansible plugin's initialize method and remove the extra_inventory option if it is None. This will prevent the host manager from creating extra_inventory_manager with None as the inventory source.

How did you verify/test it?
Tried run a few tests with pytest-ansible 3.2.1 and 4.0.0.

Signed-off-by: Xin Wang <[email protected]>
@mssonicbld
Copy link
Copy Markdown
Collaborator Author

Original PR: #9530

@mssonicbld mssonicbld merged commit e21c3ae into sonic-net:202305 Aug 18, 2023
@mssonicbld mssonicbld deleted the cherry/202305/9530 branch February 4, 2024 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants