02-17-2018 12:35 PM - edited 04-13-2022 09:27 AM
Cisco Intersight Automated Target Claim and API Usage
This document provides an overview of automated UCS and HyperFlex target (device) claim using the Cisco Intersight API and Python SDK. See the Intersight Python Package Overview for more information on the Intersight Python SDK and how to use API keys for automation with Python. For general information on Intersight, visit Cisco Intersight - Cisco
Intersight Python SDK
Requirements
Python 3.7+
OpenSSL >= 1.0.1. You can check ssl version used by Python with python -c 'import ssl; print(ssl.OPENSSL_VERSION)'
Installation using pip
pip install intersight
Python Target Claim and other Examples
git clone https://github.com/CiscoDevNet/intersight-python-utils.git
cd intersight-python-utils/config_targets
export INTERSIGHT_API_PRIVATE_KEY=/Users/guest/Downloads/v3_SecretKey.txt
export INTERSIGHT_API_KEY_ID=596cc...
[
{
"device_type": "imc",
"hostname": "172.28.225.122",
"password": "…",
"proxy_host": "proxy-wsa.esl.cisco.com",
"proxy_port": 80,
"username": "admin",
"read_only": false
}
]
(proxy_ fields are only needed if your devices are behind a proxy)
python claim_target.py -t sjc07_devices.json
Data Organization with Ansible
While not required for using the claim_target.py script, Ansible provides a simple way to use the claim_target.py script to make parallel calls to many devices and organize data for the devices being claimed. The example_inventory file shows how to organize device information into groups and share variables between groups:
[sjc07]
172.28.225.122 device_type=imc
172.28.225.20 device_type=ucs
[sjc07:vars]
password=...
[behind_proxy:children]
sjc07
[behind_proxy:vars]
proxy_host=proxy-wsa.esl.cisco.com
proxy_port=80
[all:children]
sjc07
[all:vars]
username=admin
The example_claim.yml playbook shows how to use variable passed in from the inventory and call the claim_target.py script using Ansible's script module:
---
- hosts: all
connection: local
gather_facts: false
vars:
# override on command line if needed
target_hostname: "{{ inventory_hostname }}"
target_username: "{{ username | default('admin') }}"
target_password: "{{ password }}"
platform_type: "{{ device_type | default('ucs') }}"
target_proxy_host: "{{ proxy_host | default('') }}"
target_proxy_port: "{{ proxy_port | default(0) }}"
target_read_only: "{{ read_only | default(false) }}"
environment:
PYTHONPATH: "{{ playbook_dir }}"
tasks:
- name: Define JSON string passed to claim script
set_fact:
# JSON for each host being claimed.
# Quotes and whitespace are importantant for the JSON string passed to claim script.
# ----------
stash_json: ' { "hostname": "{{ target_hostname }}",
"username": "{{ target_username }}",
"password": "{{ target_password }}",
"device_type": "{{ platform_type }}",
"proxy_host": "{{ target_proxy_host }}",
"proxy_port": "{{ target_proxy_port }}",
"read_only": "{{ target_read_only }}"
}'
- name: Claim targets
script: /usr/bin/env python claim_target.py -t ' [{{ stash_json }}]'
register: result
changed_when: '"true" in result.stdout'
Below is an example using Ansible to allow for target claim run in parallel.
$ pwd /Users/dsoper/Documents/intersight-python-utils/config_targets $ ansible-playbook -i tme_inventory example_claim.yml PLAY [all] ****************************************************************************************************************************************************************************** TASK [Define JSON string passed to claim script] **************************************************************************************************************************************** ok: [172.28.225.122] ok: [172.28.225.20] TASK [Claim targets] ******************************************************************************************************************************************************************** ok: [172.28.225.20] ...
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: