Commit Graph

74 Commits (9f899f94924fafeb330b1e8b18c970742da56e0d)

Author SHA1 Message Date
Matt Clay 9f899f9492
Require `from __future__ import annotations` (#81902) 8 months ago
Matt Clay 2cd1744be3
Use ansible.module_utils.common.text.converters (#80704)
Replace use of old `ansible.module_utils._text` and add a unit test to maintain backwards compatibility.
1 year ago
Brian Coca 89c6547892
preserve add_host/group_by on refresh (#77944)
* preserve add_host/group_by on meta: refresh_inventory

Co-authored-by: Jordan Borean <jborean93@gmail.com>
2 years ago
Jon "The Nice Guy" Spriggs 5b44035983
Hide "[WARNING]: No inventory was parsed" message (#65499)
* Add config option INVENTORY_UNPARSED_WARNING to hide the warning "No inventory was parsed, only implicit localhost is available"
2 years ago
Brian Coca 94b73d66d5
inventory manager respect --flush-cache (#77083) 2 years ago
Felix Fontein ee80e856c2
Remove more whitelist and blacklist occurances (docs only). (#76845) 2 years ago
Abhijeet Kasurde 4b347415fa
inventory: misc typo fixes (#73695)
* Misc typo fixes

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
3 years ago
Rick Elrod fa046d302c
[InventoryManager] Fix two unhandled exceptions (#73667)
Change:
- Fix regression: unhandled exception when given inventory directory
  is empty or contains empty subdirectories.
- Fix unhandled exception when limit file is actually a directory
  instead of a file.
- Fix inventory tests which previously could never fail due to missing
  `set -e`. Fixed up tests that failed after `set -e` was added. Added
  several tests.

Test Plan:
- New tests
- Fixed existing tests which previously could never fail

Tickets:
- Fixes #73658

Signed-off-by: Rick Elrod <rick@elrod.me>
3 years ago
Brian Coca ea2f37d253
allow constructed to use vars plugin (#73418)
Allow constructed to optionally use vars plugin data

* mostly for those looking to leverage group_vars/ and host_vars/
* limited to already processed sources
3 years ago
Brian Coca 1e27d4052a
fix inventory source parse error handling (#73160)
fixes #51025

added test cases
3 years ago
Matt Martz db98433e7a
Use InventoryManager for inventory_hostnames lookup (#71573)
* Use InventoryManager for inventory_hostnames lookup. Fixes #17268

* Add test for using patterns

* s/it's/its/

* Allow bypassing parse_sources in InventoryManager

* Remove unneeded method call
4 years ago
Rick Elrod 9579113941 Remove some no-op code from InventoryManager
Change:
- Remove some no-op code
- Split up a somewhat complex line into two lines
- Nuke an incorrect comment

Test Plan:
CI

Signed-off-by: Rick Elrod <rick@elrod.me>
4 years ago
Toshio Kuratomi 39b3942048 Remove left hand side slicing
Left hand side slicing is confusing and slower but maybe more memory
efficient in some circumstances.  There is one case where it adds to
code safety: when it's used to substitute a different list in place of a
slice of the original list and the original list could have been bound
to a different variable in some other code.  (The most likely case of
this is when it's a global variable and some other code might import
that variable name).

Because of the confusion factor we think it should only be used for the
safety case or where it's been benchmarked and shown to have some sort
of documentatble improvement.  At the moment, only one piece of code
falls into those categories so this PR removes all the other instances
of left hand side slicing.
4 years ago
Abhijeet Kasurde 41e19a4058
inventory: Fail on non-existing limit file (#59758)
Ansible now fails with error message when user provides
non-existing limit file.

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
5 years ago
Sloane Hertel c1f1b2029c
Support vars plugins in collections (#61078)
* Move var plugins handling to a separate file

* Allow var plugins to require whitelisting

* Add global configuration ('demand', 'start') for users to control when they execute

* Add 'stage' configuration ('all', 'task', 'inventory') for users to control on a per-plugin basis when they execute

* Update ansible-inventory and InventoryManager to the global and stage configuration

* Update host_group_vars to use stage configuration and whitelisting

* Add documentation for using new options and to the developer's guide

* Add integration tests to exercise whitelisting and the new configuration options, using vars plugins in collections, and maintain backward compatibility

* Changelog

Co-Authored-By: Brian Coca <brian.coca+git@gmail.com>
Co-Authored-By: Sandra McCann <samccann@redhat.com>
5 years ago
Sam Doran 987265a6ef
Account for empty strings when splitting the host pattern (#62442)
Improve tests
- add more unit test cases
- add specific integration test with more cases

Testing shows no major downside to calling .strip() twice in a comprehension vs. using a regular for loop and only calling .strip() once. Going with the comprehension for ease of maintenance and because comprehensions are optimized in CPython.
5 years ago
Abhijeet Kasurde 2ebc4e1e7e inventory: Handle IndexError while parsing limit file (#59776)
Handle IndexError exception raised while parsing the limit file.

Fixes: #59695

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
5 years ago
Matt Martz 284dafe476
Perfy McPerferton (#58400)
* InventoryManager start of perf improvements

* 0 not 1

* More startswith to [0] improvements

* Remove unused var

* The hash doesn't need to be a string, start as a list, make it into a tuple

* set actually appears faster than frozenset, and these don't need to be frozen

* Cache hosts lists, to avoid extra get_hosts calls, pass to get_vars too

* negligible perf improvement, it could help with memory later

* Try the fast way, fallback to the safe way

* Revert to previous logic, linting fix

* Extend pre-caching to free

* Address test failures

* Hosts are strings

* Fix unit test

* host is a string

* update test assumption

* drop SharedPluginLoaderObj, pre-create a set, instead of 2 comparisons in the list comprehension

* Dedupe code

* Change to _hosts and _hosts_all in get_vars

* Add backwards compat for strategies that don't do set host caches

* Add deprecation message to SharedPluginLoaderObj

* Remove unused SharedPluginLoaderObj import

* Update docs/comments

* Remove debugging

* Indicate what patterh_hash is

* That won't work

* Re-fix tests

* Update _set_hosts_cache to accept the play directly, use without refresh in get_hosts_remaining and get_failed_hosts for backwards compat

* Rename variable to avoid confusion

* On add_host only manipulate _hosts_cache_all

* Add warning docs around _hosts and _hosts_all args
5 years ago
Abhijeet Kasurde 4b152235ff
Typo fixes (#59227)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
5 years ago
Matt Martz 728fce0c44 Perf improvement for Templar.is_template (#57489)
* Faster is_template
5 years ago
Paul Gear 99f9f49eca Correct grammar (#57385)
"it's" == "it is"
"its" == "belonging to it"
5 years ago
Sloane Hertel cea7151a5b The InventoryData class does not have a get_vars method and InventoryManager's get_vars never appears to have been used (#56804) 5 years ago
Sloane Hertel afb5e02c19 preserve same order as inventory manager when using host lookup (#55331)
* preserve same order as inventory manager when using inventory_hostnames lookup

add a test

* move generic code
5 years ago
Brian Coca 5f69ab7f46 Better test for cache method availability (#54740)
* Better test for cache method availability

  fixes #54737

* try and ignore missing method

* avoid plugin cross contamination

* ammend clog
5 years ago
Sebastian Meyer 8d7efa7d32 Strip whitespace when reading limit file (#53564)
Fix #17088
5 years ago
Sloane Hertel 9687879840
Fix inventory cache interface (#50446)
* Replace InventoryFileCacheModule with a better developer-interface

Use new interface for inventory plugins with backwards compatibility

Auto-update the backing cache-plugin if the cache has changed after parsing the inventory plugin

* Update CacheModules to use the config system and add a deprecation warning if they are being imported directly rather than using cache_loader

* Fix foreman inventory caching

* Add tests

* Add integration test to check that fact caching works normally with cache plugins using ansible.constants and inventory caching provides a helpful error for non-compatible cache plugins

* Add some developer documentation for inventory and cache plugins

* Add user documentation for inventory caching

* Add deprecation docs

* Apply suggestions from docs review

* Add changelog
5 years ago
Brian Coca 621b052777
make missing pattern ansibleerror (#51397)
ansibleoptionserror should be limited to cli options
 (technically --limit makes this a bit grey but hosts: should not trigger the same error)
5 years ago
Brian Coca 9a1c291d01 add toggle for host pattern mismatch behaviour (#51199)
* add toggle for host pattern mismatch behaviour

	fixes #40030

* fix
5 years ago
Brian Coca 8fd0fbe431 remove usless tb initiator
always pass proper tb
5 years ago
AlanCoding f018b9d07d Fix missing tracebacks in ansible-inventory
(cherry picked from commit b62693299b)
5 years ago
Matt Martz a0d71e7735
Fix reverse_inventory order to work on python3 (#49895) 6 years ago
Pilou 1bad617f29 Inventory plugins move auto before ini (#44428)
* inventory plugins: try auto before ini

auto plugin should run before ini to avoid ini being able to parse
some plugin configuration YAML files successfully.

* Update comment

comment was added by 2ffe3c42bb but
related code was later removed by
506e6a0b2d.
6 years ago
Brian Coca ae2467ddbc Merge pull request #49080 from bcoca/better_invp_skip
more precise pluing skip msg and now in higher verbosity level
6 years ago
Matt Martz 9773a1f289
Add a Singleton metaclass, use it with Display (#48935)
* Add a Singleton class, use it with Display

* update six import

* Move remaining failes to display singleton

* Fix rebase issues

* Singleton improvements

* Add code-smell for 'from __main__ import display'. ci_complete

* s/self/cls/g

* Add docs for no-main-display

* Address linting issues

* Add changelog fragment. ci_complete

* Implement reentrant lock for class instantiation in Singleton

* Add Display singleton porting guide
6 years ago
Brian Coca 903cfa63cb
make inventory warnings a bit smarter (#46284)
less annoying for common cases
 add comment for 'tricky' conditional
6 years ago
Brian Coca 9e0c2a658f
better information for user from inventory plugins (#46766)
* better information for user from inventory plugins

 - use foreman as example
6 years ago
Brian Coca 937e710485
ensure 'text' source assumptions (#42522)
* ensure 'text' source assumptions
6 years ago
Matt Martz abb05c98f3 Make sure we are comparing bytes extensions in inventory plugins (#42475)
* Ensure we are comparing text paths with extensions. Fixes #42118

* Add changelog
6 years ago
Sloane Hertel 44e5886385 Fix setting required options for inventory plugins (#42049)
* Fix setting required options for inventory plugins

* call set_options for yaml and script plugin
6 years ago
Brian Coca 89f169127b added missing raise 6 years ago
Abhijit Menon-Sen 276358c885
Introduce inventory.any_unparsed_is_failed configuration setting (#41171)
In the process of building up the inventory by parsing each inventory
source with each available inventory plugin, there are three kinds of
possible errors (listed in order from earliest to latest):

1. One source could not be parsed by a particular plugin.
2. One source could not be parsed by any available plugin.
3. ALL sources could not be parsed by any available plugin.

The errors in (1) are a part of normal operation, e.g., the script
plugin is expected to fail to parse an ini-format source, and we will
ignore that error and try the next plugin. There is currently no way to
control this, and no known compelling use-case for a setting to control
it. This commit does not make any changes here.

We implement "any_unparsed_is_failed" to handle (2) above. If enabled,
this requires that every available source be parsed validly by at least
one plugin. In an inventory comprising a static hosts file and ec2.py,
this setting will cause a fatal error if ec2.py fails (a situation that
attracted only a warning earlier).

We clarify that the existing "unparsed_is_failed=true" setting causes a
fatal error only in (3) above, i.e., if NO inventory source could be
parsed. In other words, if there is ANY valid source in the inventory
(e.g., an ini-format static file), no combination of errors and the
setting will cause a fatal error.

If you want to execute your playbooks when your inventory is…

    (a) complete, use "any_unparsed_is_failed=true".
    (b) not empty, use "unparsed_is_failed=true".

The "unparsed_is_failed" setting should be renamed to
"all_unparsed_is_failed", but this commit does not do so.

Fixes #40512
Fixes #40996
6 years ago
Andrew Gaffney 1cf07028d4
Don't display host pattern warning for empty groups (fixes #35255) (#39927) 6 years ago
Brian Coca d9430b79f9
Ensure non ansilbe exceptions keep tracebacks (#37523) 6 years ago
Brian Coca 2e852fcd6d return empty host list when pattern is empty
fixes #37894
6 years ago
Toshio Kuratomi e707e71ec5 Except in specific cases, we want to use text strings on the controller 6 years ago
Brian Coca ef40e5e3b2 protect against plugins using verify incorrectly
assume false on any errors
6 years ago
Brian Coca fdbd1297cf catch all cases of no inventory when asked to fail
fixes #36034
6 years ago
Sloane Hertel 4a1cc661c4 add inventory caching & use in virtualbox inventory plugin (#34510)
* Inventory caching

* Add inventory caching for virtualbox

* Don't populate cache for virtualbox with stdout, use a dict of inventory instead

* Fix error creating the cache dir if it doesn't exist

* Keep cache default False and set to True in VariableManager __init__

* Check all groups before determining if a host is ungrouped.
6 years ago
Brian Coca 87c75b19dd
dont warn on not matching 'all' (#32806)
* dont warn on not matching 'all'

the implicit localhost warning shoudl be enough

* centralized no hosts handling

also extended info on implicit only
7 years ago
Brian Coca e50f931cf3 restore hostpattern regex/glob behaviour
they are back to matching both groups and hosts when they are a glob/regex

fixes #32906
7 years ago