-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
proxmox inventory: refresh_inventory not working with cache enabled #9710
Comments
Files identified in the description: If these files are incorrect, please update the |
As a first question, did you also see this behavior with other dynamic inventory plugins? From what I read in the code, there's no special handling of cached resources (except that the module is compatible with caches). Also, you can't change the inventory mid-play, same goes with the Which, unless I'm mistaken, should mean that it'll be fixed with this:
|
Basically you can update the inventory, but you cannot change the hosts the current play works on. After using Here's a simple test with the ini inventory plugin: # Save as test-inventory.yml; also create an empty file test-inventory.ini.
# Run with:
# ansible-playbook test-inventory.yml -i test-inventory.ini
- name: Reset state
hosts: localhost
gather_facts: false
tasks:
- name: Reset inventory file
ansible.builtin.copy:
dest: test-inventory.ini
content: |
[group]
primary ansible_connection=local
- name: Refresh inventory
ansible.builtin.meta: refresh_inventory
- name: Do something
hosts: group
gather_facts: false
tasks:
- name: Run command only on primary
ansible.builtin.command:
cmd: ls
- name: Update inventory file
ansible.builtin.copy:
dest: test-inventory.ini
content: |
[group]
primary ansible_connection=local
secondary ansible_connection=local
- name: Show group 'group', should still have one element
ansible.builtin.debug:
msg: "{{ groups.group }}"
delegate_to: secondary
- name: Refresh inventory
ansible.builtin.meta: refresh_inventory
- name: Show group 'group', should now have two elements
ansible.builtin.debug:
msg: "{{ groups.group }}"
delegate_to: secondary
- name: Run a command on all hosts, which happens to be only primary
ansible.builtin.command:
cmd: ls
- name: Run a command on all hosts, which happens to be only primary
ansible.builtin.command:
cmd: ls
delegate_to: secondary
- name: Do more
hosts: group
gather_facts: false
tasks:
- name: Run a command on primary and secondary
ansible.builtin.command:
cmd: ls
- name: Reset inventory file
ansible.builtin.copy:
dest: test-inventory.ini
content: |
[group]
primary ansible_connection=local
run_once: true I also tried this out by removing primary in the updated inventory (commenting out line 30), the result is that basically the middle play stops after the inventory refresh since the host is gone. So while you cannot add new hosts, you can remove existing ones with immediate effect :) (Just checked that because I was curious, I know this wasn't the question here.) |
A minor, yet very impactful detail I had wrong 😁 I didn't know that! This sounds like a really neat trick that can be useful for
Same goes here as well, I think it makes sense from how a program works kinda way, though it feels just weird that you can footgun hosts out of your play. I don't see a case for fault handling here, there's plenty of better documented ways to do so. |
But anyway, back on topic, @iqt4 the solution to your inventory woes is to either use Can you let us know if that fixes it? |
Maybe there should be an ansible-lint rule that |
I created ansible/ansible#84691 to improve the docs on |
Thanks for your quick responses. Felix, I understand your code, but I assume the topic is cache handling of the proxmox inventory plugin.
From ansible.builtin.meta module: ... you can disable the cache or, if available for your specific inventory datasource (e.g. aws), you can use the an inventory plugin instead of an inventory script). This is mainly useful when additional hosts are created and users wish to use them instead of using the ansible.builtin.add_host module. From inventory cache: ... the cache should be updated with the new inventory if the user has enabled caching. I hope that clarifies my observation and expectation. @Thulium-Drake: I have not tried any other dynamic inventory plugins |
As all the experiments by @Thulium-Drake and me above shown your problem has nothing to do with caching. It's an inherent behavior of how inventories work in Ansible. You need to start a new play to include new hosts in whatever you specified in |
The problem does not appear if I switch off the cache in the inventory plugin. The code runs always without error - independent if the new host is created during the play (inventory changed) or already existing before the play (inventory not changed). From my understanding there is an inventory (in memory, created at the begin/before the playbook) and a cache (json file). According to the documentation I found this description hxxps://steampunk.si/blog/cloud-automation-with-ansible-series-inventory-caching/ (I don't have access to this service, thus I can't test it, but it confirms my expectations. Please also look at fix) Conclusion: The issue remains, the cache file is unchanged if I create a new host and refresh the inventory with I will try another inventory plugin (may be nmap?), which will need some time. Thanks again for your patience and support |
One thing this plugin does wrong is how it handles the cache. It only has one knob "use cache or not" in its internals, which does not allow for "do not use cache, but refresh it" ( |
I tested with the nmap inventory plugin and can confirm that this one runs without error. I started to debug the code:
I need more time to test this but some of the cache logic should probably be outside the |
After some further analysis, I would like to make the proposal attached. The flow chart is not perfect but should get an idea of a solution. |
Simplified approach: Inventory-Proxmox v2.drawio.pdf |
* Proposal for #9710 * Fixed comments * Fixed trailing whitespace * Fixed changelog fragment
…nsible-collections#9760) * Proposal for ansible-collections#9710 * Fixed comments * Fixed trailing whitespace * Fixed changelog fragment
Summary
I have a dynamic proxmox inventory with cache enabled.
During a play I create an lxc container with community.general.proxmox module.
After successfully creating the container I update the inventory with ansible.builtin.meta: refresh_inventory.
However, the inventory get's updated in the play but not the cache.
Issue Type
Bug Report
Component Name
proxmox inventory
Ansible Version
Community.general Version
Configuration
OS / Environment
MacOS Sequoia 15.3
Steps to Reproduce
Expected Results
If you run this play the first time, the lxc container is set up. The line
vmid: "{{ hostvars[ct_name].proxmox_vmid }}"
does not give an error and the container is started.The cache should be updated as well and the play runs the second time (without cache update) without error.
Actual Results
Timestamp (and content) of the cache file under .cache directory does not change. Play gives an error:
The task includes an option with an undefined variable.. "hostvars['debian-t']" is undefined
Code of Conduct
The text was updated successfully, but these errors were encountered: