Skip to content

Commit

Permalink
Eaiser package mgr overrides
Browse files Browse the repository at this point in the history
also better delegation
added to docs of 'use'
  • Loading branch information
bcoca committed Jan 31, 2024
1 parent 7a0c321 commit 7ccad16
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/pkg_mgr_peek.yml
@@ -0,0 +1,2 @@
minor_changes:
- package action now has a configuration and local fact that allow to override the package manager to use and not just rely on detection.
10 changes: 10 additions & 0 deletions lib/ansible/config/base.yml
Expand Up @@ -1779,6 +1779,16 @@ OLD_PLUGIN_CACHE_CLEARING:
type: boolean
default: False
version_added: "2.8"
PACKAGE_MANAGER_OVERRIDE:
name: Override the package manager
description:
- When set, allows overriding the detected package manager fact when using the package action.
env: [{name: ANSIBLE_PACKAGE_MANAGER_OVERRIDE}]
ini:
- {key: package_manager_override, section: defaults}
vars:
- name: ansible_package_manager_override
version_added: '2.17'
PAGER:
name: pager application to use
default: less
Expand Down
2 changes: 2 additions & 0 deletions lib/ansible/modules/package.py
Expand Up @@ -40,6 +40,8 @@
description:
- The required package manager module to use (V(dnf), V(apt), and so on). The default V(auto) will use existing facts or try to autodetect it.
- You should only use this field if the automatic selection is not working for some reason.
- Since version 2.17 you can override the automatic detection both via a local facts by setting 'pkg_mgr' or override both by using the
PACKAGE_MANAGER_OVERRIDE configuration settable via ANSIBLE_PACKAGE_MANAGER_OVERRIDE enviornment variable or ansible_package_manager_override variable.
default: auto
requirements:
- Whatever is required for the package plugins specific for each system.
Expand Down
11 changes: 9 additions & 2 deletions lib/ansible/plugins/action/package.py
Expand Up @@ -16,6 +16,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations

from ansible import constants as C
from ansible.errors import AnsibleAction, AnsibleActionFail
from ansible.executor.module_common import get_action_args_with_defaults
from ansible.module_utils.facts.system.pkg_mgr import PKG_MGRS
Expand Down Expand Up @@ -45,14 +46,20 @@ def run(self, tmp=None, task_vars=None):
if module == 'auto':
try:
if self._task.delegate_to: # if we delegate, we should use delegated host's facts
module = self._templar.template("{{hostvars['%s']['ansible_facts']['pkg_mgr']}}" % self._task.delegate_to)
module = C.config.get_config_value('PACKAGE_MANAGER_OVERRIDE', variables=combine_vars(self._task.vars, task_vars.get('delegated_vars', {})))
if module is None:
dh = '%s' % self._task.delegate_to
module = self._templar.template(f"{{hostvars['{dh}']['ansible_local']['pkg_mgr']|default(hostvars['{dh}']['ansible_facts']['pkg_mgr']})}}"
else:
module = self._templar.template('{{ansible_facts.pkg_mgr}}')
module = C.config.get_config_value('PACKAGE_MANAGER_OVERRIDE', variables=task_vars)
if module is None:
module = self._templar.template('{{ansible_local.pkg_mgr|default(ansible_facts.pkg_mgr)}}')
except Exception:
pass # could not get it from template!

try:
if module == 'auto':
# TODO: change to action 'gather_facts'
facts = self._execute_module(
module_name='ansible.legacy.setup',
module_args=dict(filter='ansible_pkg_mgr', gather_subset='!all'),
Expand Down

0 comments on commit 7ccad16

Please sign in to comment.