Repository: ansible/ansible

Issue:
pacemaker module can't cleanup resources
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
pacemaker_cluster module

##### ANSIBLE VERSION
ansible 2.4.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/gsciorti/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/gsciorti/PycharmProjects/ansible/build/lib/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]

##### CONFIGURATION
Default configuration

##### OS / ENVIRONMENT
Ansible host: Fedora 25
Managed host: RHEL 7.2 running  a pacemaker cluster with:

- pacemaker-1.1.15-11.el7_3.2.x86_64
- pcs-0.9.152-10.el7_3.1.x86_64
- corosync-2.4.0-4.el7.x86_64

##### SUMMARY
The ansible module running with the parameter state=cleanup fails with an error without executing the command "pcs resource cleanup" 

##### STEPS TO REPRODUCE
```
$ ansible r7node1 -b -m pacemaker_cluster -a 'state=cleanup'
r7node1 | FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Shared connection to r7node1 closed.\r\n", 
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_dhzoKL/ansible_module_pacemaker_cluster.py\", line 234, in <module>\r\n    main()\r\n  File \"/tmp/ansible_dhzoKL/ansible_module_pacemaker_cluster.py\", line 228, in main\r\n    set_cluster(module, state, timeout, force)\r\n  File \"/tmp/ansible_dhzoKL/ansible_module_pacemaker_cluster.py\", line 125, in set_cluster\r\n    rc, out, err = module.run_command(cmd)\r\nUnboundLocalError: local variable 'cmd' referenced before assignment\r\n", 
    "msg": "MODULE FAILURE", 
    "rc": 0
}
```

##### EXPECTED RESULTS
The command "pcs resource cleanup" should be executed on the cluster node.

Note: The function  [clean_cluster](https://github.com/ansible/ansible/blob/0765ceb66dfad116ec0519a0f9272158da6600d0/lib/ansible/modules/clustering/pacemaker_cluster.py#L106) has been defined in the module pacemaker_cluster but this module call the function [set_cluster](https://github.com/ansible/ansible/blob/0765ceb66dfad116ec0519a0f9272158da6600d0/lib/ansible/modules/clustering/pacemaker_cluster.py#L106)  that doesn't manage the cleanup operation


##### ACTUAL RESULTS
```
$ ansible r7node1 -b -m pacemaker_cluster -a 'state=cleanup' -v
Using /etc/ansible/ansible.cfg as config file
r7node1 | FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Shared connection to r7node1 closed.\r\n", 
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_6AUAP8/ansible_module_pacemaker_cluster.py\", line 234, in <module>\r\n    main()\r\n  File \"/tmp/ansible_6AUAP8/ansible_module_pacemaker_cluster.py\", line 228, in main\r\n    set_cluster(module, state, timeout, force)\r\n  File \"/tmp/ansible_6AUAP8/ansible_module_pacemaker_cluster.py\", line 125, in set_cluster\r\n    rc, out, err = module.run_command(cmd)\r\nUnboundLocalError: local variable 'cmd' referenced before assignment\r\n", 
    "msg": "MODULE FAILURE", 
    "rc": 0
}

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/clustering/pacemaker_cluster.py b/lib/ansible/modules/clustering/pacemaker_cluster.py
--- a/lib/ansible/modules/clustering/pacemaker_cluster.py
+++ b/lib/ansible/modules/clustering/pacemaker_cluster.py
@@ -219,9 +219,10 @@ def main():
             module.fail_json(msg="Failed during the restart of the cluster, the cluster can't be stopped")
 
     if state in ['cleanup']:
-        set_cluster(module, state, timeout, force)
+        clean_cluster(module, timeout)
+        cluster_state = get_cluster_status(module)
         module.exit_json(changed=True,
-                 out=cluster_state)
+                         out=cluster_state)
 
 
 if __name__ == '__main__':


Test patch: