Commit graph

556 commits

Author SHA1 Message Date
Abhijeet Kasurde 4b347415fa
inventory: misc typo fixes (#73695)
* Misc typo fixes

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
2021-02-23 10:42:20 -05:00
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>
2021-02-22 16:18:23 -05:00
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
2021-02-12 11:14:50 -05:00
Brian Coca 1e27d4052a
fix inventory source parse error handling (#73160)
fixes #51025

added test cases
2021-01-18 09:33:51 -05:00
Sloane Hertel 5e03e322de
Pass the top level dictionaries to combine_vars (#72979)
* Pass the top level dictionaries to combine_vars

combine_vars uses dict.update() to replace keys
2020-12-16 11:23:23 -05:00
Evgeni Golov a10af345a9
don't set warn = True, it will be overwritten in the next line (#71682) 2020-09-12 02:58:09 -05:00
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
2020-09-02 15:42:09 -05:00
Brian Coca 22da903e9c
return changed for group_by (#32057)
* return changed for group_by

* added tests and fixed 'early registeration'
2020-06-03 12:46:40 -04:00
Martin Krizek 6086ea62ee
Remove deprecation for TRANSFORM_INVALID_GROUP_CHARS (#66650)
Fixes #61889
2020-05-13 10:16:32 -04:00
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>
2020-05-07 21:42:48 -05:00
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.
2020-05-06 09:56:40 -07:00
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>
2019-12-12 09:09:40 +05:30
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>
2019-11-04 11:41:14 -05:00
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.
2019-09-20 16:03:51 -04:00
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>
2019-07-30 13:02:17 -04:00
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
2019-07-22 14:25:20 -05:00
Abhijeet Kasurde 4b152235ff
Typo fixes (#59227)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
2019-07-19 18:18:14 +05:30
Brian Coca 48d4d6ec69
Stop ignoring merge hash behaviour in inventory (#58460)
* stop ignoring merge hash behaviour in inventory

fixes #58120

* added porting note

Co-Authored-By: Alicia Cozine <879121+acozine@users.noreply.github.com>
2019-07-17 11:16:56 -04:00
Matt Martz 728fce0c44 Perf improvement for Templar.is_template (#57489)
* Faster is_template
2019-06-06 15:49:26 -04:00
Paul Gear 99f9f49eca Correct grammar (#57385)
"it's" == "it is"
"its" == "belonging to it"
2019-06-06 15:05:01 -04:00
James Cassell 1fb0335243 group_chars: default change warning only when needed 2019-06-04 14:04:17 -07:00
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) 2019-05-22 13:56:47 -04:00
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
2019-04-18 15:54:03 -04:00
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
2019-04-09 09:32:11 -07:00
Sebastian Meyer 8d7efa7d32 Strip whitespace when reading limit file (#53564)
Fix #17088
2019-03-18 12:40:45 -04:00
Brian Coca 9b67219096
Fine tune sanity (#53544)
* modify regex to use implicit charsets this should solve issues in py3 and unicode names
* fix issue with subgroups in yaml inventory
* clarify deprecation message
* separated per name warning from deprecation
* move noise to verbosity, simplify warnings
* fix docs to reflect actual 'good' practice
* change toggle to choice list to give users more options
2019-03-11 15:12:14 -04:00
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
2019-03-06 12:12:35 -06:00
Brian Coca d241794daa
Add toggle to control invalid character substitution in group names (#52748)
* make add_group return proper name
* ensure central transform/check
* added 'silent' option to avoid spamming current users
  those already using the plugins were used to the transformations, so no need to alert them
* centralized valid var names
* dont display dupes
* comment on regex
* added regex tests
  ini and script will now warn about deprecation
* more complete errormsg
2019-03-06 11:49:40 -05:00
Alan Rominger 62feba3d15 support deterministic host ordering from group ancestors (#44067)
Fixes #44065
2019-02-20 10:41:25 -05:00
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)
2019-01-30 19:13:19 -05:00
Brian Coca 9a1c291d01 add toggle for host pattern mismatch behaviour (#51199)
* add toggle for host pattern mismatch behaviour

	fixes #40030

* fix
2019-01-25 16:00:28 -05:00
Brian Coca 8fd0fbe431 remove usless tb initiator
always pass proper tb
2019-01-22 17:33:20 -05:00
AlanCoding f018b9d07d Fix missing tracebacks in ansible-inventory
(cherry picked from commit b62693299b)
2019-01-22 17:33:20 -05:00
Abhijeet Kasurde 4817dcd0fc Minor typos (#50371)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
2019-01-15 11:37:00 +00:00
Matt Martz a0d71e7735
Fix reverse_inventory order to work on python3 (#49895) 2018-12-17 15:39:29 -06:00
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.
2018-12-11 15:17:05 -05:00
Brian Coca ae2467ddbc Merge pull request #49080 from bcoca/better_invp_skip
more precise pluing skip msg and now in higher verbosity level
2018-11-27 15:30:14 -08:00
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
2018-11-20 17:06:51 -06:00
Martin Krizek 86be056633 inventory: fix hostname check (#48789) 2018-11-16 09:02:08 -05:00
Brian Coca 05246f7db8 inventory now errors for invalid group/host names (#45499)
* inventory now errors for invalid group/host names

also made yaml inventory slightly smarte

fixes #45493

* add some 'YAML protection' to ini plugin

* better msg

* avoid ranges as positive match

* pepe

* expand inherited instead of total override
2018-11-15 16:29:40 -05:00
Brian Coca 903cfa63cb
make inventory warnings a bit smarter (#46284)
less annoying for common cases
 add comment for 'tricky' conditional
2018-11-07 11:09:32 -05:00
Monty Taylor c5b303fa52 Test host in inventory by name in remove_host (#45639)
The host argument is a Host object, and is used as such by
group.remove_host. However, self.hosts is a dictionary of host name to
Host object. Thus, the existing code is checking to see if the Host
object is one of the keys.

Use host.name to interact with the keys of the dictionary.
2018-11-06 08:19:58 -05:00
Brian Coca 9e0c2a658f
better information for user from inventory plugins (#46766)
* better information for user from inventory plugins

 - use foreman as example
2018-10-12 11:27:29 -04:00
Ken Dreyer e46ce1619f add docstring for ansible.inventory.helpers.get_group_vars
Document the expected parameter type and return type of
get_group_vars().
2018-09-14 16:02:45 -04:00
Brian Coca 12a8363fae
Error on empty group/host name (#42584)
* error on false host/group name
2018-07-12 09:59:00 -04:00
Brian Coca 937e710485
ensure 'text' source assumptions (#42522)
* ensure 'text' source assumptions
2018-07-10 09:45:37 -04:00
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
2018-07-09 12:24:51 -04:00
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
2018-06-29 11:48:35 -04:00
Brian Coca 89f169127b added missing raise 2018-06-19 16:21:16 -04:00
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
2018-06-06 09:28:58 +05:30