summaryrefslogtreecommitdiffstats
path: root/ansible_collections/cisco/intersight/playbooks/only_new_server_profiles.yml
blob: ce07f93f0813b5fdbf5c7276ef277a53d189fe9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
#
# Configure Server Profiles
#
# The hosts group used is provided by the group variable or defaulted to 'Intersight_Servers'.
# You can specify a specific host (or host group) on the command line:
#   ansible-playbook ... -e group=<your host group>
#   e.g., ansible-playbook server_profiles.yml -e group=TME_Demo
#
- name: Configure server profiles without assignment
  hosts: "{{ group | default('Intersight_Servers') }}"
  connection: local
  gather_facts: false
  vars:
    # Create an anchor for api_info that can be used throughout the file
    api_info: &api_info
      # Key can be directly specified, and vault should be used to encrypt:
      # Ex. ansible-vault encrypt_string --vault-id tme@/Users/dsoper/Documents/vault_password_file '-----BEGIN EC PRIVATE KEY-----
      #     <your private key data>
      #     -----END EC PRIVATE KEY-----'
      # To use with vault:
      #     ansible-playbook -i inventory --vault-id tme@vault_password_file intersight_server_profile.yml
      api_private_key: "{{ api_private_key }}"
      api_key_id: "{{ api_key_id }}"
      api_uri: "{{ api_uri | default(omit) }}"
      validate_certs: "{{ validate_certs | default(omit) }}"
      state: "{{ state | default(omit) }}"
    # Server Profile name default
    profile_name: "{{ inventory_hostname | regex_replace('-r$', '') }}"
  tasks:
    #
    # Configure profiles specific to server (run for each server in the inventory)
    #
    - name: Set Standalone Management Mode
      ansible.builtin.set_fact:
        mode: Standalone
      when: mode is not defined or mode == 'IntersightStandalone'
    - name: Set FIAttached Management Mode
      ansible.builtin.set_fact:
        mode: FIAttached
      when: mode == 'Intersight'
    # Get server moid when not defined in inventory
    - name: Get server moid
      when: server_moid is not defined
      delegate_to: localhost
      block:
        - name: "Get Server Moid"
          cisco.intersight.intersight_info:
            <<: *api_info
            server_names: "{{ inventory_hostname }}"
          register: server
        - name: Set server moid
          ansible.builtin.set_fact:
            server_moid: "{{ server.intersight_servers[0].Moid }}"
    - name: "Get current profile assignment"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /server/Profiles
        query_params:
          $filter: "AssignedServer.Moid eq '{{ server_moid }}' or AssociatedServer.Moid eq '{{ server_moid }}'"
      when: server_moid is defined
      register: profile
      delegate_to: localhost
    - name: "Configure Server Profile"
      cisco.intersight.intersight_server_profile:
        <<: *api_info
        organization: "{{ organization | default(omit) }}"
        name: "{{ profile_name }}"
        target_platform: "{{ mode | default(omit) }}"
        description: "Updated Profile for server name {{ inventory_hostname }}"
        assigned_server: "{{ server_moid }}"
      when:
        - server_moid is defined
        - profile is not defined or profile.api_response.Moid is not defined
      delegate_to: localhost