diff options
author | anvitpusalkar <143819336+anvitpusalkar@users.noreply.github.com> | 2024-10-30 19:59:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-30 19:59:01 +0100 |
commit | 2c6b78f5166392491f43c07504029d02aecc3380 (patch) | |
tree | 83f2e7dbce0e8b42ccba478b392f256427c83255 | |
parent | Fix returning unreachable for looped tasks (#84049) (diff) | |
download | ansible-2c6b78f5166392491f43c07504029d02aecc3380.tar.xz ansible-2c6b78f5166392491f43c07504029d02aecc3380.zip |
Add --flush-cache option for ansible and ansible-console (#84149)
* Allow CLIs that accept inventory options to flush the inventory cache(s) and fact cache
Fixes #83749
-rw-r--r-- | changelogs/fragments/84149-add-flush-cache-for-adhoc-commands.yml | 2 | ||||
-rw-r--r-- | lib/ansible/cli/__init__.py | 11 | ||||
-rw-r--r-- | lib/ansible/cli/arguments/option_helpers.py | 4 | ||||
-rwxr-xr-x | lib/ansible/cli/playbook.py | 10 | ||||
-rwxr-xr-x | test/integration/targets/adhoc/runme.sh | 8 |
5 files changed, 23 insertions, 12 deletions
diff --git a/changelogs/fragments/84149-add-flush-cache-for-adhoc-commands.yml b/changelogs/fragments/84149-add-flush-cache-for-adhoc-commands.yml new file mode 100644 index 0000000000..9f407a9a0d --- /dev/null +++ b/changelogs/fragments/84149-add-flush-cache-for-adhoc-commands.yml @@ -0,0 +1,2 @@ +minor_changes: + - ansible cli - add --flush-cache option for ad-hoc commands (https://github.com/ansible/ansible/issues/83749). diff --git a/lib/ansible/cli/__init__.py b/lib/ansible/cli/__init__.py index 3e66b88f0d..03a2b3e854 100644 --- a/lib/ansible/cli/__init__.py +++ b/lib/ansible/cli/__init__.py @@ -554,9 +554,20 @@ class CLI(ABC): # the code, ensuring a consistent view of global variables variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False)) + # flush fact cache if requested + if options['flush_cache']: + CLI._flush_cache(inventory, variable_manager) + return loader, inventory, variable_manager @staticmethod + def _flush_cache(inventory, variable_manager): + variable_manager.clear_facts('localhost') + for host in inventory.list_hosts(): + hostname = host.get_name() + variable_manager.clear_facts(hostname) + + @staticmethod def get_host_list(inventory, subset, pattern='all'): no_hosts = False diff --git a/lib/ansible/cli/arguments/option_helpers.py b/lib/ansible/cli/arguments/option_helpers.py index daa7a9a9b2..18adc16455 100644 --- a/lib/ansible/cli/arguments/option_helpers.py +++ b/lib/ansible/cli/arguments/option_helpers.py @@ -297,14 +297,14 @@ def add_inventory_options(parser): help='outputs a list of matching hosts; does not execute anything else') parser.add_argument('-l', '--limit', default=C.DEFAULT_SUBSET, dest='subset', help='further limit selected hosts to an additional pattern') + parser.add_argument('--flush-cache', dest='flush_cache', action='store_true', + help="clear the fact cache for every host in inventory") def add_meta_options(parser): """Add options for commands which can launch meta tasks from the command line""" parser.add_argument('--force-handlers', default=C.DEFAULT_FORCE_HANDLERS, dest='force_handlers', action='store_true', help="run handlers even if a task fails") - parser.add_argument('--flush-cache', dest='flush_cache', action='store_true', - help="clear the fact cache for every host in inventory") def add_module_options(parser): diff --git a/lib/ansible/cli/playbook.py b/lib/ansible/cli/playbook.py index 9e0e19d3c5..a2ad80bfa2 100755 --- a/lib/ansible/cli/playbook.py +++ b/lib/ansible/cli/playbook.py @@ -143,10 +143,6 @@ class PlaybookCLI(CLI): # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts()) CLI.get_host_list(inventory, context.CLIARGS['subset']) - # flush fact cache if requested - if context.CLIARGS['flush_cache']: - self._flush_cache(inventory, variable_manager) - # create the playbook executor, which manages running the plays via a task queue manager pbex = PlaybookExecutor(playbooks=context.CLIARGS['args'], inventory=inventory, variable_manager=variable_manager, loader=loader, @@ -228,12 +224,6 @@ class PlaybookCLI(CLI): else: return results - @staticmethod - def _flush_cache(inventory, variable_manager): - for host in inventory.list_hosts(): - hostname = host.get_name() - variable_manager.clear_facts(hostname) - def main(args=None): PlaybookCLI.cli_executor(args) diff --git a/test/integration/targets/adhoc/runme.sh b/test/integration/targets/adhoc/runme.sh index eda6d66192..1b52947761 100755 --- a/test/integration/targets/adhoc/runme.sh +++ b/test/integration/targets/adhoc/runme.sh @@ -7,3 +7,11 @@ ansible -a 'sleep 20' --task-timeout 5 localhost |grep 'The command action faile # -a parsing with json ansible --task-timeout 5 localhost -m command -a '{"cmd": "whoami"}' | grep 'rc=0' + +# test ansible --flush-cache +export ANSIBLE_CACHE_PLUGIN=jsonfile +export ANSIBLE_CACHE_PLUGIN_CONNECTION=./ +# collect and cache facts +ansible localhost -m setup > /dev/null && test -s localhost +# test flushing the fact cache +ansible --flush-cache localhost -m debug -a "msg={{ ansible_facts }}" | grep '"msg": {}' |