2018-06-05 08:32:54 +03:00
#!/usr/bin/python
# coding: utf-8 -*-
2018-09-19 17:02:27 +03:00
# Copyright: (c) 2018, Adrien Fleury <fleu42@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2018-06-05 08:32:54 +03:00
from __future__ import absolute_import , division , print_function
__metaclass__ = type
2020-02-05 21:24:46 +03:00
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.1 ' ,
' status ' : [ ' preview ' ] ,
' supported_by ' : ' community ' }
2018-06-05 08:32:54 +03:00
DOCUMENTATION = '''
- - -
module : tower_inventory_source
author : " Adrien Fleury (@fleu42) "
version_added : " 2.7 "
short_description : create , update , or destroy Ansible Tower inventory source .
description :
2020-02-05 21:24:46 +03:00
- Create , update , or destroy Ansible Tower inventory source . See
2018-06-05 08:32:54 +03:00
U ( https : / / www . ansible . com / tower ) for an overview .
options :
name :
description :
- The name to use for the inventory source .
required : True
2019-09-30 23:01:44 +03:00
type : str
2020-02-05 21:24:46 +03:00
new_name :
description :
- A new name for this assets ( will rename the asset )
required : False
type : str
2020-02-24 23:23:49 +03:00
version_added : " 3.7 "
2018-06-05 08:32:54 +03:00
description :
description :
- The description to use for the inventory source .
2019-09-30 23:01:44 +03:00
type : str
2018-06-05 08:32:54 +03:00
inventory :
description :
2020-02-05 21:24:46 +03:00
- Inventory the group should be made a member of .
2018-06-05 08:32:54 +03:00
required : True
2019-09-30 23:01:44 +03:00
type : str
2018-06-05 08:32:54 +03:00
source :
description :
2020-02-05 21:24:46 +03:00
- The source to use for this group .
2020-02-13 22:14:29 +03:00
choices : [ " scm " , " ec2 " , " gce " , " azure_rm " , " vmware " , " satellite6 " , " cloudforms " , " openstack " , " rhv " , " tower " , " custom " ]
2019-09-30 23:01:44 +03:00
type : str
2020-02-05 21:24:46 +03:00
required : False
source_path :
2018-06-05 08:32:54 +03:00
description :
2020-02-05 21:24:46 +03:00
- For an SCM based inventory source , the source path points to the file within the repo to use as an inventory .
2019-09-30 23:01:44 +03:00
type : str
2020-02-05 21:24:46 +03:00
source_script :
2019-10-29 23:20:30 +03:00
description :
2020-02-05 21:24:46 +03:00
- Inventory script to be used when group type is C ( custom ) .
2019-10-29 23:20:30 +03:00
type : str
2020-02-12 00:32:41 +03:00
required : False
2020-02-05 21:24:46 +03:00
source_vars :
2018-06-05 08:32:54 +03:00
description :
2020-02-05 21:24:46 +03:00
- The variables or environment fields to apply to this source type .
type : dict
credential :
2018-06-05 08:32:54 +03:00
description :
2020-02-05 21:24:46 +03:00
- Credential to use for the source .
2019-09-30 23:01:44 +03:00
type : str
2018-06-05 08:32:54 +03:00
source_regions :
description :
2020-02-05 21:24:46 +03:00
- Regions for cloud provider .
2019-09-30 23:01:44 +03:00
type : str
2018-06-05 08:32:54 +03:00
instance_filters :
description :
2020-02-05 21:24:46 +03:00
- Comma - separated list of filter expressions for matching hosts .
2019-09-30 23:01:44 +03:00
type : str
2018-06-05 08:32:54 +03:00
group_by :
description :
2020-02-05 21:24:46 +03:00
- Limit groups automatically created from inventory source .
2019-09-30 23:01:44 +03:00
type : str
2018-06-05 08:32:54 +03:00
overwrite :
description :
2020-02-05 21:24:46 +03:00
- Delete child groups and hosts not found in source .
2018-06-05 08:32:54 +03:00
type : bool
2020-02-05 21:24:46 +03:00
default : ' no '
2018-06-05 08:32:54 +03:00
overwrite_vars :
description :
2020-02-05 21:24:46 +03:00
- Override vars in child groups and hosts with those from external source .
2018-06-05 08:32:54 +03:00
type : bool
2020-02-05 21:24:46 +03:00
custom_virtualenv :
version_added : " 2.9 "
description :
- Local absolute file path containing a custom Python virtualenv to use .
type : str
required : False
default : ' '
timeout :
description : The amount of time ( in seconds ) to run before the task is canceled .
type : int
verbosity :
description : The verbosity level to run this inventory source under .
type : int
choices : [ 0 , 1 , 2 ]
2018-06-05 08:32:54 +03:00
update_on_launch :
description :
2020-02-05 21:24:46 +03:00
- Refresh inventory data from its source each time a job is run .
2018-06-05 08:32:54 +03:00
type : bool
2020-02-05 21:24:46 +03:00
default : ' no '
2018-06-05 08:32:54 +03:00
update_cache_timeout :
description :
2020-02-05 21:24:46 +03:00
- Time in seconds to consider an inventory sync to be current .
2019-09-30 23:01:44 +03:00
type : int
2020-02-05 21:24:46 +03:00
source_project :
description :
- Project to use as source with scm option
type : str
update_on_project_update :
description : Update this source when the related project updates if source is C ( scm )
type : bool
2018-06-05 08:32:54 +03:00
state :
description :
- Desired state of the resource .
default : " present "
choices : [ " present " , " absent " ]
2019-09-30 23:01:44 +03:00
type : str
2020-02-05 21:24:46 +03:00
tower_oauthtoken :
description :
- The Tower OAuth token to use .
required : False
type : str
2020-02-24 23:23:49 +03:00
version_added : " 3.7 "
2019-09-18 15:43:36 +03:00
extends_documentation_fragment : awx . awx . auth
2018-06-05 08:32:54 +03:00
'''
EXAMPLES = '''
2020-02-12 00:32:41 +03:00
- name : Add an inventory source
tower_inventory_source :
name : " source-inventory "
description : Source for inventory
inventory : previously - created - inventory
credential : previously - created - credential
overwrite : True
update_on_launch : True
source_vars :
private : false
2018-06-05 08:32:54 +03:00
'''
2020-02-05 21:24:46 +03:00
from . . module_utils . tower_api import TowerModule
from json import dumps
2018-06-05 08:32:54 +03:00
def main ( ) :
2020-02-05 21:24:46 +03:00
# Any additional arguments that are not fields of the item can be added here
2018-08-02 18:17:39 +03:00
argument_spec = dict (
2018-06-05 08:32:54 +03:00
name = dict ( required = True ) ,
2020-02-05 21:24:46 +03:00
new_name = dict ( type = ' str ' ) ,
2020-02-24 23:23:49 +03:00
description = dict ( required = False ) ,
2018-06-05 08:32:54 +03:00
inventory = dict ( required = True ) ,
2020-02-05 21:24:46 +03:00
#
# How do we handle manual and file? Tower does not seem to be able to activate them
#
2020-02-13 22:14:29 +03:00
source = dict ( choices = [ " scm " , " ec2 " , " gce " ,
2020-02-05 21:24:46 +03:00
" azure_rm " , " vmware " , " satellite6 " , " cloudforms " ,
" openstack " , " rhv " , " tower " , " custom " ] , required = False ) ,
source_path = dict ( ) ,
2020-02-12 00:32:41 +03:00
source_script = dict ( required = False ) ,
2020-02-05 21:24:46 +03:00
source_vars = dict ( type = ' dict ' ) ,
credential = dict ( ) ,
source_regions = dict ( ) ,
instance_filters = dict ( ) ,
group_by = dict ( ) ,
overwrite = dict ( type = ' bool ' ) ,
overwrite_vars = dict ( type = ' bool ' ) ,
2020-02-13 22:14:29 +03:00
custom_virtualenv = dict ( type = ' str ' , default = ' ' ) ,
2020-02-05 21:24:46 +03:00
timeout = dict ( type = ' int ' ) ,
verbosity = dict ( type = ' int ' , choices = [ 0 , 1 , 2 ] ) ,
update_on_launch = dict ( type = ' bool ' ) ,
update_cache_timeout = dict ( type = ' int ' ) ,
source_project = dict ( type = ' str ' ) ,
update_on_project_update = dict ( type = ' bool ' ) ,
2018-06-05 08:32:54 +03:00
state = dict ( choices = [ ' present ' , ' absent ' ] , default = ' present ' ) ,
2018-08-02 18:17:39 +03:00
)
2018-06-05 08:32:54 +03:00
2020-02-05 21:24:46 +03:00
# Create a module for ourselves
2020-02-19 00:02:05 +03:00
module = TowerModule ( argument_spec = argument_spec )
2020-02-05 21:24:46 +03:00
# Extract our parameters
2018-06-05 08:32:54 +03:00
name = module . params . get ( ' name ' )
2020-02-05 21:24:46 +03:00
new_name = module . params . get ( ' new_name ' )
2018-06-05 08:32:54 +03:00
inventory = module . params . get ( ' inventory ' )
2020-02-05 21:24:46 +03:00
source_script = module . params . get ( ' source_script ' )
credential = module . params . get ( ' credential ' )
source_project = module . params . get ( ' source_project ' )
2018-06-05 08:32:54 +03:00
state = module . params . get ( ' state ' )
2020-02-12 00:32:41 +03:00
# Attempt to look up inventory source based on the provided name and inventory ID
2020-02-19 00:02:05 +03:00
inventory_id = module . resolve_name_to_id ( ' inventories ' , inventory )
2020-02-05 21:24:46 +03:00
inventory_source = module . get_one ( ' inventory_sources ' , * * {
' data ' : {
' name ' : name ,
' inventory ' : inventory_id ,
}
} )
2020-02-12 00:32:41 +03:00
# Create the data that gets sent for create and update
2020-02-05 21:24:46 +03:00
inventory_source_fields = {
' name ' : new_name if new_name else name ,
' inventory ' : inventory_id ,
}
2020-02-19 00:02:05 +03:00
# Attempt to look up the related items the user specified (these will fail the module if not found)
2020-02-26 07:51:26 +03:00
if credential is not None :
2020-02-19 00:02:05 +03:00
inventory_source_fields [ ' credential ' ] = module . resolve_name_to_id ( ' credentials ' , credential )
2020-02-26 07:51:26 +03:00
if source_project is not None :
2020-02-19 00:02:05 +03:00
inventory_source_fields [ ' source_project ' ] = module . resolve_name_to_id ( ' projects ' , source_project )
2020-02-26 07:51:26 +03:00
if source_script is not None :
2020-02-19 00:02:05 +03:00
inventory_source_fields [ ' source_script ' ] = module . resolve_name_to_id ( ' inventory_scripts ' , source_script )
OPTIONAL_VARS = (
' description ' , ' source ' , ' source_path ' , ' source_vars ' ,
' source_regions ' , ' instance_filters ' , ' group_by ' ,
' overwrite ' , ' overwrite_vars ' , ' custom_virtualenv ' ,
' timeout ' , ' verbosity ' , ' update_on_launch ' , ' update_cache_timeout ' ,
' update_on_project_update '
)
2020-02-05 21:24:46 +03:00
# Layer in all remaining optional information
2020-02-19 00:02:05 +03:00
for field_name in OPTIONAL_VARS :
field_val = module . params . get ( field_name )
if field_val :
inventory_source_fields [ field_name ] = field_val
# Attempt to JSON encode source vars
if inventory_source_fields . get ( ' source_vars ' , None ) :
inventory_source_fields [ ' source_vars ' ] = dumps ( inventory_source_fields [ ' source_vars ' ] )
# Sanity check on arguments
if state == ' present ' and not inventory_source and not inventory_source_fields [ ' source ' ] :
module . fail_json ( msg = " If creating a new inventory source, the source param must be present " )
2020-02-05 21:24:46 +03:00
if state == ' absent ' :
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
module . delete_if_needed ( inventory_source )
elif state == ' present ' :
# If the state was present we can let the module build or update the existing inventory_source, this will return on its own
module . create_or_update_if_needed ( inventory_source , inventory_source_fields , endpoint = ' inventory_sources ' , item_type = ' inventory source ' )
2018-06-05 08:32:54 +03:00
if __name__ == ' __main__ ' :
main ( )