Initial commit
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
# (c) 2020 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
DOCUMENTATION = """
|
||||
lookup: tower_api
|
||||
author: John Westcott IV (@john-westcott-iv)
|
||||
short_description: Search the API for objects
|
||||
requirements:
|
||||
- None
|
||||
description:
|
||||
- Returns GET requests from the Ansible Tower API. See
|
||||
U(https://docs.ansible.com/ansible-tower/latest/html/towerapi/index.html) for API usage.
|
||||
- For use that is cross-compatible between the awx.awx and ansible.tower collection
|
||||
see the tower_meta module
|
||||
extends_documentation_fragment: awx.awx.auth_plugin
|
||||
options:
|
||||
_terms:
|
||||
description:
|
||||
- The endpoint to query, i.e. teams, users, tokens, job_templates, etc.
|
||||
required: True
|
||||
query_params:
|
||||
description:
|
||||
- The query parameters to search for in the form of key/value pairs.
|
||||
type: dict
|
||||
required: False
|
||||
aliases: [query, data, filter, params]
|
||||
expect_objects:
|
||||
description:
|
||||
- Error if the response does not contain either a detail view or a list view.
|
||||
type: boolean
|
||||
default: False
|
||||
aliases: [expect_object]
|
||||
expect_one:
|
||||
description:
|
||||
- Error if the response contains more than one object.
|
||||
type: boolean
|
||||
default: False
|
||||
return_objects:
|
||||
description:
|
||||
- If a list view is returned, promote the list of results to the top-level of list returned.
|
||||
- Allows using this lookup plugin to loop over objects without additional work.
|
||||
type: boolean
|
||||
default: True
|
||||
return_all:
|
||||
description:
|
||||
- If the response is paginated, return all pages.
|
||||
type: boolean
|
||||
default: False
|
||||
return_ids:
|
||||
description:
|
||||
- If response contains objects, promote the id key to the top-level entries in the list.
|
||||
- Allows looking up a related object and passing it as a parameter to another module.
|
||||
- This will convert the return to a string or list of strings depending on the number of selected items.
|
||||
type: boolean
|
||||
aliases: [return_id]
|
||||
default: False
|
||||
max_objects:
|
||||
description:
|
||||
- if C(return_all) is true, this is the maximum of number of objects to return from the list.
|
||||
- If a list view returns more an max_objects an exception will be raised
|
||||
type: integer
|
||||
default: 1000
|
||||
|
||||
notes:
|
||||
- If the query is not filtered properly this can cause a performance impact.
|
||||
"""
|
||||
|
||||
EXAMPLES = """
|
||||
- name: Load the UI settings
|
||||
set_fact:
|
||||
tower_settings: "{{ lookup('awx.awx.tower_api', 'settings/ui') }}"
|
||||
|
||||
- name: Load the UI settings specifying the connection info
|
||||
set_fact:
|
||||
tower_settings: "{{ lookup('awx.awx.tower_api', 'settings/ui' host='tower.example.com', username='admin', password=my_pass_var, verify_ssl=False) }}"
|
||||
|
||||
- name: Report the usernames of all users with admin privs
|
||||
debug:
|
||||
msg: "Admin users: {{ query('awx.awx.tower_api', 'users', query_params={ 'is_superuser': true }) | map(attribute='username') | join(', ') }}"
|
||||
|
||||
- name: debug all organizations in a loop # use query to return a list
|
||||
debug:
|
||||
msg: "Organization description={{ item['description'] }} id={{ item['id'] }}"
|
||||
loop: "{{ query('awx.awx.tower_api', 'organizations') }}"
|
||||
loop_control:
|
||||
label: "{{ item['name'] }}"
|
||||
|
||||
- name: Make sure user 'john' is an org admin of the default org if the user exists
|
||||
tower_role:
|
||||
organization: Default
|
||||
role: admin
|
||||
user: john
|
||||
when: "lookup('awx.awx.tower_api', 'users', query_params={ 'username': 'john' }) | length == 1"
|
||||
|
||||
- name: Create an inventory group with all 'foo' hosts
|
||||
tower_group:
|
||||
name: "Foo Group"
|
||||
inventory: "Demo Inventory"
|
||||
hosts: >-
|
||||
{{ query(
|
||||
'awx.awx.tower_api',
|
||||
'hosts',
|
||||
query_params={ 'name__startswith' : 'foo', },
|
||||
) | map(attribute='name') | list }}
|
||||
register: group_creation
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
_raw:
|
||||
description:
|
||||
- Response from the API
|
||||
type: dict
|
||||
returned: on successful request
|
||||
"""
|
||||
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.utils.display import Display
|
||||
from ..module_utils.tower_api import TowerAPIModule
|
||||
|
||||
|
||||
class LookupModule(LookupBase):
|
||||
display = Display()
|
||||
|
||||
def handle_error(self, **kwargs):
|
||||
raise AnsibleError(to_native(kwargs.get('msg')))
|
||||
|
||||
def warn_callback(self, warning):
|
||||
self.display.warning(warning)
|
||||
|
||||
def run(self, terms, variables=None, **kwargs):
|
||||
if len(terms) != 1:
|
||||
raise AnsibleError('You must pass exactly one endpoint to query')
|
||||
|
||||
self.set_options(direct=kwargs)
|
||||
|
||||
# Defer processing of params to logic shared with the modules
|
||||
module_params = {}
|
||||
for plugin_param, module_param in TowerAPIModule.short_params.items():
|
||||
opt_val = self.get_option(plugin_param)
|
||||
if opt_val is not None:
|
||||
module_params[module_param] = opt_val
|
||||
|
||||
# Create our module
|
||||
module = TowerAPIModule(
|
||||
argument_spec={}, direct_params=module_params,
|
||||
error_callback=self.handle_error, warn_callback=self.warn_callback
|
||||
)
|
||||
|
||||
response = module.get_endpoint(terms[0], data=self.get_option('query_params', {}))
|
||||
|
||||
if 'status_code' not in response:
|
||||
raise AnsibleError("Unclear response from API: {0}".format(response))
|
||||
|
||||
if response['status_code'] != 200:
|
||||
raise AnsibleError("Failed to query the API: {0}".format(response['json'].get('detail', response['json'])))
|
||||
|
||||
return_data = response['json']
|
||||
|
||||
if self.get_option('expect_objects') or self.get_option('expect_one'):
|
||||
if ('id' not in return_data) and ('results' not in return_data):
|
||||
raise AnsibleError(
|
||||
'Did not obtain a list or detail view at {0}, and '
|
||||
'expect_objects or expect_one is set to True'.format(terms[0])
|
||||
)
|
||||
|
||||
if self.get_option('expect_one'):
|
||||
if 'results' in return_data and len(return_data['results']) != 1:
|
||||
raise AnsibleError(
|
||||
'Expected one object from endpoint {0}, '
|
||||
'but obtained {1} from API'.format(terms[0], len(return_data['results']))
|
||||
)
|
||||
|
||||
if self.get_option('return_all') and 'results' in return_data:
|
||||
if return_data['count'] > self.get_option('max_objects'):
|
||||
raise AnsibleError(
|
||||
'List view at {0} returned {1} objects, which is more than the maximum allowed '
|
||||
'by max_objects, {2}'.format(terms[0], return_data['count'], self.get_option('max_objects'))
|
||||
)
|
||||
|
||||
next_page = return_data['next']
|
||||
while next_page is not None:
|
||||
next_response = module.get_endpoint(next_page)
|
||||
return_data['results'] += next_response['json']['results']
|
||||
next_page = next_response['json']['next']
|
||||
return_data['next'] = None
|
||||
|
||||
if self.get_option('return_ids'):
|
||||
if 'results' in return_data:
|
||||
return_data['results'] = [str(item['id']) for item in return_data['results']]
|
||||
elif 'id' in return_data:
|
||||
return_data = str(return_data['id'])
|
||||
|
||||
if self.get_option('return_objects') and 'results' in return_data:
|
||||
return return_data['results']
|
||||
else:
|
||||
return [return_data]
|
||||
@@ -0,0 +1,248 @@
|
||||
# (c) 2020 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
DOCUMENTATION = """
|
||||
lookup: tower_schedule_rrule
|
||||
author: John Westcott IV (@john-westcott-iv)
|
||||
short_description: Generate an rrule string which can be used for Tower Schedules
|
||||
requirements:
|
||||
- pytz
|
||||
- python.dateutil >= 2.7.0
|
||||
description:
|
||||
- Returns a string based on criteria which represents an rrule
|
||||
options:
|
||||
_terms:
|
||||
description:
|
||||
- The frequency of the schedule
|
||||
- none - Run this schedule once
|
||||
- minute - Run this schedule every x minutes
|
||||
- hour - Run this schedule every x hours
|
||||
- day - Run this schedule every x days
|
||||
- week - Run this schedule weekly
|
||||
- month - Run this schedule monthly
|
||||
required: True
|
||||
choices: ['none', 'minute', 'hour', 'day', 'week', 'month']
|
||||
start_date:
|
||||
description:
|
||||
- The date to start the rule
|
||||
- Used for all frequencies
|
||||
- Format should be YYYY-MM-DD [HH:MM:SS]
|
||||
type: str
|
||||
timezone:
|
||||
description:
|
||||
- The timezone to use for this rule
|
||||
- Used for all frequencies
|
||||
- Format should be as US/Eastern
|
||||
- Defaults to America/New_York
|
||||
type: str
|
||||
every:
|
||||
description:
|
||||
- The repetition in months, weeks, days hours or minutes
|
||||
- Used for all types except none
|
||||
type: int
|
||||
end_on:
|
||||
description:
|
||||
- How to end this schedule
|
||||
- If this is not defined, this schedule will never end
|
||||
- If this is a positive integer, this schedule will end after this number of occurences
|
||||
- If this is a date in the format YYYY-MM-DD [HH:MM:SS], this schedule ends after this date
|
||||
- Used for all types except none
|
||||
type: str
|
||||
on_days:
|
||||
description:
|
||||
- The days to run this schedule on
|
||||
- A comma-separated list which can contain values sunday, monday, tuesday, wednesday, thursday, friday
|
||||
- Used for week type schedules
|
||||
month_day_number:
|
||||
description:
|
||||
- The day of the month this schedule will run on (0-31)
|
||||
- Used for month type schedules
|
||||
- Cannot be used with on_the parameter
|
||||
type: int
|
||||
on_the:
|
||||
description:
|
||||
- A description on when this schedule will run
|
||||
- Two strings separated by a space
|
||||
- First string is one of first, second, third, fourth, last
|
||||
- Second string is one of sunday, monday, tuesday, wednesday, thursday, friday
|
||||
- Used for month type schedules
|
||||
- Cannot be used with month_day_number parameters
|
||||
"""
|
||||
|
||||
EXAMPLES = """
|
||||
- name: Create a string for a schedule
|
||||
debug:
|
||||
msg: "{{ query('awx.awx.tower_schedule_rrule', 'none', start_date='1979-09-13 03:45:07') }}"
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
_raw:
|
||||
description:
|
||||
- String in the rrule format
|
||||
type: string
|
||||
"""
|
||||
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.errors import AnsibleError
|
||||
from datetime import datetime
|
||||
import re
|
||||
from distutils.version import LooseVersion
|
||||
|
||||
missing_modules = []
|
||||
try:
|
||||
import pytz
|
||||
except ImportError:
|
||||
missing_modules.append('pytz')
|
||||
|
||||
try:
|
||||
from dateutil import rrule
|
||||
except ImportError:
|
||||
missing_modules.append('python.dateutil')
|
||||
|
||||
# Validate the version of python.dateutil
|
||||
try:
|
||||
import dateutil
|
||||
if LooseVersion(dateutil.__version__) < LooseVersion("2.7.0"):
|
||||
raise Exception
|
||||
except Exception:
|
||||
missing_modules.append('python.dateutil>=2.7.0')
|
||||
|
||||
if len(missing_modules) > 0:
|
||||
raise AnsibleError('You are missing the modules {0}'.format(', '.join(missing_modules)))
|
||||
|
||||
|
||||
class LookupModule(LookupBase):
|
||||
frequencies = {
|
||||
'none': rrule.DAILY,
|
||||
'minute': rrule.MINUTELY,
|
||||
'hour': rrule.HOURLY,
|
||||
'day': rrule.DAILY,
|
||||
'week': rrule.WEEKLY,
|
||||
'month': rrule.MONTHLY,
|
||||
}
|
||||
|
||||
weekdays = {
|
||||
'monday': rrule.MO,
|
||||
'tuesday': rrule.TU,
|
||||
'wednesday': rrule.WE,
|
||||
'thursday': rrule.TH,
|
||||
'friday': rrule.FR,
|
||||
'saturday': rrule.SA,
|
||||
'sunday': rrule.SU,
|
||||
}
|
||||
|
||||
set_positions = {
|
||||
'first': 1,
|
||||
'second': 2,
|
||||
'third': 3,
|
||||
'fourth': 4,
|
||||
'last': -1,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def parse_date_time(date_string):
|
||||
try:
|
||||
return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
|
||||
except ValueError:
|
||||
return datetime.strptime(date_string, '%Y-%m-%d')
|
||||
|
||||
def run(self, terms, variables=None, **kwargs):
|
||||
if len(terms) != 1:
|
||||
raise AnsibleError('You may only pass one schedule type in at a time')
|
||||
|
||||
frequency = terms[0].lower()
|
||||
|
||||
return self.get_rrule(frequency, kwargs)
|
||||
|
||||
@staticmethod
|
||||
def get_rrule(frequency, kwargs):
|
||||
|
||||
if frequency not in LookupModule.frequencies:
|
||||
raise AnsibleError('Frequency of {0} is invalid'.format(frequency))
|
||||
|
||||
rrule_kwargs = {
|
||||
'freq': LookupModule.frequencies[frequency],
|
||||
'interval': kwargs.get('every', 1),
|
||||
}
|
||||
|
||||
# All frequencies can use a start date
|
||||
if 'start_date' in kwargs:
|
||||
try:
|
||||
rrule_kwargs['dtstart'] = LookupModule.parse_date_time(kwargs['start_date'])
|
||||
except Exception:
|
||||
raise AnsibleError('Parameter start_date must be in the format YYYY-MM-DD [HH:MM:SS]')
|
||||
|
||||
# If we are a none frequency we don't need anything else
|
||||
if frequency == 'none':
|
||||
rrule_kwargs['count'] = 1
|
||||
else:
|
||||
# All non-none frequencies can have an end_on option
|
||||
if 'end_on' in kwargs:
|
||||
end_on = kwargs['end_on']
|
||||
if re.match(r'^\d+$', end_on):
|
||||
rrule_kwargs['count'] = end_on
|
||||
else:
|
||||
try:
|
||||
rrule_kwargs['until'] = LookupModule.parse_date_time(end_on)
|
||||
except Exception:
|
||||
raise AnsibleError('Parameter end_on must either be an integer or in the format YYYY-MM-DD [HH:MM:SS]')
|
||||
|
||||
# A week-based frequency can also take the on_days parameter
|
||||
if frequency == 'week' and 'on_days' in kwargs:
|
||||
days = []
|
||||
for day in kwargs['on_days'].split(','):
|
||||
day = day.strip()
|
||||
if day not in LookupModule.weekdays:
|
||||
raise AnsibleError('Parameter on_days must only contain values {0}'.format(', '.join(LookupModule.weekdays.keys())))
|
||||
days.append(LookupModule.weekdays[day])
|
||||
|
||||
rrule_kwargs['byweekday'] = days
|
||||
|
||||
# A month-based frequency can also deal with month_day_number and on_the options
|
||||
if frequency == 'month':
|
||||
if 'month_day_number' in kwargs and 'on_the' in kwargs:
|
||||
raise AnsibleError('Month based frequencies can have month_day_number or on_the but not both')
|
||||
|
||||
if 'month_day_number' in kwargs:
|
||||
try:
|
||||
my_month_day = int(kwargs['month_day_number'])
|
||||
if my_month_day < 1 or my_month_day > 31:
|
||||
raise Exception()
|
||||
except Exception:
|
||||
raise AnsibleError('month_day_number must be between 1 and 31')
|
||||
|
||||
rrule_kwargs['bymonthday'] = my_month_day
|
||||
|
||||
if 'on_the' in kwargs:
|
||||
try:
|
||||
(occurance, weekday) = kwargs['on_the'].split(' ')
|
||||
except Exception:
|
||||
raise AnsibleError('on_the parameter must be two words separated by a space')
|
||||
|
||||
if weekday not in LookupModule.weekdays:
|
||||
raise AnsibleError('Weekday portion of on_the parameter is not valid')
|
||||
if occurance not in LookupModule.set_positions:
|
||||
raise AnsibleError('The first string of the on_the parameter is not valid')
|
||||
|
||||
rrule_kwargs['byweekday'] = LookupModule.weekdays[weekday]
|
||||
rrule_kwargs['bysetpos'] = LookupModule.set_positions[occurance]
|
||||
|
||||
my_rule = rrule.rrule(**rrule_kwargs)
|
||||
|
||||
# All frequencies can use a timezone but rrule can't support the format that Tower uses.
|
||||
# So we will do a string manip here if we need to
|
||||
timezone = 'America/New_York'
|
||||
if 'timezone' in kwargs:
|
||||
if kwargs['timezone'] not in pytz.all_timezones:
|
||||
raise AnsibleError('Timezone parameter is not valid')
|
||||
timezone = kwargs['timezone']
|
||||
|
||||
# rrule puts a \n in the rule instad of a space and can't handle timezones
|
||||
return_rrule = str(my_rule).replace('\n', ' ').replace('DTSTART:', 'DTSTART;TZID={0}:'.format(timezone))
|
||||
# Tower requires an interval. rrule will not add interval if it's set to 1
|
||||
if kwargs.get('every', 1) == 1:
|
||||
return_rrule = "{0};INTERVAL=1".format(return_rrule)
|
||||
|
||||
return return_rrule
|
||||
Reference in New Issue
Block a user