diff --git a/lib/ansible/modules/network/nxos/nxos_feature.py b/lib/ansible/modules/network/nxos/nxos_feature.py index 336ee7085d6..aa27e4bfd32 100644 --- a/lib/ansible/modules/network/nxos/nxos_feature.py +++ b/lib/ansible/modules/network/nxos/nxos_feature.py @@ -65,54 +65,25 @@ EXAMPLES = ''' ''' RETURN = ''' -proposed: - description: proposed feature state - returned: always - type: dict - sample: {"state": "disabled"} -existing: - description: existing state of feature - returned: always - type: dict - sample: {"state": "enabled"} -end_state: - description: feature state after executing module - returned: always - type: dict - sample: {"state": "disabled"} -updates: - description: commands sent to the device +commands: + description: The set of commands to be sent to the remote device returned: always type: list - sample: ["no feature eigrp"] -changed: - description: check to see if a change was made on the device - returned: always - type: boolean - sample: true -feature: - description: the feature that has been examined - returned: always - type: string - sample: "vpc" + sample: ['nv overlay evpn'] ''' import re -from ansible.module_utils.nxos import load_config, run_commands -from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.nxos import load_config, run_commands +from ansible.module_utils.nxos import nxos_argument_spec +from ansible.module_utils.nxos import check_args as nxos_check_args -def apply_key_map(key_map, table): - new_dict = {} - for key, value in table.items(): - new_key = key_map.get(key) - if new_key: - value = table.get(key) - if value: - new_dict[new_key] = str(value) - else: - new_dict[new_key] = value - return new_dict +def check_args(module, warnings): + nxos_check_args(module, warnings) + + for key in ('include_defaults', 'config', 'save'): + if module.params[key] is not None: + warnings.append('argument %s is no longer supported, ignoring value' % key) def get_available_features(feature, module): @@ -121,6 +92,7 @@ def get_available_features(feature, module): command = 'show feature' command = {'command': command, 'output': 'text'} + body = run_commands(module, [command]) split_body = body[0].splitlines() @@ -212,11 +184,12 @@ def validate_feature(module, mode='show'): def main(): argument_spec = dict( feature=dict(type='str', required=True), - state=dict(choices=['enabled', 'disabled'], default='enabled', - required=False), - include_defaults=dict(default=False), + state=dict(choices=['enabled', 'disabled'], default='enabled'), + + # deprecated in Ans2.3 + include_defaults=dict(), config=dict(), - save=dict(type='bool', default=False) + save=dict() ) argument_spec.update(nxos_argument_spec) @@ -227,7 +200,6 @@ def main(): warnings = list() check_args(module, warnings) - feature = validate_feature(module) state = module.params['state'].lower() @@ -248,25 +220,15 @@ def main(): cmds = get_commands(proposed, existing, state, module) if cmds: - if module.check_mode: - module.exit_json(changed=True, commands=cmds) - else: + if not module.check_mode: load_config(module, cmds) - changed = True - updated_features = get_available_features(feature, module) - existstate = updated_features[feature] - end_state = dict(state=existstate) - if 'configure' in cmds: - cmds.pop(0) - - results = {} - results['proposed'] = proposed - results['existing'] = existing - results['end_state'] = end_state - results['updates'] = cmds - results['changed'] = changed - results['warnings'] = warnings - results['feature'] = module.params['feature'] + changed = True + + results = { + 'commands': cmds, + 'changed': changed, + 'warnings': warnings + } module.exit_json(**results) diff --git a/test/integration/nxos.yaml b/test/integration/nxos.yaml index 4c73ca02ef5..cc67ec35793 100644 --- a/test/integration/nxos.yaml +++ b/test/integration/nxos.yaml @@ -14,3 +14,4 @@ - { role: nxos_template, when: "limit_to in ['*', 'nxos_template']" } - { role: nxos_nxapi, when: "limit_to in ['*', 'nxos_nxapi']" } - { role: nxos_evpn_global, when: "limit_to in ['*', 'nxos_evpn_global']" } + - { role: nxos_feature, when: "limit_to in ['*', 'nxos_feature']" } diff --git a/test/integration/targets/nxos_feature/meta/main.yml b/test/integration/targets/nxos_feature/meta/main.yml new file mode 100644 index 00000000000..ae741cbdc71 --- /dev/null +++ b/test/integration/targets/nxos_feature/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nxos_tests diff --git a/test/integration/targets/nxos_feature/tasks/cli.yaml b/test/integration/targets/nxos_feature/tasks/cli.yaml new file mode 100644 index 00000000000..d675462dd02 --- /dev/null +++ b/test/integration/targets/nxos_feature/tasks/cli.yaml @@ -0,0 +1,15 @@ +--- +- name: collect all cli test cases + find: + paths: "{{ role_path }}/tests/cli" + patterns: "{{ testcase }}.yaml" + register: test_cases + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: run test case + include: "{{ test_case_to_run }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/nxos_feature/tasks/main.yaml b/test/integration/targets/nxos_feature/tasks/main.yaml new file mode 100644 index 00000000000..4b0f8c64d90 --- /dev/null +++ b/test/integration/targets/nxos_feature/tasks/main.yaml @@ -0,0 +1,3 @@ +--- +- { include: cli.yaml, tags: ['cli'] } +- { include: nxapi.yaml, tags: ['nxapi'] } diff --git a/test/integration/targets/nxos_feature/tasks/nxapi.yaml b/test/integration/targets/nxos_feature/tasks/nxapi.yaml new file mode 100644 index 00000000000..ea525379f7f --- /dev/null +++ b/test/integration/targets/nxos_feature/tasks/nxapi.yaml @@ -0,0 +1,28 @@ +--- +- name: collect all nxapi test cases + find: + paths: "{{ role_path }}/tests/nxapi" + patterns: "{{ testcase }}.yaml" + register: test_cases + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: enable nxapi + nxos_config: + lines: + - feature nxapi + - nxapi http port 80 + provider: "{{ cli }}" + +- name: run test case + include: "{{ test_case_to_run }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run + +- name: disable nxapi + nxos_config: + lines: + - no feature nxapi + provider: "{{ cli }}" diff --git a/test/integration/targets/nxos_feature/tests/cli/configure.yaml b/test/integration/targets/nxos_feature/tests/cli/configure.yaml new file mode 100644 index 00000000000..0eab6de0032 --- /dev/null +++ b/test/integration/targets/nxos_feature/tests/cli/configure.yaml @@ -0,0 +1,60 @@ +--- +- debug: msg="START cli/configure.yaml" + +- name: setup + nxos_config: + lines: no feature vn-segment-vlan-based + match: none + provider: "{{ cli }}" + +- name: enable vn-segment-vlan-based + nxos_feature: + feature: vn-segment-vlan-based + state: enabled + provider: "{{ cli }}" + register: result + +- assert: + that: + - "result.changed == true" + +- name: verify vn-segment-vlan-based + nxos_feature: + feature: vn-segment-vlan-based + state: enabled + provider: "{{ cli }}" + register: result + +- assert: + that: + - "result.changed == false" + +- name: disable vn-segment-vlan-based + nxos_feature: + feature: vn-segment-vlan-based + state: disabled + provider: "{{ cli }}" + register: result + +- assert: + that: + - "result.changed == true" + +- name: verify vn-segment-vlan-based + nxos_feature: + feature: vn-segment-vlan-based + state: disabled + provider: "{{ cli }}" + register: result + +- assert: + that: + - "result.changed == false" + +- name: teardown + nxos_config: + lines: no feature vn-segment-vlan-based + match: none + provider: "{{ cli }}" + +- debug: msg="END cli/configure.yaml" diff --git a/test/integration/targets/nxos_feature/tests/cli/invalid.yaml b/test/integration/targets/nxos_feature/tests/cli/invalid.yaml new file mode 100644 index 00000000000..7e198b61b27 --- /dev/null +++ b/test/integration/targets/nxos_feature/tests/cli/invalid.yaml @@ -0,0 +1,15 @@ +--- +- debug: msg="START cli/invalid.yaml" + +- name: configure invalid feature name + nxos_feature: + feature: invalid + provider: "{{ cli }}" + register: result + ignore_errors: yes + +- assert: + that: + - result.failed == true + +- debug: msg="END cli/invalid.yaml" diff --git a/test/integration/targets/nxos_feature/tests/nxapi/configure.yaml b/test/integration/targets/nxos_feature/tests/nxapi/configure.yaml new file mode 100644 index 00000000000..074b6c28133 --- /dev/null +++ b/test/integration/targets/nxos_feature/tests/nxapi/configure.yaml @@ -0,0 +1,60 @@ +--- +- debug: msg="START nxapi/configure.yaml" + +- name: setup + nxos_config: + lines: no feature vn-segment-vlan-based + match: none + provider: "{{ nxapi }}" + +- name: enable vn-segment-vlan-based + nxos_feature: + feature: vn-segment-vlan-based + state: enabled + provider: "{{ nxapi }}" + register: result + +- assert: + that: + - "result.changed == true" + +- name: verify vn-segment-vlan-based + nxos_feature: + feature: vn-segment-vlan-based + state: enabled + provider: "{{ nxapi }}" + register: result + +- assert: + that: + - "result.changed == false" + +- name: disable vn-segment-vlan-based + nxos_feature: + feature: vn-segment-vlan-based + state: disabled + provider: "{{ nxapi }}" + register: result + +- assert: + that: + - "result.changed == true" + +- name: verify vn-segment-vlan-based + nxos_feature: + feature: vn-segment-vlan-based + state: disabled + provider: "{{ nxapi }}" + register: result + +- assert: + that: + - "result.changed == false" + +- name: teardown + nxos_config: + lines: no feature vn-segment-vlan-based + match: none + provider: "{{ nxapi }}" + +- debug: msg="END nxapi/configure.yaml" diff --git a/test/integration/targets/nxos_feature/tests/nxapi/invalid.yaml b/test/integration/targets/nxos_feature/tests/nxapi/invalid.yaml new file mode 100644 index 00000000000..19223a20c3f --- /dev/null +++ b/test/integration/targets/nxos_feature/tests/nxapi/invalid.yaml @@ -0,0 +1,15 @@ +--- +- debug: msg="START nxapi/invalid.yaml" + +- name: configure invalid feature name + nxos_feature: + feature: invalid + provider: "{{ nxapi }}" + register: result + ignore_errors: yes + +- assert: + that: + - result.failed == true + +- debug: msg="END nxapi/invalid.yaml"