2015-01-09 15:37:31 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
# (c) 2013, Steven Dossett <sdossett@panath.com>
|
2017-09-19 14:49:07 +00:00
|
|
|
# (c) 2017 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2015-01-09 15:37:31 +00:00
|
|
|
|
2015-04-13 20:28:01 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-09-19 14:49:07 +00:00
|
|
|
DOCUMENTATION = """
|
|
|
|
lookup: inventory_hostnames
|
|
|
|
author:
|
|
|
|
- Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
- Steven Dossett <sdossett@panath.com>
|
|
|
|
version_added: "1.3"
|
|
|
|
short_description: list of inventory hosts matching a host pattern
|
|
|
|
description:
|
2018-06-13 06:22:42 +00:00
|
|
|
- "This lookup understands 'host patterns' as used by the `hosts:` keyword in plays
|
2017-09-19 14:49:07 +00:00
|
|
|
and can return a list of matching hosts from inventory"
|
|
|
|
notes:
|
|
|
|
- this is only worth for 'hostname patterns' it is easier to loop over the group/group_names variables otherwise.
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2018-06-13 06:22:42 +00:00
|
|
|
- name: show all the hosts matching the pattern, i.e. all but the group www
|
2017-09-19 14:49:07 +00:00
|
|
|
debug:
|
|
|
|
msg: "{{ item }}"
|
|
|
|
with_inventory_hostnames:
|
|
|
|
- all:!www
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
_hostnames:
|
|
|
|
description: list of hostnames that matched the host pattern in inventory
|
|
|
|
type: list
|
|
|
|
"""
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
from ansible.inventory.manager import split_host_pattern, order_patterns
|
2017-06-02 11:14:11 +00:00
|
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
|
2015-01-09 15:37:31 +00:00
|
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
|
|
|
2015-10-15 15:19:11 +00:00
|
|
|
def get_hosts(self, variables, pattern):
|
|
|
|
hosts = []
|
2017-06-02 11:14:11 +00:00
|
|
|
if pattern[0] in ('!', '&'):
|
2016-01-20 23:31:40 +00:00
|
|
|
obj = pattern[1:]
|
|
|
|
else:
|
|
|
|
obj = pattern
|
|
|
|
|
|
|
|
if obj in variables['groups']:
|
|
|
|
hosts = variables['groups'][obj]
|
|
|
|
elif obj in variables['groups']['all']:
|
|
|
|
hosts = [obj]
|
2015-10-15 15:19:11 +00:00
|
|
|
return hosts
|
2015-01-09 15:37:31 +00:00
|
|
|
|
2015-10-15 15:19:11 +00:00
|
|
|
def run(self, terms, variables=None, **kwargs):
|
2015-01-09 15:37:31 +00:00
|
|
|
|
2015-10-15 15:19:11 +00:00
|
|
|
host_list = []
|
|
|
|
|
|
|
|
for term in terms:
|
2017-05-23 21:16:49 +00:00
|
|
|
patterns = order_patterns(split_host_pattern(term))
|
2015-10-15 15:19:11 +00:00
|
|
|
|
|
|
|
for p in patterns:
|
|
|
|
that = self.get_hosts(variables, p)
|
|
|
|
if p.startswith("!"):
|
2017-06-02 11:14:11 +00:00
|
|
|
host_list = [h for h in host_list if h not in that]
|
2015-10-15 15:19:11 +00:00
|
|
|
elif p.startswith("&"):
|
2017-06-02 11:14:11 +00:00
|
|
|
host_list = [h for h in host_list if h in that]
|
2015-10-15 15:19:11 +00:00
|
|
|
else:
|
|
|
|
host_list.extend(that)
|
|
|
|
|
|
|
|
# return unique list
|
|
|
|
return list(set(host_list))
|