Repository: ansible/ansible

Issue:
azure_rm_deployment ignores 'wait_for_deployment_completion' option
##### ISSUE TYPE
Bug Report

##### COMPONENT NAME
azure_rm_deployment

##### ANSIBLE VERSION
```
2.3.1.0
```

##### CONFIGURATION
*nope*

##### OS / ENVIRONMENT

macOS Sierra

##### SUMMARY

The `azure_rm_resourcegroup` module ignores the `wait_for_deployment_completion: False` option and blocks until the deployment completes. 

##### STEPS TO REPRODUCE

1. Download the [example ARM templates](https://github.com/Azure/azure-quickstart-templates/tree/master/101-container-registry) to the local ansible directory.

2. Run the following playbook.

```yaml
- name: Create infra
  hosts: localhost
  gather_facts: False

  tasks:

    - name: Create resource group
      azure_rm_resourcegroup:
        name: foobar
        location: westeurope
      register: reg_rg

    - name: Deploy Docker registry
      azure_rm_deployment:
        deployment_name: registry
        resource_group_name: '{{ reg_rg.state.name }}'
        location: '{{ reg_rg.state.location }}'
        template: "{{ lookup('file', './azuredeploy.json') }}"
        parameters: "{{ lookup('file', './azuredeploy.parameters.json') }}"
        wait_for_deployment_completion: False
      register: reg_depl

    - debug: var=reg_depl
```

##### EXPECTED RESULTS
Ansible initiates the deployment then moves on to the `debug` task without waiting for the deployment completion.

##### ACTUAL RESULTS
Ansible blocks until the deployment has completed.


Discussion:
cc @devigned @lmazuel
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Yes, looking at the code it's pretty obvious :/
The [get_poller_result call](https://github.com/ansible/ansible/blob/5b03e5544045749fca87daa05380592901c0eca1/lib/ansible/modules/cloud/azure/azure_rm_deployment.py#L511) should be inside the "if" statement.
@tstringer Do you think you might have time for that? I'm not sure myself when I would be able to fix it :(
@lmazuel yep that looks straight forward enough. I'll create the PR. Thanks for the heads up! 👍 

Resolving patch:
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_deployment.py b/lib/ansible/modules/cloud/azure/azure_rm_deployment.py
--- a/lib/ansible/modules/cloud/azure/azure_rm_deployment.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_deployment.py
@@ -456,13 +456,23 @@ def exec_module(self, **kwargs):
 
         if self.state == 'present':
             deployment = self.deploy_template()
-            self.results['deployment'] = dict(
-                name=deployment.name,
-                group_name=self.resource_group_name,
-                id=deployment.id,
-                outputs=deployment.properties.outputs,
-                instances=self._get_instances(deployment)
-            )
+            if deployment is None:
+                self.results['deployment'] = dict(
+                    name=self.deployment_name,
+                    group_name=self.resource_group_name,
+                    id=None,
+                    outputs=None,
+                    instances=None
+                )
+            else:
+                self.results['deployment'] = dict(
+                    name=deployment.name,
+                    group_name=self.resource_group_name,
+                    id=deployment.id,
+                    outputs=deployment.properties.outputs,
+                    instances=self._get_instances(deployment)
+                )
+
             self.results['changed'] = True
             self.results['msg'] = 'deployment succeeded'
         else:
@@ -508,8 +518,9 @@ def deploy_template(self):
                                                                  self.deployment_name,
                                                                  deploy_parameter)
 
-            deployment_result = self.get_poller_result(result)
+            deployment_result = None
             if self.wait_for_deployment_completion:
+                deployment_result = self.get_poller_result(result)
                 while deployment_result.properties is None or deployment_result.properties.provisioning_state not in ['Canceled', 'Failed', 'Deleted',
                                                                               'Succeeded']:
                     time.sleep(self.wait_for_deployment_polling_period)


Test patch: