Commit Graph

248 Commits (66eb5681f4c3e3cc45416a537c55974b25bd5402)

Author SHA1 Message Date
James Cammarata c753ae6e22 Fixing up host pattern caching to avoid bugs
* Always cache and return unique list objects, so that if the list
  is changed later it does not impact the cached results
* Take additional parameters and the type of the pattern into account
  when building the hash string
2015-11-09 20:55:13 -05:00
James Cammarata 5040abaaf2 Merge branch 'devel_cache_for_do_template_call' of https://github.com/Yannig/ansible into Yannig-devel_cache_for_do_template_call 2015-11-06 22:04:52 -05:00
James Cammarata 63c47fb271 Fixing up performance 2015-11-05 16:22:37 -05:00
Brian Coca 483c25a609 Revert "switched host patterns to use sets, simplified logic which now uses buitins"
need this to be lists, for order and other considerations
This reverts commit 8e2f0b3f2c.
2015-11-05 15:41:17 -05:00
Brian Coca 88ba46aee0 Revert "fixed append to add"
good change on top of bad
This reverts commit 699f344e17.
2015-11-05 15:40:52 -05:00
Brian Coca 699f344e17 fixed append to add 2015-11-05 15:24:20 -05:00
Brian Coca 8e2f0b3f2c switched host patterns to use sets, simplified logic which now uses buitins 2015-11-05 15:02:06 -05:00
Yannig Perre e0aa3ff232 Cache against hosts pattern (fix a part of problem describe in https://github.com/ansible/ansible/issues/13023). 2015-11-04 22:15:02 +01:00
Brian Coca 13bf62d0e6 remove deprecation for :, add ; functionality (its deprecated but it should work) 2015-10-31 16:35:40 -04:00
James Cammarata 86de1429e5 Cleaning up FIXMEs 2015-10-22 16:03:50 -04:00
Toshio Kuratomi a17ae6f07e Clean up pyflakes warnings in inventory 2015-10-19 11:13:56 -07:00
Brian Coca 6d87bd405d Merge pull request #12765 from bcoca/lookup_inventory_hostname
implemented inventory_hostname lookup in v2
2015-10-16 15:05:23 -04:00
Toshio Kuratomi baa309309d Bundle a new version of python-six for compatibility along with some code to make it easy for distributions to override the bunndled copy if they have a new enough version. 2015-10-16 08:21:28 -07:00
Brian Coca 9b088a1e40 implemented inventory_hostname lookup in v2 2015-10-15 13:11:42 -04:00
James Cammarata 91b0b4d40c Check is_directory() before not is_file() in inventory basedir
Fixes #12742
2015-10-14 11:03:10 -04:00
James Cammarata e6d3c6745f Revert to using local file/dir tests in inventory instead of loader's
Fixes #12719
2015-10-13 12:04:01 -04:00
Brian Coca c637d60a8d fixed deprecation message 2015-10-13 11:40:21 -04:00
James Cammarata 5a0f5f1254 Load vars for localhost when created implicitly
Fixes #12666
2015-10-08 09:26:20 -04:00
James Cammarata 57372906aa Pull host/group vars when adding hosts/groups
Fixes #12582
2015-10-02 02:47:09 -04:00
Marius Gedminas 95e655eb67 Python 3: there's no basestring
Fixes one failing test.

The long series of module_utils/basic.py fixes were all because
module_utils/basic is imported in ansible/inventory/script.py.
2015-09-23 10:04:26 +03:00
Victor Salgado 20bbd66e85 Use dict.get() 2015-09-20 22:54:02 -03:00
James Cammarata 65bf14cbb0 Merge pull request #12431 from hslee16/devel
Stops using _groups_list to check for host when using 'add_host'
2015-09-20 18:17:02 -04:00
Abhijit Menon-Sen 2fcdb37e7b Support «hosts: groupname[1:]» notation (~= 'the rest of the group') 2015-09-18 22:28:34 +05:30
Alex Lee cf289191e5 return None if group does not exist in inventory groups 2015-09-17 15:58:10 -07:00
James Cammarata b386f0c64d Merge pull request #12423 from amenonsen/split-fixup
Remove deprecation warning; now handled in _split_patterns
2015-09-17 14:02:22 -04:00
James Cammarata a1fffff92c Merge branch 'addresses' of https://github.com/amenonsen/ansible into amenonsen-addresses 2015-09-17 13:43:05 -04:00
Abhijit Menon-Sen 62ca6b5e35 Remove deprecation warning; now handled in _split_patterns 2015-09-17 23:09:56 +05:30
Abhijit Menon-Sen 575a8b8c77 Merge Host.ipv[46]_address into .address
The earlier distinction was never used; .ipv6_address was always a copy
of .ipv4_address, and the latter was always used to set the remote_addr
field in the PlayContext.

Also uses the canonical ansible_host/ansible_port names when setting the
address and port from variables.
2015-09-17 23:03:48 +05:30
Victor Salgado 14fefebaad Modify _split_pattern to use map when working with list input 2015-09-17 13:52:54 -03:00
Abhijit Menon-Sen 2405861a9e Use ',' instead of ':' or ';' to separate host patterns
The earlier-recommended "pat1:pat2:pat3[x:y]" notation doesn't work well
with IPv6 addresses, so we recommend ',' as a separator instead. We know
that commas can't occur within a pattern, so we can just split on it.

We still have to accept the "foo:bar" notation because it's so commonly
used, but we issue a deprecation warning for it.

Fixes #12296
Closes #12404
Closes #12329
2015-09-17 19:31:16 +05:30
James Cammarata e97d448838 Cleanup one more missed inventory bug 2015-09-16 16:53:08 -04:00
James Cammarata 11c27078c0 Cleaning up inventory bugs from earlier group change 2015-09-16 16:36:27 -04:00
James Cammarata c8f2483d6d Cleaning up default group creation in inventory parsers 2015-09-16 12:54:54 -04:00
Thomas Quinot c005f75469 Always create 'all' group
Create 'all' even if host_list is empty, as this group needs to exist
when creating the implicit 'localhost' host.

Fixes #12391
2015-09-16 12:24:06 +02:00
Abhijit Menon-Sen 065bb52109 Be systematic about parsing and validating hostnames and addresses
This adds a parse_address(pattern) utility function that returns
(host,port), and uses it wherever where we accept IPv4 and IPv6
addresses and hostnames (or host patterns): the inventory parser
the the add_host action plugin.

It also introduces a more extensive set of unit tests that supersedes
the old add_host unit tests (which didn't actually test add_host, but
only the parsing function).
2015-09-11 21:47:18 +05:30
James Cammarata ff9f5d7dc8 Starting to add additional unit tests for VariableManager
Required some rewiring in inventory code to make sure we're using
the DataLoader class for some data file operations, which makes mocking
them much easier.

Also identified two corner cases not currently handled by the code, related
to inventory variable sources and which one "wins". Also noticed we weren't
properly merging variables from multiple group/host_var file locations
(inventory directory vs. playbook directory locations) so fixed as well.
2015-09-04 16:41:38 -04:00
Abhijit Menon-Sen 147b04b682 Remove duplicated function definition (fallout from earlier repeated merge) 2015-09-02 07:47:48 +05:30
Brian Coca b525eedb8e less misleading message for when someone looks up vars for a host not in inventory 2015-08-30 12:41:21 -04:00
James Cammarata 4c65306e73 Merge branch 'hostrange' of https://github.com/amenonsen/ansible into amenonsen-hostrange 2015-08-28 13:48:25 -04:00
Brian Coca 514fa73fcd galaxy fixes 2015-08-28 12:32:50 -04:00
Marius Gedminas 0c6ce31f76 Use 'except ... as' syntax
This syntax works on Python 2.6 through 3.x.  lib/ansible/module_utils
(and lib/ansible/modules) need to support Python 2.4, so I didn't touch
those.
2015-08-27 22:15:04 +03:00
Ilya Epifanov 81bf88b6e0 fixed hostvars access in conjunction with --limit usage 2015-08-27 00:15:26 +03:00
Brian Coca b2ae6945c4 always load vars plugins
fixes #12104
2015-08-26 11:40:53 -04:00
Brian Coca 82603bb2a0 avoids running abspath on None 2015-08-26 09:28:23 -04:00
Abhijit Menon-Sen 8bf0dbb7a9 Use [x:y] host ranges instead of [x-y]
This commit deprecates the earlier groupname[x-y] syntax in favour of
the inclusive groupname[x:y] syntax. It also makes the subscripting
code simpler and adds explanatory comments.

One problem addressed by the cleanup is that _enumeration_info used to
be called twice, and its results discarded the first time because of the
convoluted control flow.
2015-08-25 21:17:24 +05:30
Abhijit Menon-Sen 73f10de386 Document the behaviour of _match_one_pattern in some detail
The possibilities are complicated enough that I didn't want to make
changes without having a complete description of what it actually
accepts/matches. Note that this text documents current behaviour, not
necessarily the behaviour we want. Some of this is undocumented and may
not be intended.
2015-08-25 21:17:24 +05:30
Abhijit Menon-Sen fa6ffa1dbd Remove & and ! pattern prefixes as early as possible
Now everything under _match_one_pattern can ignore them. This also means
that we can use the cache to return the same results for 'foo' and '!foo'.
2015-08-25 21:17:24 +05:30
Abhijit Menon-Sen 704c3815d3 Reorder functions into a logical sequence based on usage
There are no code changes; this is committed separately so as to make
the subsequent "real" diffs easier to read.
2015-08-25 21:17:24 +05:30
Brian Coca f1b8323b62 fixed host/group var loading when inventory is a directory 2015-08-23 17:51:47 -04:00
James Cammarata e282309f6d Make sure the inventory restriction is not None 2015-08-21 16:08:21 -04:00
Brian Coca 961bee00d5 centralized the definition of 'localhost' 2015-08-19 15:49:37 -04:00
Abhijit Menon-Sen 2641f70e47 Reset self._groups_cache in refresh_inventory 2015-08-19 11:35:27 +05:30
Harald Laabs 73d6da757f Make get_group run in O(n*log(n)) instead of O(n^2) by using a cache 2015-08-19 11:35:27 +05:30
Will West b8865314ec improve ansible start up time for very large inventory
first off, we add an oddly slow basic test of 10k item inventory

Before:
```
Ran 229 tests in 13.214s

OK

real    0m13.403s
user    0m12.106s
sys 0m1.155s
```

After:
```
Ran 230 tests in 21.328s

OK

real    0m21.516s
user    0m20.099s
sys 0m1.275s
```

since that seems like a bit long for the test to add to runtime, lets profile

`python -m cProfile -s time ./bin/ansible all -i test/units/inventory_test_data/huge_range --list-hosts`

Before:
```
         1272607 function calls (1259689 primitive calls) in 8.497 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    4.393    0.000    4.396    0.000 __init__.py:395(_get_host)
    20000    2.695    0.000    2.697    0.000 __init__.py:341(__append_host_to_results)
    40369    0.113    0.000    0.113    0.000 {posix.lstat}
    50006    0.102    0.000    0.153    0.000 __init__.py:1490(combine_vars)
    40008    0.089    0.000    0.202    0.000 __init__.py:1546(_load_vars_from_path)
    20195    0.088    0.000    0.088    0.000 {posix.stat}
    10011    0.087    0.000    0.087    0.000 {posix.getcwd}
```

The top two lines are promising optimization targets

- populate Inventory's host cache more in _get_host, as we are looping
  over all the groups anyways.

- eliminate duplicate check of whether we've already included a host
  in the construction around __append_host_to_results we can infer
  presence of a host in the results list implies the presence of its
  name in the hostnames set, allowing us to only to the less expensive
  of the two checks

After:
```
         1252610 function calls (1239692 primitive calls) in 1.320 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    40369    0.105    0.000    0.105    0.000 {posix.lstat}
    50006    0.094    0.000    0.141    0.000 __init__.py:1490(combine_vars)
    40008    0.081    0.000    0.184    0.000 __init__.py:1546(_load_vars_from_path)
    10011    0.080    0.000    0.080    0.000 {posix.getcwd}
    20195    0.074    0.000    0.074    0.000 {posix.stat}
    10002    0.069    0.000    0.261    0.000 __init__.py:1517(load_vars)
```
2015-08-19 09:45:02 +05:30
Abhijit Menon-Sen 8f231ec469 Tweak deprecation warning, and apply it to --limit also 2015-08-14 10:55:13 +05:30
James Cammarata 4dba30ccd0 Cleanup missed merge conflict sections 2015-08-13 15:05:58 -04:00
Abhijit Menon-Sen d41db8edd6 Don't accept undocumented "p1;p2" patterns; note in changelog
The correct (and now only) way to say p1 OR p2 is "p1:p2".
2015-08-13 14:54:12 -04:00
Abhijit Menon-Sen 1437b8e120 Introduce _split_pattern to parse an "x:y:z" pattern
This function takes a string like 'foo:bar[1:2]:baz[x:y]-quux' and
returns a list of patterns ['foo', 'bar[1:2]', 'baz[x:y]-quux'], i.e.
splits the string on colons that are not part of a range specification.
2015-08-13 14:54:12 -04:00
James Cammarata 32034feec5 Add deprecation warning regarding host patterns 2015-08-13 14:51:08 -04:00
Abhijit Menon-Sen 6bf6b9fdbb Rename some internal functions, clarify what they do
get_hosts → used externally, not changed
_get_hosts → _evaluate_patterns (takes a list, evaluates ! and &)
__get_hosts → _match_one_pattern (takes one pattern only, ignores !&)
2015-08-13 14:51:08 -04:00
Abhijit Menon-Sen eff33bac49 Don't accept undocumented "p1;p2" patterns; note in changelog
The correct (and now only) way to say p1 OR p2 is "p1:p2".
2015-08-13 14:51:08 -04:00
Abhijit Menon-Sen 8aaa06dd50 Introduce _split_pattern to parse an "x:y:z" pattern
This function takes a string like 'foo:bar[1:2]:baz[x:y]-quux' and
returns a list of patterns ['foo', 'bar[1:2]', 'baz[x:y]-quux'], i.e.
splits the string on colons that are not part of a range specification.
2015-08-13 14:51:08 -04:00
Abhijit Menon-Sen baf637b9ae Remove dead code: Inventory.also_restrict_to/lift_also_restriction
This was used earlier to implement serial, but that's now done using
restrict_to_hosts() (whose docstring is also suitably adjusted here)
and there are no more callers.
2015-08-13 14:51:08 -04:00
Brian Coca b905f51341 fixed typo 2015-08-13 11:05:20 -04:00
Brian Coca 49d88cef9c meta: refresh_inventory and several inventory fixes 2015-08-12 10:35:12 -04:00
James Cammarata 26d5a17b59 Make sure inventory loads files with non-relative paths
This is a slightly different fix than we originally committed, but fixes
the problem in a less invasive way (and I believe it's generally better
that we don't deal with relative paths internally past this point)

Fixes #11789
2015-08-06 10:18:58 -04:00
Brian Coca b9433650d1 Revert "Path of group_vars and host_vars were getting the basedir added twice."
in view of simpler solution incomming from james
This reverts commit bae7a02be5.
2015-08-06 10:09:43 -04:00
Toshio Kuratomi bae7a02be5 Path of group_vars and host_vars were getting the basedir added twice.
Fix inventory so this won't happen and fix DataLoader so that it will
test relative paths relative to self._basedir

Fixes #11789
2015-08-05 17:41:17 -07:00
Toshio Kuratomi f0431eaea9 Make ansible_python_interpreter work with synchronize
Fixes #11836
2015-08-03 13:32:24 -07:00
Abhijit Menon-Sen 845784b572 Remove outdated FIXMEs; all use combine_vars already 2015-07-30 23:47:41 +05:30
Brian Coca 77fc3ce759 removed unused import 2015-07-28 08:28:52 -04:00
James Cammarata 65ae9780fa Merge pull request #11079 from amegianeg/fix_broad_except_in_inventory
Avoid too broad except in Inventory
2015-07-23 13:36:02 -04:00
James Cammarata 4e94bb64d8 Fix group/host var loading relative to playbook basedir 2015-07-14 09:26:24 -04:00
James Cammarata 87ca475704 Exclude the all/ungrouped groups from pattern matching results
Fixes #5375
2015-06-17 16:25:58 -04:00
James Cammarata 90445ee67d Add ::1 where we see 127.0.0.1, for better ipv6 support
Fixes #5764
2015-06-17 16:03:19 -04:00
Marc Abramowitz a385744426 Add inventory file to "Unable to find" error msg
E.g.:

    $ ansible gabriel -m ping -i ssh_config.py
    ERROR! Unable to find an inventory file (ssh_config.py), specify one with -i ?
2015-06-17 11:00:13 -07:00
James Cammarata 30c1a2d861 Have group/host var file loading check for YAML extensions too
Fixes #11132
2015-06-16 11:55:56 -04:00
Toshio Kuratomi f9b56a5d7c Fix raising AnsibleError 2015-06-05 11:41:23 -07:00
James Cammarata 620fad9f8d Fixing an oops in inventory/__init__.py where the slots are incorrect 2015-06-01 20:02:15 -05:00
James Cammarata 2bad888f28 Merge branch 'v2_final' into devel_switch_v2
Conflicts:
	lib/ansible/inventory/__init__.py
	lib/ansible/modules/core
	lib/ansible/utils/__init__.py
	lib/ansible/utils/module_docs.py
2015-05-28 15:26:03 -05:00
alberto 37ae5aab31 Capture only IOError when reading shebang from inventory file, to avoid ignoring other possible exceptions like timeouts from a task 2015-05-28 12:19:32 +02:00
Brian Coca 0913b8263c made special treatment of certain filesystem for selinux configurable 2015-05-15 13:52:27 -04:00
Brian Coca e2de336a23 made special treatment of certain filesystem for selinux configurable 2015-05-15 13:19:15 -04:00
James Cammarata ce3ef7f4c1 Making the switch to v2 2015-05-03 21:47:26 -05:00
Brian Coca 61a30e5f49 better exception handling with delegated hosts 2014-12-03 07:26:42 -05:00
James Cammarata d19fe8d95d Fetch vars for host directly when calculating the delegated user
This fixes the case in which the delegated to host may not be in the
specified hosts list, in which cases facts/vars for the host were
not available in the injected hostvars.

This also fixes the inventory variable fetching function, so that an
unknown host raises a proper error as opposed to a NoneType exception.

Fixes #8224
2014-10-10 01:18:18 -05:00
Serge van Ginderachter 0ab721d51e better logic for all-group variables for implicit localhost 2014-08-19 16:41:51 +02:00
Serge van Ginderachter a2cfe8770b Inventory: fix logic mistake in loading/retrieving variables for groups 2014-08-19 16:34:26 +02:00
James Cammarata e3895840d1 Make sure group_vars/all is loaded for ungrouped hosts
Fixes #6563
2014-08-18 12:29:46 -05:00
James Cammarata b4476c238f Use match() instead of search() for pattern matching
Also related to f48fa3, fix for #8614
2014-08-14 12:26:52 -05:00
James Cammarata f48fa3759a Fix host pattern matching and enhance error detection
Fixes #8614
2014-08-14 12:13:40 -05:00
James Cammarata bea0845322 Fix vault_password positional args error 2014-08-14 10:28:08 -05:00
Michael DeHaan 8c4161d4a1 Merge branch 'retf_py' of git://github.com/berendt/ansible into devel
Conflicts:
	lib/ansible/inventory/vars_plugins/group_vars.py
	plugins/callbacks/mail.py
2014-08-08 12:17:37 -04:00
Serge van Ginderachter e8b45a9ef7 Invalidate host/group variables cache when loading
extra varsiables based on playbook basedir.
2014-07-14 15:21:33 +02:00
Serge van Ginderachter d39ff55688 Fix logic bug when loading vars for a new playbook basedir. 2014-07-14 15:12:53 +02:00
James Cammarata 4e5eb7559e Merge branch 'svg_and_inventory_refactor' into devel 2014-07-10 14:43:07 -05:00
Dan Slimmon 307a3e8a9c Fixed regex square-bracket bug.
Regexes were being parsed like ordinary ansible host patterns, so
square-bracket groups were getting interpolated wrongly.
2014-07-10 00:08:12 +00:00
James Cammarata aa261bdd14 Optimizing groups_for_host() lookup in inventory 2014-07-07 21:08:39 -05:00
James Cammarata fe892fccb1 Merge remote-tracking branch 'svg/varsplugins_refactor' into svg_and_inventory_refactor 2014-07-07 14:16:37 -05:00
James Cammarata 11a5fc85f4 Merge branch 'inventory_refactoring' into svg_and_inventory_refactor 2014-07-07 13:11:58 -05:00