Repository: ansible/ansible-modules-core

Issue:
Can't set stickiness on ELBs (ec2_elb_lb)
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

ec2_elb_lb
##### ANSIBLE VERSION

```
ansible 2.1.0.0
  config file = 
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
##### OS / ENVIRONMENT

Linux Mint 17
Python 2.7.6
boto 2.40.0
##### SUMMARY

When creating an ELB with a stickiness policy, the file name and the policy type are used to create a policy name. Policy names may only contain [A-Za-z0-9-], but since upgrading to Ansible 2.1.0, the filename now contains [.], which causes AWS to return a ValidationError through Boto.
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

```
      - name: Create ELBs
        ec2_elb_lb:
            name: "{{ item.name }}"
            security_group_names: [ "{{ item.group }}" ]
            subnets: "{{ public.results|map(attribute='subnet.id')|list }}"
            cross_az_load_balancing: "yes"
            idle_timeout: 60
            listeners:
                - protocol: http
                  load_balancer_port: 80
                  instance_port: 81
                - protocol: https
                  load_balancer_port: 443
                  instance_protocol: http
                  instance_port: 80
                  ssl_certificate_id: "{{ item.cert }}"
                - protocol: https
                  load_balancer_port: 8080
                  instance_protocol: http
                  instance_port: 8080
                  ssl_certificate_id: "{{ item.cert }}"
                - protocol: https
                  load_balancer_port: 8443
                  instance_protocol: http
                  instance_port: 8443
                  ssl_certificate_id: "{{ item.cert }}"
            health_check:
                ping_protocol: tcp
                ping_port: 80
                response_timeout: 10
                interval: 15
                unhealthy_threshold: 10
                healthy_threshold: 2
            stickiness:
                type: loadbalancer
                enabled: yes
                expiration: 3600
            state: present
        with_items: "{{ load_balancers }}"
        register: elbs
```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

ELBs created.
##### ACTUAL RESULTS

No ELBs. Error:

```
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py", line 1328, in <module>
    main()
  File "/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py", line 1312, in main
    elb_man.ensure_ok()
  File "/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py", line 410, in _do_op
    return op(*args, **kwargs)
  File "/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py", line 493, in ensure_ok
    self.select_stickiness_policy()
  File "/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py", line 1042, in select_stickiness_policy
    self._set_stickiness_policy(elb_info, listeners_dict, policy, **policy_attrs)
  File "/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py", line 1005, in _set_stickiness_policy
    self._create_policy(policy_attrs['param_value'], policy_attrs['method'], policy[0])
  File "/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py", line 982, in _create_policy
    getattr(self.elb_conn, policy_meth )(policy_param, self.elb.name, policy)
  File "/usr/local/lib/python2.7/dist-packages/boto/ec2/elb/__init__.py", line 637, in create_lb_cookie_stickiness_policy
    return self.get_status('CreateLBCookieStickinessPolicy', params)
  File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1227, in get_status
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.BotoServerError: BotoServerError: 400 Bad Request
<ErrorResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
  <Error>
    <Type>Sender</Type>
    <Code>ValidationError</Code>
    <Message>Policy name cannot contain characters that are not letters, or digits or the dash.</Message>
  </Error>
  <RequestId>a1cc6c32-2759-11e6-96db-198531c49883</RequestId>
</ErrorResponse>


failed: [localhost] (item={u'cert': u'REDACTED', u'group': u'REDACTED', u'name': u'REDACTED'}) => {"failed": true, "invocation": {"module_name": "ec2_elb_lb"}, "item": {"cert": "REDACTED", "group": "REDACTED", "name": "REDACTED"}, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py\", line 1328, in <module>\n    main()\n  File \"/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py\", line 1312, in main\n    elb_man.ensure_ok()\n  File \"/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py\", line 410, in _do_op\n    return op(*args, **kwargs)\n  File \"/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py\", line 493, in ensure_ok\n    self.select_stickiness_policy()\n  File \"/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py\", line 1042, in select_stickiness_policy\n    self._set_stickiness_policy(elb_info, listeners_dict, policy, **policy_attrs)\n  File \"/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py\", line 1005, in _set_stickiness_policy\n    self._create_policy(policy_attrs['param_value'], policy_attrs['method'], policy[0])\n  File \"/tmp/ansible_MauX1F/ansible_module_ec2_elb_lb.py\", line 982, in _create_policy\n    getattr(self.elb_conn, policy_meth )(policy_param, self.elb.name, policy)\n  File \"/usr/local/lib/python2.7/dist-packages/boto/ec2/elb/__init__.py\", line 637, in create_lb_cookie_stickiness_policy\n    return self.get_status('CreateLBCookieStickinessPolicy', params)\n  File \"/usr/local/lib/python2.7/dist-packages/boto/connection.py\", line 1227, in get_status\n    raise self.ResponseError(response.status, response.reason, body)\nboto.exception.BotoServerError: BotoServerError: 400 Bad Request\n<ErrorResponse xmlns=\"http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/\">\n  <Error>\n    <Type>Sender</Type>\n    <Code>ValidationError</Code>\n    <Message>Policy name cannot contain characters that are not letters, or digits or the dash.</Message>\n  </Error>\n  <RequestId>a1cc6c32-2759-11e6-96db-198531c49883</RequestId>\n</ErrorResponse>\n\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}
```



Discussion:

Resolving patch:
diff --git a/cloud/amazon/ec2_elb_lb.py b/cloud/amazon/ec2_elb_lb.py
--- a/cloud/amazon/ec2_elb_lb.py
+++ b/cloud/amazon/ec2_elb_lb.py
@@ -976,7 +976,7 @@ def _set_idle_timeout(self):
             self.elb_conn.modify_lb_attribute(self.name, 'ConnectingSettings', attributes.connecting_settings)
 
     def _policy_name(self, policy_type):
-        return __file__.split('/')[-1].replace('_', '-')  + '-' + policy_type
+        return __file__.split('/')[-1].split('.')[0].replace('_', '-')  + '-' + policy_type
 
     def _create_policy(self, policy_param, policy_meth, policy):
         getattr(self.elb_conn, policy_meth )(policy_param, self.elb.name, policy)


Test patch: