forked from ucs-compute-solutions/FlashStack_IMM_Ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_all_inventory.yml
59 lines (59 loc) · 2.59 KB
/
update_all_inventory.yml
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
---
#
# Summary: Auto generate (or update) the Ansible inventory file with all servers (Name and Moid or each discovered server)
#
# The hosts group used is provided by the group variable or defaulted to 'Intersight'.
# 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
#
# This playbook only runs once (and not for each server in the inventory), but the hosts group is used to get API key info
#
- hosts: "{{ group | default('Intersight') }}"
connection: local
gather_facts: false
# vars_files:
# - Input_Required/api_info.yaml
vars:
# Create an anchor for api_info that can be used throughout the file
api_info: &api_info
# if api_key vars are omitted, INTERSIGHT_API_KEY_ID, INTERSIGHT_API_PRIVATE_KEY,
# and INTERSIGHT_API_URI environment variables used for API key data
api_private_key: "{{ api_private_key | default(omit) }}"
api_key_id: "{{ api_key_id | default(omit) }}"
api_uri: "{{ api_uri | default(omit) }}"
validate_certs: "{{ validate_certs | default(omit) }}"
state: "{{ state | default(omit) }}"
# Change filepath if you want to update a different inventory file
filepath: "{{ inventory_file }}"
# Change host_group if you want to use another group name for your servers in the created inventory
host_group: Intersight_Servers
tasks:
# Enclose tasks in a block that is only run once
- block:
# Find all servers
- cisco.intersight.intersight_info:
<<: *api_info
server_names:
register: all_results
# Place the servers in a group in the file
- debug:
msg: Inventory filepath "{{ filepath }}"
- lineinfile:
path: "{{ filepath }}"
line: "[{{ host_group }}]"
create: true
# Update servers in the file
- lineinfile:
path: "{{ filepath }}"
insertafter: "^\\[{{ host_group }}\\]"
regexp: "^{{ item.Name }} serial={{ item.Serial }} "
# Each line of the inventory has the following:
line: "{{ item.Name }} serial={{ item.Serial }} server_moid={{ item.Moid }} model={{ item.Model }} mode={{ item.ManagementMode }} object_type={{ item.SourceObjectType | regex_replace('compute.')}}"
create: true
loop: "{{ all_results.intersight_servers }}"
loop_control:
label: "{{ item.Name }}"
when: all_results.intersight_servers is defined
delegate_to: localhost
run_once: true