From 476030a49c2aff82f35e44a9ad9099fb16217d70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Thu, 11 Jan 2018 22:26:39 +0100 Subject: [PATCH] vr_server: implement startup_script (#34565) * simplify * vr_server: implement startup_script --- lib/ansible/module_utils/vultr.py | 2 +- lib/ansible/modules/cloud/vultr/vr_server.py | 42 +++++++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/ansible/module_utils/vultr.py b/lib/ansible/module_utils/vultr.py index e3682f31f0e..d735af90c54 100644 --- a/lib/ansible/module_utils/vultr.py +++ b/lib/ansible/module_utils/vultr.py @@ -203,7 +203,7 @@ class Vultr: }) return r_data - self.module.fail_json(msg="Could not find %s %s: %s" % (resource, key, value)) + self.module.fail_json(msg="Could not find %s with %s: %s" % (resource, key, value)) def get_result(self, resource): if resource: diff --git a/lib/ansible/modules/cloud/vultr/vr_server.py b/lib/ansible/modules/cloud/vultr/vr_server.py index 0a39b1bfaf6..3fc0d93cc01 100644 --- a/lib/ansible/modules/cloud/vultr/vr_server.py +++ b/lib/ansible/modules/cloud/vultr/vr_server.py @@ -67,6 +67,10 @@ options: user_data: description: - User data to be passed to the server. + startup_script: + description: + - Name of the startup script to execute on boot. + - Only considered while creating the server. ssh_keys: description: - List of SSH keys passed to the server on creation. @@ -351,61 +355,49 @@ class AnsibleVultrServer(Vultr): } self.server_power_state = None - def get_os(self): - name = self.module.params.get('os') - if not name: - return {} + def get_startup_script(self): + return self.query_resource_by_key( + key='name', + value=self.module.params.get('startup_script'), + resource='startupscript', + ) + def get_os(self): return self.query_resource_by_key( key='name', - value=name, + value=self.module.params.get('os'), resource='os', use_cache=True ) def get_ssh_key(self): - name = self.module.params.get('ssh_key') - if not name: - return {} - return self.query_resource_by_key( key='name', - value=name, + value=self.module.params.get('ssh_key'), resource='sshkey', use_cache=True ) def get_region(self): - name = self.module.params.get('region') - if not name: - return {} - return self.query_resource_by_key( key='name', - value=name, + value=self.module.params.get('region'), resource='regions', use_cache=True ) def get_plan(self): - name = self.module.params.get('plan') - if not name: - return {} - return self.query_resource_by_key( key='name', - value=name, + value=self.module.params.get('plan'), resource='plans', use_cache=True ) def get_firewall_group(self): - name = self.module.params.get('firewall_group') - if not name: - return {} return self.query_resource_by_key( key='description', - value=name, + value=self.module.params.get('firewall_group'), resource='firewall', query_by='group_list' ) @@ -492,6 +484,7 @@ class AnsibleVultrServer(Vultr): 'tag': self.module.params.get('tag'), 'reserved_ip_v4': self.module.params.get('reserved_ip_v4'), 'user_data': self.get_user_data(), + 'SCRIPTID': self.get_startup_script().get('SCRIPTID'), } self.api_query( path="/v1/server/create", @@ -835,6 +828,7 @@ def main(): tag=dict(), reserved_ip_v4=dict(), firewall_group=dict(), + startup_script=dict(), user_data=dict(), ssh_keys=dict(type='list', aliases=['ssh_key']), region=dict(),