in lib/ansible/plugins/inventory/script/alicloud.py [0:0]
def read_settings(self):
''' Reads the settings from the alicloud.ini file '''
config = configparser.ConfigParser()
ecs_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'alicloud.ini')
ecs_ini_path = os.path.expanduser(os.path.expandvars(os.environ.get('ALICLOUD_INI_PATH', ecs_default_ini_path)))
config.read(ecs_ini_path)
access_key = os.environ.get('ALICLOUD_ACCESS_KEY', os.environ.get('ALICLOUD_ACCESS_KEY_ID', None))
if not access_key:
access_key = self.get_option(config, 'credentials', 'alicloud_access_key')
secret_key = os.environ.get('ALICLOUD_SECRET_KEY', os.environ.get('ALICLOUD_SECRET_ACCESS_KEY', None))
if not secret_key:
secret_key = self.get_option(config, 'credentials', 'alicloud_secret_key')
security_token = os.environ.get('ALICLOUD_SECURITY_TOKEN', None)
if not security_token:
security_token = self.get_option(config, 'credentials', 'alicloud_security_token')
alicloud_region = os.environ.get('ALICLOUD_REGION', None)
if not alicloud_region:
alicloud_region = self.get_option(config, 'credentials', 'alicloud_region')
ecs_role_name = os.environ.get('ALICLOUD_ECS_ROLE_NAME', None)
if not ecs_role_name:
ecs_role_name = self.get_option(config, 'credentials', 'alicloud_ecs_role_name')
profile = os.environ.get('ALICLOUD_PROFILE', None)
if not profile:
profile = self.get_option(config, 'credentials', 'alicloud_profile')
shared_credentials_file = os.environ.get('ALICLOUD_SHARED_CREDENTIALS_FILE', None)
if not shared_credentials_file:
shared_credentials_file = self.get_option(config, 'credentials', 'alicloud_shared_credentials_file')
assume_role = self.get_option(config, 'credentials', 'assume_role')
assume_role_params = {}
role_arn = os.environ.get('ALICLOUD_ASSUME_ROLE_ARN', None)
if not role_arn and assume_role:
assume_role_params['role_arn'] = assume_role.get('role_arn')
session_name = os.environ.get('ALICLOUD_ASSUME_ROLE_SESSION_NAME', None)
if not session_name and assume_role:
assume_role_params['session_name'] = assume_role.get('session_name')
session_expiration = os.environ.get('ALICLOUD_ASSUME_ROLE_SESSION_EXPIRATION', None)
if not session_expiration and assume_role:
assume_role_params['session_expiration'] = assume_role.get('session_expiration')
if assume_role:
assume_role_params['policy'] = assume_role.get('policy')
credentials = {
'alicloud_access_key': access_key,
'alicloud_secret_key': secret_key,
'security_token': security_token,
'ecs_role_name': ecs_role_name,
'profile': profile,
'shared_credentials_file': shared_credentials_file,
'assume_role': assume_role_params,
'alicloud_region': alicloud_region
}
self.credentials = get_profile(credentials)
# Regions
config_regions = self.get_option(config, 'ecs', 'regions')
if not config_regions or config_regions == 'all':
all_regions = self.connect_to_ecs(footmark.ecs, "cn-beijing").describe_regions()
exclude_regions = []
if self.get_option(config, 'ecs', 'regions_exclude'):
exclude_regions = [ex.strip() for ex in self.get_option(config, 'ecs', 'regions_exclude').split(',') if ex.strip()]
for region in all_regions:
if exclude_regions and region.id in exclude_regions:
continue
self.regions.append(region.id)
else:
self.regions = config_regions.split(",")
# # Destination addresses
self.destination_variable = self.get_option(config, 'ecs', 'destination_variable', "")
self.hostname_variable = self.get_option(config, 'ecs', 'hostname_variable', "")
self.destination_format = self.get_option(config, 'ecs', 'destination_format', "")
self.destination_format_tags = self.get_option(config, 'ecs', 'destination_format_tags', "")
# Instance states to be gathered in inventory. Default is 'running'.
ecs_valid_instance_states = ['pending', 'running', 'starting', 'stopping', 'stopped']
if self.get_option(config, 'ecs', 'all_instances'):
self.ecs_instance_states.extend(ecs_valid_instance_states)
elif self.get_option(config, 'ecs', 'instance_states'):
for instance_state in self.get_option(config, 'ecs', 'instance_states').split(","):
instance_state = instance_state.strip()
if instance_state not in ecs_valid_instance_states:
continue
self.ecs_instance_states.append(instance_state)
else:
self.ecs_instance_states.append('running')
# Cache related
cache_dir = os.path.expanduser(self.get_option(config, 'ecs', 'cache_path'))
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
cache_name = 'ansible-alicloud'
self.cache_path_cache = cache_dir + "/%s.cache" % cache_name
self.cache_path_index = cache_dir + "/%s.index" % cache_name
self.cache_max_age = float(self.get_option(config, 'ecs', 'cache_max_age'))
self.expand_csv_tags = self.get_option(config, 'ecs', 'expand_csv_tags')
# Configure nested groups instead of flat namespace.
self.nested_groups = self.get_option(config, 'ecs', 'nested_groups')
# Configure which groups should be created.
group_by_options = [
'group_by_instance_id',
'group_by_region',
'group_by_availability_zone',
'group_by_instance_type',
'group_by_image_id',
'group_by_vpc_id',
'group_by_vswitch_id',
'group_by_security_group',
'group_by_tag_keys',
'group_by_tag_none'
]
for option in group_by_options:
setattr(self, option, self.get_option(config, 'ecs', option))
# Do we need to just include hosts that match a pattern?
try:
pattern_include = self.get_option(config, 'ecs', 'pattern_include')
if pattern_include and len(pattern_include) > 0:
self.pattern_include = re.compile(pattern_include)
except configparser.NoOptionError:
raise
# Do we need to exclude hosts that match a pattern?
try:
pattern_exclude = self.get_option(config, 'ecs', 'pattern_exclude')
if pattern_exclude and len(pattern_exclude) > 0:
self.pattern_exclude = re.compile(pattern_exclude)
except configparser.NoOptionError:
raise
instance_filters = self.get_option(config, 'ecs', 'instance_filters')
if instance_filters and len(instance_filters) > 0:
tags = {}
for field in instance_filters.split(','):
field = field.strip()
if not field or '=' not in field:
continue
key, value = [x.strip() for x in field.split('=', 1)]
if not key:
continue
elif key.startswith("tag:"):
tags[key[4:]] = value
continue
elif key in ['page_size', 'page_number']:
try:
if value and int(value):
value = int(value)
except Exception:
raise
self.ecs_instance_filters[key] = value
if tags:
self.ecs_instance_filters['tags'] = tags