diff --git a/lib/ansible/modules/remote_management/ucs/ucs_vhba_template.py b/lib/ansible/modules/remote_management/ucs/ucs_vhba_template.py new file mode 100644 index 0000000000..3f493bb9ed --- /dev/null +++ b/lib/ansible/modules/remote_management/ucs/ucs_vhba_template.py @@ -0,0 +1,260 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + +DOCUMENTATION = r''' +--- +module: ucs_vhba_template +short_description: Configures vHBA templates on Cisco UCS Manager +description: +- Configures vHBA templates on Cisco UCS Manager. +- Examples can be used with the UCS Platform Emulator U(https://communities.cisco.com/ucspe). +extends_documentation_fragment: ucs +options: + state: + description: + - If C(present), will verify vHBA templates are present and will create if needed. + - If C(absent), will verify vHBA templates are absent and will delete if needed. + choices: [present, absent] + default: present + name: + description: + - Name of the vHBA template + required: yes + description: + description: + - Description for the vHBA template + aliases: [ descr ] + fabric: + description: + - Fabric ID + choices: [A, B] + default: A + redundancy_type: + description: + - Redundancy Type + choices: [none, primary, secondary] + default: none + vsan: + description: + - VSAN name + default: default + template_type: + description: + - Template Type + choices: [initial-template, updating-template] + default: initial-template + max_data: + description: + - Max Data Field Size + default: '2048' + wwpn_pool: + description: + - WWPN Pool name + default: default + qos_policy: + description: + - QoS Policy name + pin_group: + description: + - Pin Group + stats_policy: + description: + - Stats Threshold Policy name + default: default + org_dn: + description: + - Org dn (distinguished name) + default: org-root +requirements: +- ucsmsdk +author: +- David Soper (@dsoper2) +- CiscoUcs (@CiscoUcs) +version_added: '2.5' +''' + +EXAMPLES = r''' +- name: Configure vHBA template + ucs_vhba_template: + hostname: 172.16.143.150 + username: admin + password: password + name: vHBA-A + fabric: A + vsan: VSAN-A + wwpn_pool: WWPN-Pool-A + +- name: Remote vHBA template + ucs_vhba_template: + hostname: 172.16.143.150 + username: admin + password: password + name: vHBA-A + state: absent +''' + +RETURN = r''' +# +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.remote_management.ucs import UCSModule, ucs_argument_spec + + +def main(): + argument_spec = ucs_argument_spec + argument_spec.update( + org_dn=dict(type='str', default='org-root'), + name=dict(type='str'), + descr=dict(type='str'), + fabric=dict(type='str', default='A', choices=['A', 'B']), + redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']), + vsan=dict(type='str', default='default'), + template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']), + max_data=dict(type='str', default='2048'), + wwpn_pool=dict(type='str', default='default'), + qos_policy=dict(type='str'), + pin_group=dict(type='str'), + stats_policy=dict(type='str', default='default'), + state=dict(type='str', default='present', choices=['present', 'absent']), + vhba_template_list=dict(type='list'), + ) + + # Note that use of vhba_template_list is an experimental feature which allows multiple resource updates with a single UCSM connection. + # Support for vhba_template_list may change or be removed once persistent UCS connections are supported. + # Either vhba_template_list or name is required (user can specify either a list of single resource). + + module = AnsibleModule( + argument_spec, + supports_check_mode=True, + required_one_of=[ + ['vhba_template_list', 'name'] + ], + mutually_exclusive=[ + ['vhba_template_list', 'name'] + ], + ) + ucs = UCSModule(module) + + err = False + + from ucsmsdk.mometa.vnic.VnicSanConnTempl import VnicSanConnTempl + from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf + + changed = False + try: + # Only documented use is a single resource, but to also support experimental + # feature allowing multiple updates all params are converted to a vhba_template_list below. + + if module.params['vhba_template_list']: + # directly use the list (single resource and list are mutually exclusive + vhba_template_list = module.params['vhba_template_list'] + else: + # single resource specified, create list from the current params + vhba_template_list = [module.params] + for vhba_template in vhba_template_list: + mo_exists = False + props_match = False + # set default params. Done here to set values for lists which can't be done in the argument_spec + if not vhba_template.get('descr'): + vhba_template['descr'] = '' + if not vhba_template.get('fabric'): + vhba_template['fabric'] = 'A' + if not vhba_template.get('redundancy_type'): + vhba_template['redundancy_type'] = 'none' + if not vhba_template.get('vsan'): + vhba_template['vsan'] = 'default' + if not vhba_template.get('template_type'): + vhba_template['template_type'] = 'initial-template' + if not vhba_template.get('max_data'): + vhba_template['max_data'] = '2048' + if not vhba_template.get('wwpn_pool'): + vhba_template['wwpn_pool'] = 'default' + if not vhba_template.get('qos_policy'): + vhba_template['qos_policy'] = '' + if not vhba_template.get('pin_group'): + vhba_template['pin_group'] = '' + if not vhba_template.get('stats_policy'): + vhba_template['stats_policy'] = 'default' + # dn is /san-conn-templ- + dn = module.params['org_dn'] + '/san-conn-templ-' + vhba_template['name'] + + mo = ucs.login_handle.query_dn(dn) + if mo: + mo_exists = True + # check top-level mo props + kwargs = {} + kwargs['descr'] = vhba_template['descr'] + kwargs['switch_id'] = vhba_template['fabric'] + kwargs['redundancy_pair_type'] = vhba_template['redundancy_type'] + kwargs['templ_type'] = vhba_template['template_type'] + kwargs['max_data_field_size'] = vhba_template['max_data'] + kwargs['ident_pool_name'] = vhba_template['wwpn_pool'] + kwargs['qos_policy_name'] = vhba_template['qos_policy'] + kwargs['pin_to_group_name'] = vhba_template['pin_group'] + kwargs['stats_policy_name'] = vhba_template['stats_policy'] + if (mo.check_prop_match(**kwargs)): + # top-level props match, check next level mo/props + child_dn = dn + '/if-default' + mo_1 = ucs.login_handle.query_dn(child_dn) + if mo_1: + kwargs = {} + kwargs['name'] = vhba_template['vsan'] + if (mo_1.check_prop_match(**kwargs)): + props_match = True + + if module.params['state'] == 'absent': + # mo must exist but all properties do not have to match + if mo_exists: + if not module.check_mode: + ucs.login_handle.remove_mo(mo) + ucs.login_handle.commit() + changed = True + else: + if not props_match: + if not module.check_mode: + # create if mo does not already exist + mo = VnicSanConnTempl( + parent_mo_or_dn=module.params['org_dn'], + name=vhba_template['name'], + descr=vhba_template['descr'], + switch_id=vhba_template['fabric'], + redundancy_pair_type=vhba_template['redundancy_type'], + templ_type=vhba_template['template_type'], + max_data_field_size=vhba_template['max_data'], + ident_pool_name=vhba_template['wwpn_pool'], + qos_policy_name=vhba_template['qos_policy'], + pin_to_group_name=vhba_template['pin_group'], + stats_policy_name=vhba_template['stats_policy'], + ) + + mo_1 = VnicFcIf( + parent_mo_or_dn=mo, + name=vhba_template['vsan'], + ) + + ucs.login_handle.add_mo(mo, True) + ucs.login_handle.commit() + changed = True + + except Exception as e: + err = True + ucs.result['msg'] = "setup error: %s " % str(e) + + ucs.result['changed'] = changed + if err: + module.fail_json(**ucs.result) + module.exit_json(**ucs.result) + + +if __name__ == '__main__': + main() diff --git a/test/integration/targets/ucs_vhba_template/aliases b/test/integration/targets/ucs_vhba_template/aliases new file mode 100644 index 0000000000..30cc9de4a2 --- /dev/null +++ b/test/integration/targets/ucs_vhba_template/aliases @@ -0,0 +1,6 @@ +# Not enabled, but can be used with the UCS Platform Emulator or UCS hardware. +# Example integration_config.yml: +# --- +# ucs_hostname: 172.16.143.136 +# ucs_username: admin +# ucs_password: password diff --git a/test/integration/targets/ucs_vhba_template/tasks/main.yml b/test/integration/targets/ucs_vhba_template/tasks/main.yml new file mode 100644 index 0000000000..abf46ba68f --- /dev/null +++ b/test/integration/targets/ucs_vhba_template/tasks/main.yml @@ -0,0 +1,128 @@ +# Test code for the UCS modules +# Copyright 2017, David Soper (@dsoper2) + +- name: Test that we have a UCS host, UCS username, and UCS password + fail: + msg: 'Please define the following variables: ucs_hostname, ucs_username and ucs_password.' + when: ucs_hostname is not defined or ucs_username is not defined or ucs_password is not defined + + +# Setup (clean environment) +- name: vHBA templates absent + ucs_vhba_template: &vhba_templates_absent + hostname: "{{ ucs_hostname }}" + username: "{{ ucs_username }}" + password: "{{ ucs_password }}" + name: vHBA-A + state: absent + + +# Test present (check_mode) +- name: vHBA templates present (check_mode) + ucs_vhba_template: &vhba_templates_present + hostname: "{{ ucs_hostname }}" + username: "{{ ucs_username }}" + password: "{{ ucs_password }}" + name: vHBA-A + fabric: A + vsan: VSAN-A + wwpn_pool: WWPN-Pool-A + check_mode: yes + register: cm_vhba_templates_present + + +# Present (normal mode) +- name: vHBA templates present (normal mode) + ucs_vhba_template: *vhba_templates_present + register: nm_vhba_templates_present + + +# Test present again (idempotent) +- name: vHBA templates present again (check_mode) + ucs_vhba_template: *vhba_templates_present + check_mode: yes + register: cm_vhba_templates_present_again + + +# Present again (normal mode) +- name: vHBA templates present again (normal mode) + ucs_vhba_template: *vhba_templates_present + register: nm_vhba_templates_present_again + + +# Verfiy present +- name: Verify vHBA templates present results + assert: + that: + - cm_vhba_templates_present.changed == nm_vhba_templates_present.changed == true + - cm_vhba_templates_present_again.changed == nm_vhba_templates_present_again.changed == false + + +# Test change (check_mode) +- name: vHBA templates description change (check_mode) + ucs_vhba_template: &vhba_templates_change + <<: *vhba_templates_present + descr: Testing Ansible + check_mode: yes + register: cm_vhba_templates_descr_change + + +# Change (normal mode) +- name: vHBA templates description change (normal mode) + ucs_vhba_template: *vhba_templates_change + register: nm_vhba_templates_descr_change + + +# Test change again (idempotent) +- name: vHBA templates description again (check_mode) + ucs_vhba_template: *vhba_templates_change + check_mode: yes + register: cm_vhba_templates_descr_change_again + + +# Change again (normal mode) +- name: vHBA templates description change again (normal mode) + ucs_vhba_template: *vhba_templates_change + register: nm_vhba_templates_descr_change_again + + +# Verfiy change +- name: Verify vHBA templates change results + assert: + that: + - cm_vhba_templates_descr_change.changed == nm_vhba_templates_descr_change.changed == true + - cm_vhba_templates_descr_change_again.changed == nm_vhba_templates_descr_change_again.changed == false + + +# Teardown (clean environment) +- name: vHBA templates absent (check_mode) + ucs_vhba_template: *vhba_templates_absent + check_mode: yes + register: cm_vhba_templates_absent + + +# Absent (normal mode) +- name: vHBA templates absent (normal mode) + ucs_vhba_template: *vhba_templates_absent + register: nm_vhba_templates_absent + + +# Test absent again (idempotent) +- name: vHBA templates absent again (check_mode) + ucs_vhba_template: *vhba_templates_absent + check_mode: yes + register: cm_vhba_templates_absent_again + + +# Absent again (normal mode) +- name: vHBA templates absent again (normal mode) + ucs_vhba_template: *vhba_templates_absent + register: nm_vhba_templates_absent_again + + +# Verfiy absent +- name: Verify vHBA templates absent results + assert: + that: + - cm_vhba_templates_absent.changed == nm_vhba_templates_absent.changed == true + - cm_vhba_templates_absent_again.changed == nm_vhba_templates_absent_again.changed == false