Repository: ansible/ansible

Issue:
Cannot delete user without login profile using `iam` module
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
When trying to delete a user without a login profile an error is returned.

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
`iam`

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```ansible 2.7.0.dev0 (devel 54a20f64fc) last updated 2018/07/24 13:40:56 (GMT +200)
  config file = None
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/documents/programming/ansible/lib/ansible
  executable location = /home/user/.venv-asf/bin/ansible
  python version = 3.6.5 (default, May 11 2018, 04:00:52) [GCC 8.1.0]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->


##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
N/A

##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->
We just try to make a user without login profile absent. I don't really know what 'user without login profile' means, but using the same task with `present` instead of `absent` seems to create one.

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Absent iam user
      iam:
        iam_type: user
        state: absent
        name: username
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
User is deleted.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
It fails with error: Login Profile for User username cannot be found.

Sorry for not providing much info but sensitive data is all over the place and I'm afraid I might provide more than I should. If you need more info then let me know.

##### ADDITIONAL INFO
Looking at the code, it seems that there's a check to avoid this error already, but it's wrong (possibly the message changed?)

https://github.com/ansible/ansible/blob/4f1746ee1d4bd15af390e8bc7bf3f9c67163455a/lib/ansible/modules/cloud/amazon/iam.py#L257

But the actual error message is not anything with `Cannot find Login Profile`, but actually: `Login Profile for User username cannot be found.`.

I'm now tring to create a fix by checking for this error instead, I hope that's ok.


Discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/amazon/iam.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/amazon/iam.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @defionscode
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Hi Uwila,

Thank you for the issue, just so you are aware we have a dedicated Working Group for aws.
You can find other people interested in this in #ansible-aws on Freenode IRC
For more information about communities, meetings and agendas see https://github.com/ansible/community

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: community_workgroups --->

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/iam.py b/lib/ansible/modules/cloud/amazon/iam.py
--- a/lib/ansible/modules/cloud/amazon/iam.py
+++ b/lib/ansible/modules/cloud/amazon/iam.py
@@ -254,7 +254,7 @@ def delete_dependencies_first(module, iam, name):
         changed = True
     except boto.exception.BotoServerError as err:
         error_msg = boto_exception(err)
-        if 'Cannot find Login Profile' not in error_msg:
+        if 'Login Profile for User ' + name + ' cannot be found.' not in error_msg:
             module.fail_json(changed=changed, msg="Failed to delete login profile: %s" % err, exception=traceback.format_exc())
 
     # try to detach policies


Test patch: