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

Inventory cache not flushed when using --flush-cache #73699

Closed
ioguix opened this issue Feb 23, 2021 · 5 comments · Fixed by #77083
Closed

Inventory cache not flushed when using --flush-cache #73699

ioguix opened this issue Feb 23, 2021 · 5 comments · Fixed by #77083
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. inventory Inventory category P3 Priority 3 - Approved, No Time Limitation python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@ioguix
Copy link

ioguix commented Feb 23, 2021

SUMMARY

The inventory cache is not flushed when using --flush-cache

ISSUE TYPE
  • Bug Report
COMPONENT NAME

cli and/or inventory manager

ANSIBLE VERSION
ansible 2.9.16
  config file = /home/ioguix/git/support/ansible/ansible.cfg
  configured module search path = ['/home/ioguix/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]
CONFIGURATION
DEFAULT_HOST_LIST(/tmp/prov/ansible/ansible.cfg) = ['/tmp/prov/ansible/bug_report.yml']
DEFAULT_INVENTORY_PLUGIN_PATH(/tmp/prov/ansible/ansible.cfg) = ['/tmp/prov/ansible']
INVENTORY_CACHE_ENABLED(/tmp/prov/ansible/ansible.cfg) = True
INVENTORY_CACHE_PLUGIN(/tmp/prov/ansible/ansible.cfg) = jsonfile
INVENTORY_CACHE_PLUGIN_CONNECTION(/tmp/prov/ansible/ansible.cfg) = .
OS / ENVIRONMENT

Debian 10

STEPS TO REPRODUCE

ansible.cfg:

[defaults]
inventory_plugins = .
inventory = bug_report.yml

[inventory]
cache=True
cache_plugin=jsonfile
cache_connection=.

Dummy inventory plugin bug_report.py displaying if the inventory manager gave cache=True or not:

from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable

class InventoryModule(BaseInventoryPlugin, Cacheable):
    def parse(self, inventory, loader, path, cache=True):
        if cache:
            self.display.v("Using data from cache")
        else:
            self.display.v("Updating cache")

Inventory yaml file bug_report.yml:

---
plugin: bug_report
EXPECTED RESULTS

The doc state in https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#inventory-cache:

This value comes from the inventory manager and indicates whether the inventory is being refreshed (such as via --flush-cache or the meta task refresh_inventory).

From previous minimal test case, as far as I understand the doc and comments in other inventory plugins, the first call, using --flush-cache, should output Updating cache. The second one should output Using data from cache:

$ ansible-playbook -v --list-hosts --flush-cache playbook-dummy.yml
Using /tmp/prov/ansible/ansible.cfg as config file
Updating cache
[...]

$ ansible-playbook -v --list-hosts playbook-dummy.yml
Using /tmp/prov/ansible/ansible.cfg as config file
Using data from cache
[...]
ACTUAL RESULTS

No matter if using --flush-cache or not, the inventory manager is setting cache=True and the plugin always shows Using data from cache:

$ ansible-playbook -v --list-hosts --flush-cache playbook-dummy.yml
Using /tmp/prov/ansible/ansible.cfg as config file
Using data from cache
[...]

$ ansible-playbook -v --list-hosts playbook-dummy.yml
Using /tmp/prov/ansible/ansible.cfg as config file
Using data from cache
[...]

Looking at source code in current devel branch, it seems --flush-cache only flush facts and ignore inventory: https://github.com/ansible/ansible/blob/devel/lib/ansible/cli/playbook.py#L208

@ansibot
Copy link
Contributor

ansibot commented Feb 23, 2021

Files identified in the description:

  • lib/ansible/inventory

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. inventory Inventory category needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Feb 23, 2021
@ioguix
Copy link
Author

ioguix commented Feb 24, 2021

The following patch seem to do the trick, but it's really naive fix, leading to load the inventory from cache, THEN refresh the inventory and the cache:

diff --git a/lib/ansible/cli/playbook.py b/lib/ansible/cli/playbook.py
index a3890df18a..fa9906430e 100644
--- a/lib/ansible/cli/playbook.py
+++ b/lib/ansible/cli/playbook.py
@@ -209,3 +209,4 @@ class PlaybookCLI(CLI):
         for host in inventory.list_hosts():
             hostname = host.get_name()
             variable_manager.clear_facts(hostname)
+        inventory.refresh_inventory()

I'm sure there's some smarter way to fix this. I can imagine at least two different ways:

  • adding cache parameter to inventory constructor
  • set parse=False to the inventory constructor from _play_prereqs then call inventory.parse_sources(cache=False) there

@jborean93 jborean93 added P3 Priority 3 - Approved, No Time Limitation and removed needs_triage Needs a first human triage before being processed. labels Feb 25, 2021
@wbh1
Copy link

wbh1 commented Feb 14, 2022

This is happening to me on 2.12, as well.

@bvergnaud
Copy link

Same thing over here.

ansible-playbook 2.10.17
  config file = /home/bvergnaud/anonymized/ansible.cfg
  configured module search path = ['/home/bvergnaud/anonymized/library/ansible']
  ansible python module location = /home/bvergnaud/.local/share/virtualenvs/iaas-playbooks--tQt6UoE/lib/python3.8/site-packages/ansible
  executable location = /home/bvergnaud/.local/share/virtualenvs/iaas-playbooks--tQt6UoE/bin/ansible-playbook
  python version = 3.8.12 (default, Jan  1 2022, 20:36:01) [GCC 11.1.0]

@bcoca
Copy link
Member

bcoca commented Feb 21, 2022

Currently the patch above would be only way. To 'properly implement' this requires new features:

  1. add 'flush cache' ability to each inventory plugin
  2. add a call from inventory manager to cycle over all inventory sources and run their 'flush cache' methods

or

always pass cache=true|false to inventory on startup

@ansibot ansibot added the has_pr This issue has an associated PR. label Feb 21, 2022
@ansible ansible locked and limited conversation to collaborators Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. inventory Inventory category P3 Priority 3 - Approved, No Time Limitation python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants