Repository: ansible/ansible

Issue:
oc module extends instead of replaces a list
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
oc

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/luka/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, Jul 21 2017, 03:24:34) [GCC 7.1.1 20170630]

```

##### CONFIGURATION
default
<!---
If using Ansible 2.4 or above, paste 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 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.
-->
Linux / Manjaro

##### SUMMARY
<!--- Explain the problem briefly -->
oc module with inline definition expands list of triggers.
i.e. when set to empty list, it maintains old configuration, while having some triggers adds duplicate entry on each run.

##### 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.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Create OpenShift resource
  oc:
    host: "{{ inventory_hostname }}"
    validate_certs: false
    state: present
    inline: "{{ lookup('template', openshift_template) | from_yaml }}"
    token: "{{ namespace_token }}"
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
replaced list

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
extended list with duplicates / persisting old config




Discussion:
cc @kevensen
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I'm happy to take a look.  Can you please provide an example of the output you see?
needs_info
```yaml
  triggers:
  - type: ConfigChange
  - imageChangeParams:
      automatic: true
      containerNames:
      - gitlab-ci-build-on-merge
      from:
        kind: ImageStreamTag
        name: gitlab-ci-build-on-merge:latest
        namespace: gitlab-ci-runner-test
    type: ImageChange
  - imageChangeParams:
      automatic: true
      containerNames:
      - gitlab-ci-build-on-merge
      from:
        kind: ImageStreamTag
        name: gitlab-ci-build-on-merge:latest
        namespace: gitlab-ci-runner-test
    type: ImageChange
  - imageChangeParams:
      automatic: true
      containerNames:
      - gitlab-ci-build-on-merge
      from:
        kind: ImageStreamTag
        name: gitlab-ci-build-on-merge:latest
        namespace: gitlab-ci-runner-test
    type: ImageChange
```

Template for this deploymentconfig has triggers for image change and config change. After multiple runs of playbook, multiple ImageChange entries are added, while ConfigChange remains only one.

When using template that has explicitly set triggers to empty lists, the triggers remain the same.

As far as I can tell, the issue is [here](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/clustering/oc.py#L349-L355) when merging two sets.
Value should probably be overriden if it's defined in new config, and persisted if no new configuration is specified.
@lukadante thanks.  I'll take a look at this today.

Resolving patch:
diff --git a/lib/ansible/modules/clustering/oc.py b/lib/ansible/modules/clustering/oc.py
--- a/lib/ansible/modules/clustering/oc.py
+++ b/lib/ansible/modules/clustering/oc.py
@@ -347,28 +347,9 @@ def merge(self, source, destination, changed):
                     _, changed = self.merge(value, node, changed)
 
             elif isinstance(value, list) and key in destination.keys():
-                try:
-                    if set(destination[key]) != set(destination[key] +
-                                                    source[key]):
-                        destination[key] = list(set(destination[key] +
-                                                    source[key]))
-                        changed = True
-                except TypeError:
-                    for new_dict in source[key]:
-                        found = False
-                        for old_dict in destination[key]:
-                            if ('name' in old_dict.keys() and
-                                    'name' in new_dict.keys()):
-                                if old_dict['name'] == new_dict['name']:
-                                    destination[key].remove(old_dict)
-                                    break
-                            if old_dict == new_dict:
-                                found = True
-                                break
-
-                        if not found:
-                            destination[key].append(new_dict)
-                            changed = True
+                if destination[key] != source[key]:
+                    destination[key] = source[key]
+                    changed = True
 
             elif (key not in destination.keys() or
                   destination[key] != source[key]):


Test patch: