Repository: ansible/ansible

Issue:
Issue with junos_config module with order of statements
<!---
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 -->
modules/junos/junos_config

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
/glide/bin/ansible --version
ansible 2.2.0.0
  config file = /home/users/.ansible.cfg
  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).
-->
no changes.

##### 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.
-->
N/A

##### SUMMARY
<!--- Explain the problem briefly -->
I was using the junos_template module in 2.1 with pretty good results, but 2.2 merged junos_template into junos_config.  Good idea, but I don't think it's working right.  The first line is there to instantiate the ACL in case it doesn't already exist, in order to prevent any errors when executing the second line which blows away the existing ACL in order to replace it.  This worked using junos_template, but when I use junos_config it seems that the second 'delete' statement is not executing and I get this output:

```
[cns04.sea3:/glide/cns/ansible/playbook/deploy]$ ansible-playbook junos_config_test.yml --check --diff -i inventory --limit test

PLAY [Test junos_config module] ************************************************

TASK [junos_config ACL test] ***************************************************

[edit firewall family inet]
      filter ISP-INBOUND { ... }
+     filter ANSIBLE-TEST {
+         term ANSIBLE {
+             then accept;
+         }
+         term TEST-TERM-1 {
+             from {
+                 protocol udp;
+             }
+             then {
+                 log;
+                 discard;
+             }
+         }
+     }

changed: [router_name]

PLAY RECAP *********************************************************************
router_name : ok=1    changed=1    unreachable=0    failed=0   
```
I shouldn't see that ANSIBLE term in the diff if those lines execute in the proper order.   I should only see TEST-TERM-1.  Any ideas?

Basically it seems its not honoring the ordering the same case applies when using src: template.

##### 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: Test junos_config module
  gather_facts: no
  connection: local

  vars:
    cli:
      host: "{{ inventory_hostname }}"

  tasks:
    - name: junos_config ACL test
      junos_config:
        provider: "{{ cli }}"
        backup: yes
        lines:
          - set firewall family inet filter ANSIBLE-TEST term ANSIBLE then accept
          - delete firewall family inet filter ANSIBLE-TEST
          - set firewall family inet filter ANSIBLE-TEST term TEST-TERM-1 from protocol udp
          - set firewall family inet filter ANSIBLE-TEST term TEST-TERM-1 then log
          - set firewall family inet filter ANSIBLE-TEST term TEST-TERM-1 then discard

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
the ordering of the lines or config to be honored.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:
cc @privateip @gundalow @Qalthos @ganeshnalawade
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/network/junos/junos_config.py b/lib/ansible/modules/network/junos/junos_config.py
--- a/lib/ansible/modules/network/junos/junos_config.py
+++ b/lib/ansible/modules/network/junos/junos_config.py
@@ -237,7 +237,7 @@ def diff_commands(commands, config):
     updates = list()
     visited = set()
 
-    for item in commands:
+    for index, item in enumerate(commands):
         if len(item) > 0:
             if not item.startswith('set') and not item.startswith('delete'):
                 raise ValueError('line must start with either `set` or `delete`')
@@ -246,7 +246,9 @@ def diff_commands(commands, config):
                 updates.append(item)
 
             elif item.startswith('delete'):
-                for entry in config:
+                for entry in config + commands[0:index]:
+                    if entry.startswith('set'):
+                        entry = entry[4:]
                     if entry.startswith(item[7:]) and item not in visited:
                         updates.append(item)
                         visited.add(item)


Test patch: