intersight-ansible icon indicating copy to clipboard operation
intersight-ansible copied to clipboard

Request: intersight_server_profile param to deploy profile

Open jeisenbath opened this issue 1 year ago • 0 comments

I would like either a new option to the "state" param to be "deployed", or a new boolean param "deploy" to allow profile deploy when creating a new profile.

Currently intersight_server_profile module only creates and sets the ConfigState of a profile to "Assigned" to a server. In order to then deploy a profile, several other tasks with some complex conditionals and until statements are required to wait until the profile is finished validating, and to put those tasks in an idempotent state (checking that ConfigContext.ControlAction is in "No-op"). Then further tasks required to check if the profile deploy fails, and what the fail message is.

Example below is our code we use to perform all of these tasks, which I hope this request would greatly simplify.

---
- name: Get server info
  cisco.intersight.intersight_info:
    api_private_key: "{{ intersight_api_private_key }}"
    api_key_id: "{{ intersight_api_key_id }}"
    api_uri: "{{ intersight_api_uri | default(omit) }}"
    validate_certs: "{{ intersight_validate_certs | default(false) }}"
    server_names: "{{ intersight_server_name }}"
  register: server_info
  delegate_to: localhost
  retries: 20
  delay: 60
  until: server_info.intersight_servers[0].Moid is defined

- name: Set server Moid
  ansible.builtin.set_fact:
    server_moid: "{{ server_info.intersight_servers[0].Moid }}"

- name: Configure Server Profile
  cisco.intersight.intersight_server_profile:
    api_private_key: "{{ intersight_api_private_key }}"
    api_key_id: "{{ intersight_api_key_id }}"
    api_uri: "{{ intersight_api_uri | default(omit) }}"
    validate_certs: "{{ intersight_validate_certs | default(false) }}"
    organization: "{{ intersight_organization }}"
    name: "{{ intersight_profile.name }}"
    target_platform: "{{ intersight_target_platform }}"
    tags: "{{ intersight_profile.tags }}"
    description: "{{ intersight_profile.description }}"
    assigned_server: "{{ server_moid }}"
    boot_order_policy: "{{ intersight_profile.boot_order_policy | default(omit) }}"
    local_user_policy: "{{ intersight_profile.local_user_policy | default(omit) }}"
    ntp_policy: "{{ intersight_profile.ntp_policy | default(omit) }}"
    virtual_media_policy: "{{ intersight_profile.virtual_media_policy | default(omit) }}"
    storage_policy: "{{ intersight_profile.storage_policy | default(omit) }}"
  when: server_moid is defined
  delegate_to: localhost

- name: Get Profile
  cisco.intersight.intersight_rest_api:
    api_private_key: "{{ intersight_api_private_key }}"
    api_key_id: "{{ intersight_api_key_id }}"
    api_uri: "{{ intersight_api_uri | default(omit) }}"
    validate_certs: "{{ intersight_validate_certs | default(false) }}"
    resource_path: /server/Profiles
    query_params:
      $filter: "Name eq '{{ intersight_profile.name }}'"
  register: profile_results
  delegate_to: localhost
  until: (profile_results.api_response.ConfigContext.ControlAction == "No-op")
  delay: 60
  retries: 20

- name: Deploy profile to assigned server
  cisco.intersight.intersight_rest_api:
    api_private_key: "{{ intersight_api_private_key }}"
    api_key_id: "{{ intersight_api_key_id }}"
    api_uri: "{{ intersight_api_uri | default(omit) }}"
    validate_certs: "{{ intersight_validate_certs | default(false) }}"
    resource_path: /server/Profiles
    query_params:
      $filter: "Name eq '{{ intersight_profile.name }}'"
    api_body: {
      "Action": "{{ intersight_profile_action | default('Deploy') }}"
    }
  delegate_to: localhost
  register: profile_deploy_action
  when: (profile_results.api_response.ConfigContext.ControlAction == "No-op") and
        (profile_results.api_response.ConfigContext.ConfigState != "Associated")

- name: Wait for profile deploy to complete
  cisco.intersight.intersight_rest_api:
    api_private_key: "{{ intersight_api_private_key }}"
    api_key_id: "{{ intersight_api_key_id }}"
    api_uri: "{{ intersight_api_uri | default(omit) }}"
    validate_certs: "{{ intersight_validate_certs | default(false) }}"
    resource_path: /server/Profiles
    query_params:
      $filter: "Name eq '{{ intersight_profile.name }}'"
  register: profile_deploy_results
  delegate_to: localhost
  until: ('api_response' in profile_deploy_results) and ((profile_deploy_results.api_response.ConfigContext.OperState == 'Ok') or
     (profile_deploy_results.api_response.ConfigContext.OperState == 'Failed'))
  delay: 60
  retries: 20

- name: Report error on deploy fail
  when: profile_deploy_results.api_response.ConfigContext.OperState == 'Failed'
  block:
  - name: Get failure reason from intersight
    cisco.intersight.intersight_rest_api:
      api_private_key: "{{ intersight_api_private_key }}"
      api_key_id: "{{ intersight_api_key_id }}"
      api_uri: "{{ intersight_api_uri | default(omit) }}"
      validate_certs: "{{ intersight_validate_certs | default(false) }}"
      resource_path: /workflow/WorkflowInfos
      query_params:
        $orderby: CreateTime desc
        $filter: (Status in ('FAILED')) and (Input.workflowContext.TargetCtxList.TargetName eq '{{ intersight_profile.name }}')
        $expand: TaskInfos($select=Status,FailureReason)
        $top: 1
    register: deployfail_results
    delegate_to: localhost

  - ansible.builtin.fail:
      msg: "{% for message in deployfail_results.api_response.TaskInfos if message.Status == 'FAILED' %}{{ message.FailureReason }}{% endfor %}"

jeisenbath avatar May 19 '23 18:05 jeisenbath