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
pull/84210/head
anvitpusalkar 1 year ago committed by GitHub
parent 03acb22f99
commit 2c6b78f516
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
minor_changes:
- ansible cli - add --flush-cache option for ad-hoc commands (https://github.com/ansible/ansible/issues/83749).

@ -554,8 +554,19 @@ 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'):

@ -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):

@ -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)

@ -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": {}'

Loading…
Cancel
Save