in lib/ansible/modules/cloud/alicloud/ali_eni.py [0:0]
def main():
argument_spec = ecs_argument_spec()
argument_spec.update(
dict(
eni_id=dict(type='str', aliases=['id']),
instance_id=dict(type='str'),
private_ip_address=dict(type='str', aliases=['private_ip']),
vswitch_id=dict(type='str', aliases=['subnet_id']),
description=dict(type='str'),
name=dict(type='str'),
security_groups=dict(type='list', elements='str'),
state=dict(default='present', choices=['present', 'absent']),
attached=dict(default=False, type='bool'),
tags=dict(type='dict'),
purge_tags=dict(type='bool', default=False)
)
)
module = AnsibleModule(argument_spec=argument_spec,
required_if=([
('attached', True, ['instance_id'])
])
)
if not HAS_FOOTMARK:
module.fail_json(msg='footmark required for this module')
ecs = ecs_connect(module)
state = module.params.get("state")
eni = uniquely_find_eni(ecs, module)
changed = False
if state == 'absent':
if not eni:
module.exit_json(changed=changed, interface={})
try:
changed = eni.delete()
module.exit_json(changed=changed, interface={})
except Exception as e:
module.fail_json(msg="{0}".format(e))
# when state is present
group_ids = module.params.get("security_groups")
name = module.params.get("name")
description = module.params.get("description")
if not eni:
if not group_ids:
module.fail_json(msg="'security_groups' is required when creating a new ENI")
vswitch_id = module.params.get("vswitch_id")
if not vswitch_id:
module.fail_json(msg="'vswitch_id' is required when creating a new ENI")
try:
params = module.params
params['security_group_id'] = group_ids[0]
params['primary_ip_address'] = module.params.get("private_ip_address")
params['network_interface_name'] = module.params.get("name")
params['client_token'] = "Ansible-Alicloud-{0}-{1}".format(hash(str(module.params)), str(time.time()))
eni = ecs.create_network_interface(**params)
module.exit_json(changed=True, interface=eni.get().read())
except Exception as e:
module.fail_json(msg="{0}".format(e))
if not group_ids:
group_ids = eni.security_group_ids["security_group_id"]
if not name:
name = eni.name
if not description:
description = eni.description
try:
if eni.modify(group_ids, name, description):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))
attached = module.params.get("attached")
instance_id = module.params.get("instance_id")
try:
if attached:
if not eni.instance_id:
if eni.attach(instance_id):
changed = True
elif eni.instance_id != instance_id and eni.detach(eni.instance_id) and eni.attach(instance_id):
changed = True
else:
if eni.detach(eni.instance_id):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))
tags = module.params['tags']
if module.params['purge_tags']:
removed = {}
if not tags:
removed = eni.tags
else:
for key, value in list(eni.tags.items()):
if key not in list(tags.keys()):
removed[key] = value
try:
if eni.remove_tags(removed):
changed = True
module.exit_json(changed=changed, interface=eni.get().read())
except Exception as e:
module.fail_json(msg="{0}".format(e))
if tags:
try:
if eni.add_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))
module.exit_json(changed=changed, interface=eni.get().read())