diff --git a/lib/ansible/modules/cloud/azure/azure_rm_webapp.py b/lib/ansible/modules/cloud/azure/azure_rm_webapp.py index 3831f52db0f..d4a6e5cc5a4 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_webapp.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_webapp.py @@ -158,6 +158,16 @@ options: - Purge any existing application settings. Replace web app application settings with app_settings. type: bool + app_state: + description: + - Start/Stop/Restart the web app. + type: str + choices: + - started + - stopped + - restarted + default: started + state: description: - Assert the state of the Web App. @@ -410,6 +420,11 @@ class AzureRMWebApps(AzureRMModuleBase): type='bool', default=False ), + app_state=dict( + type='str', + choices=['started', 'stopped', 'restarted'], + default='started' + ), state=dict( type='str', default='present', @@ -450,6 +465,7 @@ class AzureRMWebApps(AzureRMModuleBase): self.container_settings = None self.purge_app_settings = False + self.app_state = 'started' self.results = dict( changed=False, @@ -595,6 +611,7 @@ class AzureRMWebApps(AzureRMModuleBase): to_be_updated = True self.to_do = Actions.CreateOrUpdate + self.site.tags = self.tags # service plan is required for creation if not self.plan: @@ -631,7 +648,7 @@ class AzureRMWebApps(AzureRMModuleBase): self.log('Result: {0}'.format(old_response)) - update_tags, old_response['tags'] = self.update_tags(old_response.get('tags', dict())) + update_tags, self.site.tags = self.update_tags(old_response.get('tags', None)) if update_tags: to_be_updated = True @@ -693,8 +710,26 @@ class AzureRMWebApps(AzureRMModuleBase): if self.to_do == Actions.CreateOrUpdate: response = self.create_update_webapp() + self.results['id'] = response['id'] + webapp = None + if old_response: + webapp = old_response + if response: + webapp = response + + if webapp: + if (webapp['state'] != 'Stopped' and self.app_state == 'stopped') or \ + (webapp['state'] != 'Running' and self.app_state == 'started') or \ + self.app_state == 'restarted': + + self.results['changed'] = True + if self.check_mode: + return self.results + + self.set_webapp_state(self.app_state) + return self.results # compare existing web app with input, determine weather it's update operation @@ -943,6 +978,29 @@ class AzureRMWebApps(AzureRMModuleBase): return False + def set_webapp_state(self, appstate): + ''' + Start/stop/restart web app + :return: deserialized updating response + ''' + try: + if appstate == 'started': + response = self.web_client.web_apps.start(resource_group_name=self.resource_group, name=self.name) + elif appstate == 'stopped': + response = self.web_client.web_apps.stop(resource_group_name=self.resource_group, name=self.name) + elif appstate == 'restarted': + response = self.web_client.web_apps.restart(resource_group_name=self.resource_group, name=self.name) + else: + self.fail("Invalid web app state {0}".format(appstate)) + + self.log("Response : {0}".format(response)) + + return response + except CloudError as ex: + request_id = ex.request_id if ex.request_id else '' + self.log("Failed to {0} web app {1} in resource group {2}, request_id {3} - {4}".format( + appstate, self.name, self.resource_group, request_id, str(ex))) + def main(): """Main execution""" diff --git a/test/integration/targets/azure_rm_webapp/tasks/main.yml b/test/integration/targets/azure_rm_webapp/tasks/main.yml index 8cb6cbf2063..bd92fea4e21 100644 --- a/test/integration/targets/azure_rm_webapp/tasks/main.yml +++ b/test/integration/targets/azure_rm_webapp/tasks/main.yml @@ -22,6 +22,31 @@ plan: "{{ win_plan_name }}" register: output +- name: stop the web app + azure_rm_webapp: + resource_group: "{{ resource_group }}" + name: "{{ win_app_name }}2" + plan: "{{ win_plan_name }}" + app_state: stopped + register: output + +- name: assert output changed + assert: + that: + output.changed + +# enable after webapp_facts merged +# - name: get the web app +# azure_rm_webapp_facts: +# resource_group: "{{ resource_group }}" +# name: "{{ win_app_name }}2" +# register: stopped + +# - name: assert web app is stopped +# assert: +# that: +# - stopped.properties.state == "Stopped" + - name: Create a windows web app with existing app service plan, try to update some root level params azure_rm_webapp: resource_group: "{{ resource_group }}" @@ -251,4 +276,4 @@ - name: Assert the web app was created assert: - that: output.changed \ No newline at end of file + that: output.changed