We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
vmware_local_user_manager error: 'module' not defined
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

vmware_local_user_manager
##### ANSIBLE VERSION

```
ansible 2.2.0 (devel 321d2e8cee) last updated 2016/08/21 14:29:27 (GMT +200)
  lib/ansible/modules/core: (detached HEAD 91a839f1e3) last updated 2016/08/21 14:32:29 (GMT +200)
  lib/ansible/modules/extras: (detached HEAD 1aeb9f8a8c) last updated 2016/08/21 14:32:44 (GMT +200)
```
##### CONFIGURATION

None
##### OS / ENVIRONMENT

Execution from: Ubuntu 14.04
Execution to: VMware ESXi 6.0 U2
##### SUMMARY

Execution of module vmware_local_user_manager fails with error
##### STEPS TO REPRODUCE

```
- name: "vSphere ESXi: Add users"
       vmware_local_user_manager: validate_certs=False hostname={{ inventory_hostname }} username=root password=mypassword local_user_name=foo local_user_password=bar
```
##### EXPECTED RESULTS

Create user on target system
##### ACTUAL RESULTS

Error:

```
TASK [vSphere ESXi: Add users] *************************************************
task path: /home/devel/ansible-configuration/vmware.yml:15
Using module file /home/devel/ansible/lib/ansible/modules/extras/cloud/vmware/vmware_local_user_manager.py
<esxi> ESTABLISH LOCAL CONNECTION FOR USER: devel
<esxi> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382 `" && echo ansible-tmp-1471786926.92-121489380863382="` echo $HOME/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382 `" ) && sleep 0'
<esxi> PUT /tmp/tmpHVuXHh TO /home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/vmware_local_user_manager.py
<esxi> EXEC /bin/sh -c 'chmod u+x /home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/ /home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/vmware_local_user_manager.py && sleep 0'
<esxi> EXEC /bin/sh -c '/usr/bin/python /home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/vmware_local_user_manager.py; rm -rf "/home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/" > /dev/null 2>&1 && sleep 0'
fatal: [esxi]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_args": {
            "hostname": "esxi", 
            "local_user_description": null, 
            "local_user_name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "local_user_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "state": "present", 
            "username": "devel", 
            "validate_certs": false
        }, 
        "module_name": "vmware_local_user_manager"
    }, 
    "msg": "global name 'module' is not defined"
}
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `cloud/vmware/vmware_local_user_manager.py`
Content:
```
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 # Copyright IBM Corp. 2016
5 # Author(s): Andreas Nafpliotis <nafpliot@de.ibm.com>
6 
7 # This file is part of Ansible
8 #
9 # Ansible is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Ansible is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Ansible.  If not, see <http://www.gnu.org/licenses/
21 
22 DOCUMENTATION = '''
23 ---
24 module: vmware_local_user_manager
25 short_description: Manage local users on an ESXi host
26 description:
27     - Manage local users on an ESXi host
28 version_added: "2.2"
29 author: Andreas Nafpliotis
30 notes:
31     - Tested on ESXi 6.0
32     - Be sure that the ESXi user used for login, has the appropriate rights to create / delete / edit users
33 requirements:
34     - "python >= 2.6"
35     - PyVmomi installed
36 options:
37     local_user_name:
38         description:
39             - The local user name to be changed
40         required: True
41     local_user_password:
42         description:
43             - The password to be set
44         required: False
45     local_user_description:
46         description:
47             - Description for the user
48         required: False
49     state:
50         description:
51             - Indicate desired state of the user. If the user already exists when C(state=present), the user info is updated
52         choices: ['present', 'absent']
53         default: present
54 extends_documentation_fragment: vmware.documentation
55 '''
56 
57 EXAMPLES = '''
58 # Example vmware_local_user_manager command from Ansible Playbooks
59 - name: Add local user to ESXi
60   local_action:
61       module: vmware_local_user_manager
62       hostname: esxi_hostname
63       username: root
64       password: vmware
65       local_user_name: foo
66 '''
67 
68 RETURN = '''# '''
69 
70 try:
71     from pyVmomi import vim, vmodl
72     HAS_PYVMOMI = True
73 except ImportError:
74     HAS_PYVMOMI = False
75 
76 
77 class VMwareLocalUserManager(object):
78     def __init__(self, module):
79         self.module = module
80         self.content = connect_to_api(self.module)
81         self.local_user_name = self.module.params['local_user_name']
82         self.local_user_password = self.module.params['local_user_password']
83         self.local_user_description = self.module.params['local_user_description']
84         self.state = self.module.params['state']
85 
86     def process_state(self):
87         try:
88             local_account_manager_states = {
89                 'absent': {
90                    'present': self.state_remove_user,
91                    'absent': self.state_exit_unchanged,
92                 },
93                 'present': {
94                     'present': self.state_update_user,
95                     'absent': self.state_create_user,
96                 }
97             }
98 
99             local_account_manager_states[self.state][self.check_local_user_manager_state()]()
100         except vmodl.RuntimeFault as runtime_fault:
101             self.module.fail_json(msg=runtime_fault.msg)
102         except vmodl.MethodFault as method_fault:
103             self.module.fail_json(msg=method_fault.msg)
104         except Exception as e:
105             self.module.fail_json(msg=str(e))
106 
107 
108     def check_local_user_manager_state(self):
109         user_account = self.find_user_account()
110         if not user_account:
111             return 'absent'
112         else:
113             return 'present'
114 
115 
116     def find_user_account(self):
117         searchStr = self.local_user_name
118         exactMatch = True
119         findUsers = True
120         findGroups = False
121         user_account = self.content.userDirectory.RetrieveUserGroups(None, searchStr, None, None, exactMatch, findUsers, findGroups)
122         return user_account
123 
124 
125     def create_account_spec(self):
126         account_spec = vim.host.LocalAccountManager.AccountSpecification()
127         account_spec.id = self.local_user_name
128         account_spec.password = self.local_user_password
129         account_spec.description = self.local_user_description
130         return account_spec
131 
132 
133     def state_create_user(self):
134         account_spec = self.create_account_spec()
135 
136         try:
137             task = self.content.accountManager.CreateUser(account_spec)
138             self.module.exit_json(changed=True)
139         except vmodl.RuntimeFault as runtime_fault:
140             module.fail_json(msg=runtime_fault.msg)
141         except vmodl.MethodFault as method_fault:
142             module.fail_json(msg=method_fault.msg)
143 
144     def state_update_user(self):
145         account_spec = self.create_account_spec()
146 
147         try:
148             task = self.content.accountManager.UpdateUser(account_spec)
149             self.module.exit_json(changed=True)
150         except vmodl.RuntimeFault as runtime_fault:
151             module.fail_json(msg=runtime_fault.msg)
152         except vmodl.MethodFault as method_fault:
153             module.fail_json(msg=method_fault.msg)
154 
155 
156     def state_remove_user(self):
157         try:
158             task = self.content.accountManager.RemoveUser(self.local_user_name)
159             self.module.exit_json(changed=True)
160         except vmodl.RuntimeFault as runtime_fault:
161             module.fail_json(msg=runtime_fault.msg)
162         except vmodl.MethodFault as method_fault:
163             module.fail_json(msg=method_fault.msg)
164 
165 
166     def state_exit_unchanged(self):
167         self.module.exit_json(changed=False)
168 
169 
170 
171 def main():
172 
173     argument_spec = vmware_argument_spec()
174     argument_spec.update(dict(local_user_name=dict(required=True, type='str'),
175                               local_user_password=dict(required=False, type='str', no_log=True),
176                               local_user_description=dict(required=False, type='str'),
177                               state=dict(default='present', choices=['present', 'absent'], type='str')))
178 
179     module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
180 
181     if not HAS_PYVMOMI:
182         module.fail_json(msg='pyvmomi is required for this module')
183 
184     vmware_local_user_manager = VMwareLocalUserManager(module)
185     vmware_local_user_manager.process_state()
186 
187 from ansible.module_utils.vmware import *
188 from ansible.module_utils.basic import *
189 
190 if __name__ == '__main__':
191     main()
192
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/cloud/vmware/vmware_local_user_manager.py b/cloud/vmware/vmware_local_user_manager.py
--- a/cloud/vmware/vmware_local_user_manager.py
+++ b/cloud/vmware/vmware_local_user_manager.py
@@ -137,9 +137,9 @@
             task = self.content.accountManager.CreateUser(account_spec)
             self.module.exit_json(changed=True)
         except vmodl.RuntimeFault as runtime_fault:
-            module.fail_json(msg=runtime_fault.msg)
+            self.module.fail_json(msg=runtime_fault.msg)
         except vmodl.MethodFault as method_fault:
-            module.fail_json(msg=method_fault.msg)
+            self.module.fail_json(msg=method_fault.msg)
 
     def state_update_user(self):
         account_spec = self.create_account_spec()
@@ -148,9 +148,9 @@
             task = self.content.accountManager.UpdateUser(account_spec)
             self.module.exit_json(changed=True)
         except vmodl.RuntimeFault as runtime_fault:
-            module.fail_json(msg=runtime_fault.msg)
+            self.module.fail_json(msg=runtime_fault.msg)
         except vmodl.MethodFault as method_fault:
-            module.fail_json(msg=method_fault.msg)
+            self.module.fail_json(msg=method_fault.msg)
 
 
     def state_remove_user(self):
@@ -158,9 +158,9 @@
             task = self.content.accountManager.RemoveUser(self.local_user_name)
             self.module.exit_json(changed=True)
         except vmodl.RuntimeFault as runtime_fault:
-            module.fail_json(msg=runtime_fault.msg)
+            self.module.fail_json(msg=runtime_fault.msg)
         except vmodl.MethodFault as method_fault:
-            module.fail_json(msg=method_fault.msg)
+            self.module.fail_json(msg=method_fault.msg)
 
 
     def state_exit_unchanged(self):

{"golden_diff": "diff --git a/cloud/vmware/vmware_local_user_manager.py b/cloud/vmware/vmware_local_user_manager.py\n--- a/cloud/vmware/vmware_local_user_manager.py\n+++ b/cloud/vmware/vmware_local_user_manager.py\n@@ -137,9 +137,9 @@\n             task = self.content.accountManager.CreateUser(account_spec)\n             self.module.exit_json(changed=True)\n         except vmodl.RuntimeFault as runtime_fault:\n-            module.fail_json(msg=runtime_fault.msg)\n+            self.module.fail_json(msg=runtime_fault.msg)\n         except vmodl.MethodFault as method_fault:\n-            module.fail_json(msg=method_fault.msg)\n+            self.module.fail_json(msg=method_fault.msg)\n \n     def state_update_user(self):\n         account_spec = self.create_account_spec()\n@@ -148,9 +148,9 @@\n             task = self.content.accountManager.UpdateUser(account_spec)\n             self.module.exit_json(changed=True)\n         except vmodl.RuntimeFault as runtime_fault:\n-            module.fail_json(msg=runtime_fault.msg)\n+            self.module.fail_json(msg=runtime_fault.msg)\n         except vmodl.MethodFault as method_fault:\n-            module.fail_json(msg=method_fault.msg)\n+            self.module.fail_json(msg=method_fault.msg)\n \n \n     def state_remove_user(self):\n@@ -158,9 +158,9 @@\n             task = self.content.accountManager.RemoveUser(self.local_user_name)\n             self.module.exit_json(changed=True)\n         except vmodl.RuntimeFault as runtime_fault:\n-            module.fail_json(msg=runtime_fault.msg)\n+            self.module.fail_json(msg=runtime_fault.msg)\n         except vmodl.MethodFault as method_fault:\n-            module.fail_json(msg=method_fault.msg)\n+            self.module.fail_json(msg=method_fault.msg)\n \n \n     def state_exit_unchanged(self):\n", "issue": "vmware_local_user_manager error: 'module' not defined\n##### ISSUE TYPE\n- Bug Report\n##### COMPONENT NAME\n\nvmware_local_user_manager\n##### ANSIBLE VERSION\n\n```\nansible 2.2.0 (devel 321d2e8cee) last updated 2016/08/21 14:29:27 (GMT +200)\n  lib/ansible/modules/core: (detached HEAD 91a839f1e3) last updated 2016/08/21 14:32:29 (GMT +200)\n  lib/ansible/modules/extras: (detached HEAD 1aeb9f8a8c) last updated 2016/08/21 14:32:44 (GMT +200)\n```\n##### CONFIGURATION\n\nNone\n##### OS / ENVIRONMENT\n\nExecution from: Ubuntu 14.04\nExecution to: VMware ESXi 6.0 U2\n##### SUMMARY\n\nExecution of module vmware_local_user_manager fails with error\n##### STEPS TO REPRODUCE\n\n```\n- name: \"vSphere ESXi: Add users\"\n       vmware_local_user_manager: validate_certs=False hostname={{ inventory_hostname }} username=root password=mypassword local_user_name=foo local_user_password=bar\n```\n##### EXPECTED RESULTS\n\nCreate user on target system\n##### ACTUAL RESULTS\n\nError:\n\n```\nTASK [vSphere ESXi: Add users] *************************************************\ntask path: /home/devel/ansible-configuration/vmware.yml:15\nUsing module file /home/devel/ansible/lib/ansible/modules/extras/cloud/vmware/vmware_local_user_manager.py\n<esxi> ESTABLISH LOCAL CONNECTION FOR USER: devel\n<esxi> EXEC /bin/sh -c '( umask 77 && mkdir -p \"` echo $HOME/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382 `\" && echo ansible-tmp-1471786926.92-121489380863382=\"` echo $HOME/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382 `\" ) && sleep 0'\n<esxi> PUT /tmp/tmpHVuXHh TO /home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/vmware_local_user_manager.py\n<esxi> EXEC /bin/sh -c 'chmod u+x /home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/ /home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/vmware_local_user_manager.py && sleep 0'\n<esxi> EXEC /bin/sh -c '/usr/bin/python /home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/vmware_local_user_manager.py; rm -rf \"/home/devel/.ansible/tmp/ansible-tmp-1471786926.92-121489380863382/\" > /dev/null 2>&1 && sleep 0'\nfatal: [esxi]: FAILED! => {\n    \"changed\": false, \n    \"failed\": true, \n    \"invocation\": {\n        \"module_args\": {\n            \"hostname\": \"esxi\", \n            \"local_user_description\": null, \n            \"local_user_name\": \"VALUE_SPECIFIED_IN_NO_LOG_PARAMETER\", \n            \"local_user_password\": \"VALUE_SPECIFIED_IN_NO_LOG_PARAMETER\", \n            \"password\": \"VALUE_SPECIFIED_IN_NO_LOG_PARAMETER\", \n            \"state\": \"present\", \n            \"username\": \"devel\", \n            \"validate_certs\": false\n        }, \n        \"module_name\": \"vmware_local_user_manager\"\n    }, \n    \"msg\": \"global name 'module' is not defined\"\n}\n```\n\n", "before_files": [{"content": "#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\n# Copyright IBM Corp. 2016\n# Author(s): Andreas Nafpliotis <nafpliot@de.ibm.com>\n\n# This file is part of Ansible\n#\n# Ansible is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# Ansible is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with Ansible.  If not, see <http://www.gnu.org/licenses/\n\nDOCUMENTATION = '''\n---\nmodule: vmware_local_user_manager\nshort_description: Manage local users on an ESXi host\ndescription:\n    - Manage local users on an ESXi host\nversion_added: \"2.2\"\nauthor: Andreas Nafpliotis\nnotes:\n    - Tested on ESXi 6.0\n    - Be sure that the ESXi user used for login, has the appropriate rights to create / delete / edit users\nrequirements:\n    - \"python >= 2.6\"\n    - PyVmomi installed\noptions:\n    local_user_name:\n        description:\n            - The local user name to be changed\n        required: True\n    local_user_password:\n        description:\n            - The password to be set\n        required: False\n    local_user_description:\n        description:\n            - Description for the user\n        required: False\n    state:\n        description:\n            - Indicate desired state of the user. If the user already exists when C(state=present), the user info is updated\n        choices: ['present', 'absent']\n        default: present\nextends_documentation_fragment: vmware.documentation\n'''\n\nEXAMPLES = '''\n# Example vmware_local_user_manager command from Ansible Playbooks\n- name: Add local user to ESXi\n  local_action:\n      module: vmware_local_user_manager\n      hostname: esxi_hostname\n      username: root\n      password: vmware\n      local_user_name: foo\n'''\n\nRETURN = '''# '''\n\ntry:\n    from pyVmomi import vim, vmodl\n    HAS_PYVMOMI = True\nexcept ImportError:\n    HAS_PYVMOMI = False\n\n\nclass VMwareLocalUserManager(object):\n    def __init__(self, module):\n        self.module = module\n        self.content = connect_to_api(self.module)\n        self.local_user_name = self.module.params['local_user_name']\n        self.local_user_password = self.module.params['local_user_password']\n        self.local_user_description = self.module.params['local_user_description']\n        self.state = self.module.params['state']\n\n    def process_state(self):\n        try:\n            local_account_manager_states = {\n                'absent': {\n                   'present': self.state_remove_user,\n                   'absent': self.state_exit_unchanged,\n                },\n                'present': {\n                    'present': self.state_update_user,\n                    'absent': self.state_create_user,\n                }\n            }\n\n            local_account_manager_states[self.state][self.check_local_user_manager_state()]()\n        except vmodl.RuntimeFault as runtime_fault:\n            self.module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            self.module.fail_json(msg=method_fault.msg)\n        except Exception as e:\n            self.module.fail_json(msg=str(e))\n\n\n    def check_local_user_manager_state(self):\n        user_account = self.find_user_account()\n        if not user_account:\n            return 'absent'\n        else:\n            return 'present'\n\n\n    def find_user_account(self):\n        searchStr = self.local_user_name\n        exactMatch = True\n        findUsers = True\n        findGroups = False\n        user_account = self.content.userDirectory.RetrieveUserGroups(None, searchStr, None, None, exactMatch, findUsers, findGroups)\n        return user_account\n\n\n    def create_account_spec(self):\n        account_spec = vim.host.LocalAccountManager.AccountSpecification()\n        account_spec.id = self.local_user_name\n        account_spec.password = self.local_user_password\n        account_spec.description = self.local_user_description\n        return account_spec\n\n\n    def state_create_user(self):\n        account_spec = self.create_account_spec()\n\n        try:\n            task = self.content.accountManager.CreateUser(account_spec)\n            self.module.exit_json(changed=True)\n        except vmodl.RuntimeFault as runtime_fault:\n            module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            module.fail_json(msg=method_fault.msg)\n\n    def state_update_user(self):\n        account_spec = self.create_account_spec()\n\n        try:\n            task = self.content.accountManager.UpdateUser(account_spec)\n            self.module.exit_json(changed=True)\n        except vmodl.RuntimeFault as runtime_fault:\n            module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            module.fail_json(msg=method_fault.msg)\n\n\n    def state_remove_user(self):\n        try:\n            task = self.content.accountManager.RemoveUser(self.local_user_name)\n            self.module.exit_json(changed=True)\n        except vmodl.RuntimeFault as runtime_fault:\n            module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            module.fail_json(msg=method_fault.msg)\n\n\n    def state_exit_unchanged(self):\n        self.module.exit_json(changed=False)\n\n\n\ndef main():\n\n    argument_spec = vmware_argument_spec()\n    argument_spec.update(dict(local_user_name=dict(required=True, type='str'),\n                              local_user_password=dict(required=False, type='str', no_log=True),\n                              local_user_description=dict(required=False, type='str'),\n                              state=dict(default='present', choices=['present', 'absent'], type='str')))\n\n    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)\n\n    if not HAS_PYVMOMI:\n        module.fail_json(msg='pyvmomi is required for this module')\n\n    vmware_local_user_manager = VMwareLocalUserManager(module)\n    vmware_local_user_manager.process_state()\n\nfrom ansible.module_utils.vmware import *\nfrom ansible.module_utils.basic import *\n\nif __name__ == '__main__':\n    main()\n", "path": "cloud/vmware/vmware_local_user_manager.py"}], "after_files": [{"content": "#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\n# Copyright IBM Corp. 2016\n# Author(s): Andreas Nafpliotis <nafpliot@de.ibm.com>\n\n# This file is part of Ansible\n#\n# Ansible is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# Ansible is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with Ansible.  If not, see <http://www.gnu.org/licenses/\n\nDOCUMENTATION = '''\n---\nmodule: vmware_local_user_manager\nshort_description: Manage local users on an ESXi host\ndescription:\n    - Manage local users on an ESXi host\nversion_added: \"2.2\"\nauthor: Andreas Nafpliotis\nnotes:\n    - Tested on ESXi 6.0\n    - Be sure that the ESXi user used for login, has the appropriate rights to create / delete / edit users\nrequirements:\n    - \"python >= 2.6\"\n    - PyVmomi installed\noptions:\n    local_user_name:\n        description:\n            - The local user name to be changed\n        required: True\n    local_user_password:\n        description:\n            - The password to be set\n        required: False\n    local_user_description:\n        description:\n            - Description for the user\n        required: False\n    state:\n        description:\n            - Indicate desired state of the user. If the user already exists when C(state=present), the user info is updated\n        choices: ['present', 'absent']\n        default: present\nextends_documentation_fragment: vmware.documentation\n'''\n\nEXAMPLES = '''\n# Example vmware_local_user_manager command from Ansible Playbooks\n- name: Add local user to ESXi\n  local_action:\n      module: vmware_local_user_manager\n      hostname: esxi_hostname\n      username: root\n      password: vmware\n      local_user_name: foo\n'''\n\nRETURN = '''# '''\n\ntry:\n    from pyVmomi import vim, vmodl\n    HAS_PYVMOMI = True\nexcept ImportError:\n    HAS_PYVMOMI = False\n\n\nclass VMwareLocalUserManager(object):\n    def __init__(self, module):\n        self.module = module\n        self.content = connect_to_api(self.module)\n        self.local_user_name = self.module.params['local_user_name']\n        self.local_user_password = self.module.params['local_user_password']\n        self.local_user_description = self.module.params['local_user_description']\n        self.state = self.module.params['state']\n\n    def process_state(self):\n        try:\n            local_account_manager_states = {\n                'absent': {\n                   'present': self.state_remove_user,\n                   'absent': self.state_exit_unchanged,\n                },\n                'present': {\n                    'present': self.state_update_user,\n                    'absent': self.state_create_user,\n                }\n            }\n\n            local_account_manager_states[self.state][self.check_local_user_manager_state()]()\n        except vmodl.RuntimeFault as runtime_fault:\n            self.module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            self.module.fail_json(msg=method_fault.msg)\n        except Exception as e:\n            self.module.fail_json(msg=str(e))\n\n\n    def check_local_user_manager_state(self):\n        user_account = self.find_user_account()\n        if not user_account:\n            return 'absent'\n        else:\n            return 'present'\n\n\n    def find_user_account(self):\n        searchStr = self.local_user_name\n        exactMatch = True\n        findUsers = True\n        findGroups = False\n        user_account = self.content.userDirectory.RetrieveUserGroups(None, searchStr, None, None, exactMatch, findUsers, findGroups)\n        return user_account\n\n\n    def create_account_spec(self):\n        account_spec = vim.host.LocalAccountManager.AccountSpecification()\n        account_spec.id = self.local_user_name\n        account_spec.password = self.local_user_password\n        account_spec.description = self.local_user_description\n        return account_spec\n\n\n    def state_create_user(self):\n        account_spec = self.create_account_spec()\n\n        try:\n            task = self.content.accountManager.CreateUser(account_spec)\n            self.module.exit_json(changed=True)\n        except vmodl.RuntimeFault as runtime_fault:\n            self.module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            self.module.fail_json(msg=method_fault.msg)\n\n    def state_update_user(self):\n        account_spec = self.create_account_spec()\n\n        try:\n            task = self.content.accountManager.UpdateUser(account_spec)\n            self.module.exit_json(changed=True)\n        except vmodl.RuntimeFault as runtime_fault:\n            self.module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            self.module.fail_json(msg=method_fault.msg)\n\n\n    def state_remove_user(self):\n        try:\n            task = self.content.accountManager.RemoveUser(self.local_user_name)\n            self.module.exit_json(changed=True)\n        except vmodl.RuntimeFault as runtime_fault:\n            self.module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            self.module.fail_json(msg=method_fault.msg)\n\n\n    def state_exit_unchanged(self):\n        self.module.exit_json(changed=False)\n\n\n\ndef main():\n\n    argument_spec = vmware_argument_spec()\n    argument_spec.update(dict(local_user_name=dict(required=True, type='str'),\n                              local_user_password=dict(required=False, type='str', no_log=True),\n                              local_user_description=dict(required=False, type='str'),\n                              state=dict(default='present', choices=['present', 'absent'], type='str')))\n\n    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)\n\n    if not HAS_PYVMOMI:\n        module.fail_json(msg='pyvmomi is required for this module')\n\n    vmware_local_user_manager = VMwareLocalUserManager(module)\n    vmware_local_user_manager.process_state()\n\nfrom ansible.module_utils.vmware import *\nfrom ansible.module_utils.basic import *\n\nif __name__ == '__main__':\n    main()\n", "path": "cloud/vmware/vmware_local_user_manager.py"}]}