diff --git a/CHANGELOG.md b/CHANGELOG.md index bc8bbefd..116ac505 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ FEATURES: IMPROVEMENTS: +- Improve inventory alicloud.py meta and use public_ip_address instead eip_address ([#152](https://github.com/alibaba/ansible-provider/pull/152)) - Improve module ali_vpc and ali_security_group multi_ok ([#149](https://github.com/alibaba/ansible-provider/pull/149)) - Improve module ali_slb_vsg and ali_slb_vsg_facts ([#148](https://github.com/alibaba/ansible-provider/pull/148)) - Improve module ali_slb_lb and ali_slb_lb_facts ([#147](https://github.com/alibaba/ansible-provider/pull/147)) diff --git a/contrib/inventory/alicloud.ini b/contrib/inventory/alicloud.ini index a03974ba..fc3bfe1a 100644 --- a/contrib/inventory/alicloud.ini +++ b/contrib/inventory/alicloud.ini @@ -17,8 +17,7 @@ regions = cn-beijing # Each ECS instance has server ip address variables associated with it. # This destination_variable is used as the address of a server, and it support # following value: -# - public_ip_address: return the public IP address of the ECS server -# - eip_address: return the elastic IP address of the ECS server +# - public_ip_address: return the public IP address or elastic ip address of the ECS server # - private_ip_address: return the inner or private IP address of the ECS server # WARNING: For instances in a private subnet, this should be set to 'private_ip_address', # and Ansible must be run from within the subnet. diff --git a/contrib/inventory/alicloud.py b/contrib/inventory/alicloud.py index 005a37ec..0340d42f 100755 --- a/contrib/inventory/alicloud.py +++ b/contrib/inventory/alicloud.py @@ -291,10 +291,12 @@ def add_instance(self, instance, region): # Select the best destination address if self.destination_variable: - if not instance.vswitch_id and self.destination_variable in ('private_ip_address', 'inner_ip_address'): - dest = getattr(instance, 'inner_ip_address', None) - else: - dest = getattr(instance, self.destination_variable, None) + if self.destination_variable == 'inner_ip_address': + self.destination_variable = 'private_ip_address' + elif self.destination_variable == 'eip_address': + self.destination_variable = 'public_ip_address' + + dest = getattr(instance, self.destination_variable, None) if not dest: # Skip instances we cannot address @@ -408,34 +410,9 @@ def add_instance(self, instance, region): self.push(self.inventory, hostname, dest) self.push_group(self.inventory, 'alicloud', hostname) - self.inventory["_meta"]["hostvars"][hostname] = self.get_host_info_dict_from_instance(instance) + self.inventory["_meta"]["hostvars"][hostname] = instance.read() self.inventory["_meta"]["hostvars"][hostname]['ansible_ssh_host'] = dest - def get_host_info_dict_from_instance(self, instance): - instance_vars = {} - for key in vars(instance): - value = getattr(instance, key) - - # Handle complex types - if type(value) in [int, bool]: - instance_vars[key] = value - elif isinstance(value, string_types): - instance_vars[key] = value.strip() - elif not value: - instance_vars[key] = '' - elif key == 'tags': - for k, v in value.items(): - if self.expand_csv_tags and ',' in v: - v = map(lambda x: x.strip(), v.split(',')) - instance_vars['tag_' + k] = v - elif key == 'groups': - instance_vars["security_group_ids"] = ','.join([str(i) for i in value]) - else: - pass - # TODO Product codes if someone finds them useful - - return instance_vars - def get_host_info(self): ''' Get variables about a specific host ''' @@ -453,7 +430,7 @@ def get_host_info(self): region, instance_id, instance_name = self.index[self.args.host] instance = self.get_instance_by_id(region, instance_id) - return self.json_format_dict(self.get_host_info_dict_from_instance(instance), True) + return self.json_format_dict(instance.read(), True) def connect_to_ecs(self, module, region):