Skip to content

[action] [PR:19258] Fix shebang to /usr/bin/python in ansible modules for compatibility#484

Merged
mssonicbld merged 1 commit intoAzure:202412from
mssonicbld:cherry/msft-202412/19258
Jul 2, 2025
Merged

[action] [PR:19258] Fix shebang to /usr/bin/python in ansible modules for compatibility#484
mssonicbld merged 1 commit intoAzure:202412from
mssonicbld:cherry/msft-202412/19258

Conversation

@mssonicbld
Copy link
Collaborator

Description of PR

We encountered the following issue when trying to upgrade ansible:
/bin/sh: 1: /usr/bin/env python3: not found
This error occurred when attempting to invoke certain Ansible modules.

According to the Module format and documentation,

image the recommended shebang is: `#!/usr/bin/python`

From the logs, we can observe the following behavior:

  • When the shebang is /usr/bin/env python, Ansible translates it into:
   203 1751357337.89274: _low_level_execute_command(): starting
   203 1751357337.89277: _low_level_execute_command(): executing: /bin/sh -c ''"'"'/usr/bin/env python'"'"' && sleep 0'
  • In contrast, when using /usr/bin/python, Ansible attempts a full interpreter discovery:
   171 1751357179.48738: _low_level_execute_command(): starting
   171 1751357179.48741: _low_level_execute_command(): executing: /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'python3.13'"'"'; command -v '"'"'python3.12'"'"'; command -v '"'"'python3.11'"'"'; command -v '"'"'python3.10'"'"'; command -v '"'"'python3.9'"'"'; command -v '"'"'python3.8'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python3'"'"'; echo ENDFOUND && sleep 0'

This indicates that Ansible treats /usr/bin/env python literally and does not invoke its interpreter auto-discovery logic, leading to failures on systems without the python symlink.

To satisfy the requirements of the latest ansible, we update the shebang lines in all affected modules in advance.

Summary:
Fixes # (issue)

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • New Test case
    • Skipped for non-supported platforms
  • Test case improvement

Back port request

  • 202205
  • 202305
  • 202311
  • 202405
  • 202411
  • 202505

Approach

What is the motivation for this PR?

We encountered the following issue when trying to upgrade ansible:
/bin/sh: 1: /usr/bin/env python3: not found
This error occurred when attempting to invoke certain Ansible modules.

According to the Module format and documentation,

image the recommended shebang is: `#!/usr/bin/python`

From the logs, we can observe the following behavior:

  • When the shebang is /usr/bin/env python, Ansible translates it into:
   203 1751357337.89274: _low_level_execute_command(): starting
   203 1751357337.89277: _low_level_execute_command(): executing: /bin/sh -c ''"'"'/usr/bin/env python'"'"' && sleep 0'
  • In contrast, when using /usr/bin/python, Ansible attempts a full interpreter discovery:
   171 1751357179.48738: _low_level_execute_command(): starting
   171 1751357179.48741: _low_level_execute_command(): executing: /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'python3.13'"'"'; command -v '"'"'python3.12'"'"'; command -v '"'"'python3.11'"'"'; command -v '"'"'python3.10'"'"'; command -v '"'"'python3.9'"'"'; command -v '"'"'python3.8'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python3'"'"'; echo ENDFOUND && sleep 0'

This indicates that Ansible treats /usr/bin/env python literally and does not invoke its interpreter auto-discovery logic, leading to failures on systems without the python symlink.

To satisfy the requirements of the latest ansible, we update the shebang lines in all affected modules in advance.

How did you do it?

To satisfy the requirements of the latest ansible, this PR updates the shebang lines in all affected modules to #!/usr/bin/python.

How did you verify/test it?

We need to make sure that this change won't affect current test firstly -- test by pipeline itself. And then, we need to make sure that this change works in the new version -- test locally.

Any platform specific information?

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

Documentation

<!--
Please make sure you've read and understood our contributing guidelines;
https://github.com/sonic-net/SONiC/blob/gh-pages/CONTRIBUTING.md

Please provide following information to help code review process a bit easier:
-->
### Description of PR
<!--
- Please include a summary of the change and which issue is fixed.
- Please also include relevant motivation and context. Where should reviewer start? background context?
- List any dependencies that are required for this change.
-->
We encountered the following issue when trying to upgrade ansible:
 `/bin/sh: 1: /usr/bin/env python3: not found`
This error occurred when attempting to invoke certain Ansible modules.

According to the [Module format and documentation](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_documenting.html#python-shebang-utf-8-coding),

<img width="1156" alt="image" src="https://github.com/user-attachments/assets/c91a0372-5622-44d7-81e1-d2bd63100d54" />
the recommended shebang is: `#!/usr/bin/python`

From the logs, we can observe the following behavior:
+ When the shebang is /usr/bin/env python, Ansible translates it into:
```
   203 1751357337.89274: _low_level_execute_command(): starting
   203 1751357337.89277: _low_level_execute_command(): executing: /bin/sh -c ''"'"'/usr/bin/env python'"'"' && sleep 0'
```

+ In contrast, when using /usr/bin/python, Ansible attempts a full interpreter discovery:
```
   171 1751357179.48738: _low_level_execute_command(): starting
   171 1751357179.48741: _low_level_execute_command(): executing: /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'python3.13'"'"'; command -v '"'"'python3.12'"'"'; command -v '"'"'python3.11'"'"'; command -v '"'"'python3.10'"'"'; command -v '"'"'python3.9'"'"'; command -v '"'"'python3.8'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python3'"'"'; echo ENDFOUND && sleep 0'
```

This indicates that Ansible treats `/usr/bin/env python` literally and does not invoke its interpreter auto-discovery logic, leading to failures on systems without the python symlink.

To satisfy the requirements of the latest ansible, we update the shebang lines in all affected modules in advance.

Summary:
Fixes # (issue)

### Type of change

<!--
- Fill x for your type of change.
- e.g.
- [x] Bug fix
-->

- [ ] Bug fix
- [ ] Testbed and Framework(new/improvement)
- [ ] New Test case
    - [ ] Skipped for non-supported platforms
- [x] Test case improvement

### Back port request
- [ ] 202205
- [ ] 202305
- [ ] 202311
- [ ] 202405
- [ ] 202411
- [ ] 202505

### Approach
#### What is the motivation for this PR?
We encountered the following issue when trying to upgrade ansible:
 `/bin/sh: 1: /usr/bin/env python3: not found`
This error occurred when attempting to invoke certain Ansible modules.

According to the [Module format and documentation](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_documenting.html#python-shebang-utf-8-coding),

<img width="1156" alt="image" src="https://github.com/user-attachments/assets/c91a0372-5622-44d7-81e1-d2bd63100d54" />
the recommended shebang is: `#!/usr/bin/python`

From the logs, we can observe the following behavior:
+ When the shebang is /usr/bin/env python, Ansible translates it into:
```
   203 1751357337.89274: _low_level_execute_command(): starting
   203 1751357337.89277: _low_level_execute_command(): executing: /bin/sh -c ''"'"'/usr/bin/env python'"'"' && sleep 0'
```

+ In contrast, when using /usr/bin/python, Ansible attempts a full interpreter discovery:
```
   171 1751357179.48738: _low_level_execute_command(): starting
   171 1751357179.48741: _low_level_execute_command(): executing: /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'python3.13'"'"'; command -v '"'"'python3.12'"'"'; command -v '"'"'python3.11'"'"'; command -v '"'"'python3.10'"'"'; command -v '"'"'python3.9'"'"'; command -v '"'"'python3.8'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python3'"'"'; echo ENDFOUND && sleep 0'
```

This indicates that Ansible treats `/usr/bin/env python` literally and does not invoke its interpreter auto-discovery logic, leading to failures on systems without the python symlink.

To satisfy the requirements of the latest ansible, we update the shebang lines in all affected modules in advance.

#### How did you do it?
To satisfy the requirements of the latest ansible, this PR updates the shebang lines in all affected modules to #!/usr/bin/python.

#### How did you verify/test it?
We need to make sure that this change won't affect current test firstly -- test by pipeline itself. And then, we need to make sure that this change works in the new version -- test locally.

#### Any platform specific information?

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

### Documentation
<!--
(If it's a new feature, new test case)
Did you update documentation/Wiki relevant to your implementation?
Link to the wiki page?
-->
@mssonicbld
Copy link
Collaborator Author

Original PR: sonic-net/sonic-mgmt#19258

@mssonicbld
Copy link
Collaborator Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld mssonicbld merged commit 0285776 into Azure:202412 Jul 2, 2025
6 of 14 checks passed
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.

1 participant