-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathupdate_all_inventory.yml
57 lines (57 loc) · 2.42 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
---
#
# 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:
# Create an anchor for api_info that can be used throughout the file
api_info: &api_info
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) }}"
# 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
- intersight_facts:
<<: *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:
# Name server_moid=<Moid value> model=<Model value> boot_policy=<policy from tag> | 'na'
line: "{{ item.Name }} serial={{ item.Serial }} server_moid={{ item.Moid }} model={{ item.Model }}"
create: true
# Ansible and jmespath contains have type differences, so to/from_json used
loop: "{{ all_results.intersight_servers }}"
loop_control:
label: "{{ item.Name }}"
when: all_results.intersight_servers is defined
delegate_to: localhost
run_once: true