Skip to content

Commit

Permalink
Improve inventory alicloud.py meta and use public_ip_address instead …
Browse files Browse the repository at this point in the history
…eip_address
  • Loading branch information
xiaozhu36 committed Jan 4, 2019
1 parent 0ab86cb commit f3ddcc3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions contrib/inventory/alicloud.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 8 additions & 31 deletions contrib/inventory/alicloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '''

Expand All @@ -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):

Expand Down

0 comments on commit f3ddcc3

Please sign in to comment.