Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix apt delete package by wildcard if it is not found #70333

Closed
wants to merge 5 commits into from

Conversation

nickgryg
Copy link
Contributor

@nickgryg nickgryg commented Jun 26, 2020

SUMMARY

When we install/delete package(s) by wildcard using apt module, we always use function expand_pkgspec_from_fnmatches() to find the exact package(s) name(s). The thing is that we match our wildcard name only among packages in apt cache. That's totally correct when we install packages, because we need to be sure that we can install them in the target OS.
But it's not a case when we are trying to remove package(s) from the target system (state: 'absent'). We shouldn't care if we didn't find package(s) for removal in apt cache. The only explanation that there is(are) no package(s) installed in the target system which is(are) matched by wildcard. If so, we don't need to remove them. Ansible shouldn't throw an error in this case.

Fixes #62262

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

apt

ADDITIONAL INFORMATION

We have a simple playbook.yaml:

---
- hosts: localhost
  connection: local
  gather_facts: True
  tasks:
    - apt:
         name: "test-test-test*"
         state: absent
         autoremove: yes

Before this PR:

TASK [apt] *****************************************************************************************************************************************************************************************************
task path: /ansible/playbook.yaml:6
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir /root/.ansible/tmp/ansible-tmp-1593194564.1-9044-82833657605949 && echo ansible-tmp-1593194564.1-9044-82833657605949="` echo /root/.ansible/tmp/ansible-tmp-1593194564.1-9044-82833657605949 `" ) && sleep 0'
Using module file /ansible/lib/ansible/modules/apt.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-8994OqAboD/tmpF1KRHS TO /root/.ansible/tmp/ansible-tmp-1593194564.1-9044-82833657605949/AnsiballZ_apt.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1593194564.1-9044-82833657605949/ /root/.ansible/tmp/ansible-tmp-1593194564.1-9044-82833657605949/AnsiballZ_apt.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1593194564.1-9044-82833657605949/AnsiballZ_apt.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1593194564.1-9044-82833657605949/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
  File "/tmp/ansible_apt_payload_FIkYeG/ansible_apt_payload.zip/ansible/modules/apt.py", line 547, in expand_pkgspec_from_fnmatches
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "allow_unauthenticated": false,
            "autoclean": false,
            "autoremove": true,
            "cache_valid_time": 0,
            "deb": null,
            "default_release": null,
            "dpkg_options": "force-confdef,force-confold",
            "force": false,
            "force_apt_get": false,
            "install_recommends": null,
            "name": "test-test-test*",
            "only_upgrade": false,
            "package": [
                "test-test-test*"
            ],
            "policy_rc_d": null,
            "purge": false,
            "state": "absent",
            "update_cache": null,
            "update_cache_retries": 5,
            "update_cache_retry_max_delay": 12,
            "upgrade": null
        }
    },
    "msg": "No package(s) matching 'test-test-test*' available"
}

After this PR:

TASK [apt] *****************************************************************************************************************************************************************************************************
task path: /root/ansible/playbook.yaml:6
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir /root/.ansible/tmp/ansible-tmp-1593537430.2411246-5847-78438765107749 && echo ansible-tmp-1593537430.2411246-5847-78438765107749="` echo /root/.ansible/tmp/ansible-tmp-1593537430.2411246-5847-78438765107749 `" ) && sleep 0'
Using module file /root/ansible/lib/ansible/modules/apt.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-5798y68sy6pa/tmp0v22nj5r TO /root/.ansible/tmp/ansible-tmp-1593537430.2411246-5847-78438765107749/AnsiballZ_apt.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1593537430.2411246-5847-78438765107749/ /root/.ansible/tmp/ansible-tmp-1593537430.2411246-5847-78438765107749/AnsiballZ_apt.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1593537430.2411246-5847-78438765107749/AnsiballZ_apt.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1593537430.2411246-5847-78438765107749/ > /dev/null 2>&1 && sleep 0'
[WARNING]: No package(s) matching 'test-test-test*' to delete. It might be the name(s) was(were) misspelled.
ok: [localhost] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "allow_unauthenticated": false,
            "autoclean": false,
            "autoremove": true,
            "cache_valid_time": 0,
            "deb": null,
            "default_release": null,
            "dpkg_options": "force-confdef,force-confold",
            "force": false,
            "force_apt_get": false,
            "install_recommends": null,
            "name": "test-test-test*",
            "only_upgrade": false,
            "package": [
                "test-test-test*"
            ],
            "policy_rc_d": null,
            "purge": false,
            "state": "absent",
            "update_cache": null,
            "update_cache_retries": 5,
            "update_cache_retry_max_delay": 12,
            "upgrade": null
        }
    }
}
META: ran handlers
META: ran handlers

@ansibot ansibot added affects_2.11 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. new_contributor This PR is the first contribution by a new community member. packaging Packaging category support:core This issue/PR relates to code supported by the Ansible Engineering Team. traceback This issue/PR includes a traceback. labels Jun 26, 2020
nickgryg added a commit to nickgryg/ansible that referenced this pull request Jun 26, 2020
@ansibot ansibot added the support:community This issue/PR relates to code supported by the Ansible community. label Jun 26, 2020
@samdoran samdoran removed the needs_triage Needs a first human triage before being processed. label Jun 30, 2020
lib/ansible/modules/apt.py Outdated Show resolved Hide resolved
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. core_review In order to be merged, this PR must follow the core review workflow. and removed core_review In order to be merged, this PR must follow the core review workflow. new_contributor This PR is the first contribution by a new community member. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jun 30, 2020
@nickgryg nickgryg requested a review from bcoca June 30, 2020 17:30
lib/ansible/modules/apt.py Outdated Show resolved Hide resolved
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jul 2, 2020
@nickgryg nickgryg requested a review from bcoca July 2, 2020 02:42
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Jul 2, 2020
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Jul 10, 2020
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Aug 3, 2022
@ansibot
Copy link
Contributor

ansibot commented Aug 3, 2022

The test ansible-test sanity --test pylint [explain] failed with 2 errors:

lib/ansible/modules/apt.py:620:82: undefined-variable: Undefined variable 'to_text'
lib/ansible/modules/apt.py:623:33: undefined-variable: Undefined variable 'to_text'

click here for bot help

@ansibot ansibot added ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Aug 3, 2022
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Aug 3, 2022
@nickgryg nickgryg requested a review from s-hertel August 3, 2022 18:44
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Aug 3, 2022
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Aug 11, 2022
@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html and removed stale_review Updates were made after the last review and the last review is more than 7 days old. labels Sep 28, 2022
@bcoca
Copy link
Member

bcoca commented Apr 5, 2023

@nickgryg can you resolve the conflict and add tests?
!waiting_on_contributor

@ansibot ansibot added stale_review Updates were made after the last review and the last review is more than 7 days old. has_issue and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jul 12, 2023
@ansibot ansibot removed the needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html label Sep 7, 2023
@ansibot ansibot added the needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html label Sep 14, 2023
@ansibot ansibot removed the needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html label Sep 28, 2023
@ansibot ansibot added the needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html label Oct 5, 2023
@Akasurde
Copy link
Member

I am unable to reproduce the original issue with Ansible 2.14/2.15/2.16 and devel. Closing this PR.

@nickgryg Thanks for the contribution. Please feel free to open a new PR if the problem persists. Thanks.

@Akasurde Akasurde closed this Mar 18, 2024
@ansible ansible locked and limited conversation to collaborators Apr 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.11 bug This issue/PR relates to a bug. has_issue module This issue/PR relates to a module. needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html packaging Packaging category stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old. support:core This issue/PR relates to code supported by the Ansible Engineering Team. traceback This issue/PR includes a traceback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

apt absent fails when wildcard doesn't match any packages
6 participants