Repository: ansible/ansible

Issue:
edgeos_config command diff only considers router's active config
<!---
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. -->

##### 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-->
edgeos_config

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.3
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/jan/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /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).-->
```
CACHE_PLUGIN(/home/jan/Programme/Ansible/Schumacherweg/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/home/jan/Programme/Ansible/Schumacherweg/ansible.cfg) = cache
CACHE_PLUGIN_TIMEOUT(/home/jan/Programme/Ansible/Schumacherweg/ansible.cfg) = 86400
DEFAULT_GATHERING(/home/jan/Programme/Ansible/Schumacherweg/ansible.cfg) = smart
DEFAULT_HOST_LIST(/home/jan/Programme/Ansible/Schumacherweg/ansible.cfg) = ['/home/jan/P>
DEFAULT_VAULT_PASSWORD_FILE(/home/jan/Programme/Ansible/Schumacherweg/ansible.cfg) = /ho>
RETRY_FILES_ENABLED(/home/jan/Programme/Ansible/Schumacherweg/ansible.cfg) = False
```

##### 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.-->
Running on Arch Linux, managing EdgeOS v1.10.1 router 

##### SUMMARY
<!--- Explain the problem briefly -->
When comparing the active config with the desired commands, effects of previously scheduled desired commands is not taken into account.

##### 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. -->
Make sure no dhcp-server is present. 
Create a command chain to set up some config, starting from a clean state. Example set-style config:

<!--- Paste example playbooks or commands between quotes below -->
```yaml
delete service dhcp-server
set service dhcp-server shared-network-name test
# ... possibly more instructions
```
Roll out with
```yaml
- edgeos_config:
    src: config-set.j2
```
Then roll out the playbook for a second time. 

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
When rolling out the playbook for the second time, no changes are applied to the system and no services are restarted. 

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
When rolling out for the second time, only the `delete` command is executed, leaving the router without any configured dhcp-server. 

-vvv output of the first run:
```yaml
changed: [ubnt01] => {
    "backup_path": "/home/jan/Programme/Ansible/Schumacherweg/roles/edgeos_dhcp_server/backup/ubnt01_config.2018-05-19@15:34:58",                                                 
    "changed": true,
    "commands": [
        "set service dhcp-server shared-network-name test"                                               
    ],
    "filtered": [],
    "invocation": {
        "module_args": {
            "backup": false,
            "comment": "configured by edgeos_config",
            "config": null,
            "lines": null,
            "match": "line",
            "save": false,
            "src": "delete service dhcp-server\nset service dhcp-server shared-network-name test"
        }
    }
}

```

-vvv output of the second run: 
```yaml
changed: [ubnt01] => {
    "backup_path": "/home/jan/Programme/Ansible/Schumacherweg/roles/edgeos_dhcp_server/backup/ubnt01_config.2018-05-19@15:34:09",                                                 
    "changed": true,
    "commands": [
        "delete service dhcp-server"
    ],
    "filtered": [],
    "invocation": {
        "module_args": {
            "backup": false,
            "comment": "configured by edgeos_config",
            "config": null,
            "lines": null,
            "match": "line",
            "save": false,
            "src": "delete service dhcp-server\nset service dhcp-server shared-network-name test"                        
        }
    }
}
```


Discussion:
Files identified in the description:
* [lib/ansible/modules/network/edgeos/edgeos_config.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/edgeos/edgeos_config.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 @qalthos @samdoran
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Thank you for reporting this.

I am able to duplicate this and have determined why it is happening. Working on a fix, though it's a bit difficult.

Resolving patch:
diff --git a/lib/ansible/modules/network/edgeos/edgeos_config.py b/lib/ansible/modules/network/edgeos/edgeos_config.py
--- a/lib/ansible/modules/network/edgeos/edgeos_config.py
+++ b/lib/ansible/modules/network/edgeos/edgeos_config.py
@@ -163,6 +163,7 @@ def diff_config(commands, config):
 
     updates = list()
     visited = set()
+    delete_commands = [line for line in commands if line.startswith('delete')]
 
     for line in commands:
         item = str(line).replace("'", '')
@@ -170,8 +171,18 @@ def diff_config(commands, config):
         if not item.startswith('set') and not item.startswith('delete'):
             raise ValueError('line must start with either `set` or `delete`')
 
-        elif item.startswith('set') and item not in config:
-            updates.append(line)
+        elif item.startswith('set'):
+
+            if item not in config:
+                updates.append(line)
+
+            # If there is a corresponding delete command in the desired config, make sure to append
+            # the set command even though it already exists in the running config
+            else:
+                ditem = re.sub('set', 'delete', item)
+                for line in delete_commands:
+                    if ditem.startswith(line):
+                        updates.append(item)
 
         elif item.startswith('delete'):
             if not config:


Test patch: