Skip to content

Instantly share code, notes, and snippets.

@haad
Last active January 16, 2017 10:42
Show Gist options
  • Save haad/a798a0335de2f2ddc3846957565c010a to your computer and use it in GitHub Desktop.
Save haad/a798a0335de2f2ddc3846957565c010a to your computer and use it in GitHub Desktop.
diff --git i/library/hpilo_boot w/library/hpilo_boot
index a6eba2d..d019c3d 100755
--- i/library/hpilo_boot
+++ w/library/hpilo_boot
@@ -22,36 +22,44 @@ DOCUMENTATION = '''
---
author: Dag Wieers
module: hpilo_boot
-short_description: Boot system using specific media through HP iLO interface
+requirements: [ python hpilo ]
+short_description: Boot system using specific media through HP iLO interface.
description:
- "This module boots a system through its HP iLO interface. The boot media
can be one of: cdrom, floppy, hdd, network or usb."
- This module requires the hpilo python module.
-version_added: "0.8"
+version_added: 2.3
options:
host:
description:
- The HP iLO hostname/address that is linked to the physical system.
+ default: null
required: true
+ aliases: ['ilo_host']
login:
description:
- The login name to authenticate to the HP iLO interface.
+ required: true
default: Administrator
+ aliases: ['user']
password:
description:
- The password to authenticate to the HP iLO interface.
+ required: true
default: admin
+ aliases: ['pass']
media:
description:
- - The boot media to boot the system from
+ - The boot media to boot the system from.
+ required: true
default: network
choices: [ "cdrom", "floppy", "hdd", "network", "normal", "usb" ]
image:
description:
- - "The URL of a cdrom, floppy or usb boot media image.
- 'protocol://username:password@hostname:port/filename'"
+ - The URL of a cdrom, floppy or usb boot media image.
+ 'protocol://<username>:<password>@<hostname>:<port>/filename'
- protocol is either 'http' or 'https'
- - "username:password is optional"
+ - username:password is optional
- port is optional
state:
description:
@@ -62,33 +70,21 @@ options:
- "connect: Connect the virtual media device and set to boot_always"
- "disconnect: Disconnects the virtual media device and set to no_boot"
- "poweroff: Power off the server"
+ required: true
default: boot_once
choices: [ "boot_always", "boot_once", "connect", "disconnect", "no_boot", "poweroff" ]
force:
description:
- Whether to force a reboot (even when the system is already booted).
- - As a safeguard, without force, hpilo_boot will refuse to reboot a server that is already running.
+ - As a safeguard, without force, hpilo_boot will refuse to reboot a
+ server that is already running.
default: no
choices: [ "yes", "no" ]
-examples:
- - description: Task to boot a system using an ISO from an HP iLO interface only if the system is an HP server
- code: |
- - hpilo_boot:
- host: YOUR_ILO_ADDRESS
- login: YOUR_ILO_LOGIN
- password: YOUR_ILO_PASSWORD
- media: cdrom
- image: http://some-web-server/iso/boot.iso
- when: cmdb_hwmodel.startswith('HP ')
- delegate_to: localhost
- - description: Power off a server
- code:
- hpilo_boot:
- host: YOUR_ILO_HOST
- login: YOUR_ILO_LOGIN
- password: YOUR_ILO_PASSWORD
- state: poweroff
- delegate_to: localhost
+ timeout:
+ description:
+ - Set HPilo connection timeout.
+ required: False
+ default: 60
notes:
- To use a USB key image you need to specify floppy as boot media.
- This module ought to be run from a system that can access the HP iLO
@@ -96,14 +92,36 @@ notes:
using delegate_to.
'''
-import sys
-import time
+EXAMPLES = '''
+# Task to boot a system using an ISO from an HP iLO interface only if the system is an HP server
+- hpilo_boot:
+ host: YOUR_ILO_ADDRESS
+ login: YOUR_ILO_LOGIN
+ password: YOUR_ILO_PASSWORD
+ media: cdrom
+ image: http://some-web-server/iso/boot.iso
+ when: cmdb_hwmodel.startswith('HP ')
+ delegate_to: localhost
+
+# Power off a server
+- hpilo_boot:
+ host: YOUR_ILO_HOST
+ login: YOUR_ILO_LOGIN
+ password: YOUR_ILO_PASSWORD
+ state: poweroff
+ delegate_to: localhost
+'''
+
+RETURN = '''
+#
+'''
+
import warnings
try:
import hpilo
+ HAS_HPILO = True
except ImportError:
- print "failed=True msg='hpilo python module unavailable'"
- sys.exit(1)
+ HAS_HPILO = False
# Surpress warnings from hpilo
warnings.simplefilter('ignore')
@@ -111,14 +129,15 @@ warnings.simplefilter('ignore')
def main():
module = AnsibleModule(
- argument_spec = dict(
- host = dict(required=True),
- login = dict(default='Administrator'),
- password = dict(default='admin'),
- media = dict(default=None, choices=['cdrom', 'floppy', 'rbsu', 'hdd', 'network', 'normal', 'usb']),
- image = dict(default=None),
- state = dict(default='boot_once', choices=['boot_always', 'boot_once', 'connect', 'disconnect', 'no_boot', 'poweroff']),
- force = dict(default='no', choices=BOOLEANS),
+ argument_spec=dict(
+ host=dict(required=True, type='str', aliases=['ilo_host']),
+ login=dict(default='Administrator', type='str', aliases=['user']),
+ password=dict(default='admin', type='str', aliases=['pass']),
+ media=dict(default='network', choices=['cdrom', 'floppy', 'rbsu', 'hdd', 'network', 'normal', 'usb']),
+ image=dict(default=None, type='str'),
+ state=dict(default='boot_once', choices=['boot_always', 'boot_once', 'connect', 'disconnect', 'no_boot', 'poweroff']),
+ force=dict(default='no', type='bool'),
+ timeout=dict(default=60, type='int')
)
)
@@ -128,9 +147,13 @@ def main():
media = module.params.get('media')
image = module.params.get('image')
state = module.params.get('state')
- force = module.boolean(module.params.get('force'))
+ force = module.params.get('force')
+ timeout = module.params.get('timeout')
- ilo = hpilo.Ilo(host, login=login, password=password)
+ if not HAS_HPILO:
+ module.fail_json(msg="hpilo python module is required.")
+
+ ilo = hpilo.Ilo(host, login=login, password=password, timeout=timeout)
changed = False
status = {}
power_status = 'UNKNOWN'
@@ -141,7 +164,7 @@ def main():
try:
ilo.set_one_time_boot(media)
except hpilo.IloError:
- time.sleep(60)
+ #time.sleep(60)
ilo.set_one_time_boot(media)
# TODO: Verify if image URL exists/works
@@ -155,7 +178,7 @@ def main():
changed = True
elif media in ('floppy', 'usb'):
ilo.set_vf_status(state, True)
- status = ilo.get_vf_status()
+ status = ilo.get_vm_status(device='floppy')
changed = True
# Only perform a boot when state is boot_once or boot_always, or in case we want to force a reboot
@@ -176,8 +199,7 @@ def main():
# ilo.set_host_power(host_power=True)
changed = True
- elif state in ('poweroff'):
-
+ elif state in 'poweroff':
power_status = ilo.get_host_power_status()
if not power_status == 'OFF':
@@ -187,6 +209,9 @@ def main():
module.exit_json(changed=changed, power=power_status, **status)
-# this is magic, see lib/ansible/module_common.py
-#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
-main()
+# import module snippets
+from ansible.module_utils.basic import *
+from ansible.module_utils.urls import *
+
+if __name__ == "__main__":
+ main()
diff --git i/library/hpilo_facts w/library/hpilo_facts
index 55fb44d..291654b 100755
--- i/library/hpilo_facts
+++ w/library/hpilo_facts
@@ -22,116 +22,125 @@ DOCUMENTATION = '''
---
author: Dag Wieers
module: hpilo_facts
+requirements: [ python hpilo ]
short_description: Gather facts through an HP iLO interface
description:
- - This module gathers facts for a specific system using its HP iLO interface.
- These facts include hardware and network related information useful
- for provisioning (e.g. macaddress, uuid).
- - This module requires the hpilo python module.
-version_added: "0.8"
+ - This module gathers facts for a specific system using its HP iLO interface.
+ These facts include hardware and network related information useful
+ for provisioning (e.g. macaddress, uuid).
+ - This module requires the hpilo python module.
+version_added: 2.3
options:
- host:
- description:
- - The HP iLO hostname/address that is linked to the physical system.
- required: true
- login:
- description:
- - The login name to authenticate to the HP iLO interface.
- default: Administrator
- password:
- description:
- - The password to authenticate to the HP iLO interface.
- default: admin
-examples:
- - description: Task to gather facts from a HP iLO interface only if the system is an HP server
- code: |
- - hpilo_facts:
- host: YOUR_ILO_ADDRESS
- login: YOUR_ILO_LOGIN
- password: YOUR_ILO_PASSWORD
- when: cmdb_hwmodel.startswith('HP ')
- delegate_to: localhost
- - fail:
- msg: 'CMDB serial ({{ cmdb_serialno }}) does not match hardware serial ({{ hw_system_serial }}) !'
- when: cmdb_serialno != hw_system_serial
- - description: Typical output of HP iLO_facts for a physical system
- code: |
- - hw_bios_date: "05/05/2011"
- hw_bios_version: "P68"
- hw_eth0:
- - macaddress: "00:11:22:33:44:55"
- macaddress_dash: "00-11-22-33-44-55"
- hw_eth1:
- - macaddress: "00:11:22:33:44:57"
- macaddress_dash: "00-11-22-33-44-57"
- hw_eth2:
- - macaddress: "00:11:22:33:44:5A"
- macaddress_dash: "00-11-22-33-44-5A"
- hw_eth3:
- - macaddress: "00:11:22:33:44:5C"
- macaddress_dash: "00-11-22-33-44-5C"
- hw_eth_ilo:
- - macaddress: "00:11:22:33:44:BA"
- macaddress_dash: "00-11-22-33-44-BA"
- hw_product_name: "ProLiant DL360 G7"
- hw_product_uuid: "ef50bac8-2845-40ff-81d9-675315501dac"
- hw_system_serial: "ABC12345D6"
- hw_uuid: "123456ABC78901D2"
+ host:
+ description:
+ - The HP iLO hostname/address that is linked to the physical system.
+ required: true
+ login:
+ description:
+ - The login name to authenticate to the HP iLO interface.
+ default: Administrator
+ password:
+ description:
+ - The password to authenticate to the HP iLO interface.
+ default: admin
notes:
- - This module ought to be run from a system that can access the HP iLO
- interface directly, either by using local_action or
- using delegate_to.
+ - This module ought to be run from a system that can access the HP iLO
+ interface directly, either by using local_action or
+ using delegate_to.
+'''
+
+EXAMPLES = '''
+# Task to gather facts from a HP iLO interface only if the system is an HP server
+- hpilo_facts:
+ host: YOUR_ILO_ADDRESS
+ login: YOUR_ILO_LOGIN
+ password: YOUR_ILO_PASSWORD
+ when: cmdb_hwmodel.startswith('HP ')
+ delegate_to: localhost
+
+- fail:
+ msg: 'CMDB serial ({{ cmdb_serialno }}) does not match hardware serial ({{ hw_system_serial }}) !'
+ when: cmdb_serialno != hw_system_serial
+'''
+
+RETURN = '''
+
+- hw_bios_date: "05/05/2011"
+ hw_bios_version: "P68"
+ hw_eth0:
+- macaddress: "00:11:22:33:44:55"
+ macaddress_dash: "00-11-22-33-44-55"
+ hw_eth1:
+- macaddress: "00:11:22:33:44:57"
+ macaddress_dash: "00-11-22-33-44-57"
+ hw_eth2:
+- macaddress: "00:11:22:33:44:5A"
+ macaddress_dash: "00-11-22-33-44-5A"
+ hw_eth3:
+- macaddress: "00:11:22:33:44:5C"
+ macaddress_dash: "00-11-22-33-44-5C"
+ hw_eth_ilo:
+- macaddress: "00:11:22:33:44:BA"
+ macaddress_dash: "00-11-22-33-44-BA"
+ hw_product_name: "ProLiant DL360 G7"
+ hw_product_uuid: "ef50bac8-2845-40ff-81d9-675315501dac"
+ hw_system_serial: "ABC12345D6"
+ hw_uuid: "123456ABC78901D2"
'''
-import sys
import re
import warnings
try:
import hpilo
+ HAS_HPILO = True
except ImportError:
- print "failed=True msg='hpilo python module unavailable'"
- sys.exit(1)
+ HAS_HPILO = False
# Surpress warnings from hpilo
warnings.simplefilter('ignore')
-
def parse_flat_interface(entry, non_numeric='hw_eth_ilo'):
try:
factname = 'hw_eth' + str(int(entry['Port']) - 1)
except:
factname = non_numeric
- facts = {
- 'macaddress': entry['MAC'].replace('-', ':'),
- 'macaddress_dash': entry['MAC']
- }
- return (factname, facts)
-
+ facts = {
+ 'macaddress': entry['MAC'].replace('-', ':'),
+ 'macaddress_dash': entry['MAC']
+ }
+ return (factname, facts)
def main():
+ argument_spec = dict(
+ host = dict(default=None, required=True, type='str', aliases=['ilo_host']),
+ login = dict(default='Administrator', type='str', aliases=['user']),
+ password = dict(default='admin', type='str', aliases=['pass'])
+ )
module = AnsibleModule(
- argument_spec = dict(
- host = dict(required=True),
- login = dict(default='Administrator'),
- password = dict(default='admin'),
- )
+ argument_spec=argument_spec,
)
host = module.params.get('host')
login = module.params.get('login')
password = module.params.get('password')
+ if not HAS_HPILO:
+ module.fail_json(msg="hpilo python module is required.")
+
ilo = hpilo.Ilo(host, login=login, password=password)
# TODO: Count number of CPUs, DIMMs and total memory
data = ilo.get_host_data()
+
facts = {
'module_hw': True,
}
+
for entry in data:
- if not entry.has_key('type'): continue
+ if not entry.has_key('type'):
+ continue
if entry['type'] == 0: # BIOS Information
facts['hw_bios_version'] = entry['Family']
facts['hw_bios_date'] = entry['Date']
@@ -153,9 +162,9 @@ def main():
'macaddress': value.replace('-', ':'),
'macaddress_dash': value
}
- else:
- (factname, entry_facts) = parse_flat_interface(entry, 'hw_eth_ilo')
- facts[factname] = entry_facts
+ else:
+ (factname, entry_facts) = parse_flat_interface(entry, 'hw_eth_ilo')
+ facts[factname] = entry_facts
elif entry['type'] == 209: # HPQ NIC iSCSI MAC Info
for (name, value) in [(e['name'], e['value']) for e in entry['fields']]:
if name.startswith('Port'):
@@ -165,17 +174,18 @@ def main():
factname = 'hw_iscsi_ilo'
elif name.startswith('MAC'):
facts[factname] = {
- 'macaddress': value.replace('-', ':'),
- 'macaddress_dash': value
+ 'macaddress': value.replace('-', ':'),
+ 'macaddress_dash': value
}
- elif entry['type'] == 233: # Embedded NIC MAC Assignment (Alternate data format)
- (factname, entry_facts) = parse_flat_interface(entry, 'hw_eth_ilo')
- facts[factname] = entry_facts
+ elif entry['type'] == 233: # Embedded NIC MAC Assignment (Alternate data format)
+ (factname, entry_facts) = parse_flat_interface(entry, 'hw_eth_ilo')
+ facts[factname] = entry_facts
# collect health (RAM/CPU data)
health = ilo.get_embedded_health()
facts['hw_health'] = health
memory_details_summary = health.get('memory', {}).get('memory_details_summary')
+
# RAM as reported by iLO 2.10 on ProLiant BL460c Gen8
if memory_details_summary:
facts['hw_memory_details_summary'] = memory_details_summary
@@ -186,13 +196,14 @@ def main():
ram = re.search('(\d+)\s+(\w+)', cpu_total_memory_size)
if ram:
if ram.group(2) == 'GB':
- facts['hw_memory_total'] = facts['hw_memory_total'] + int(ram.group(1))
+ facts['hw_memory_total'] = int(facts['hw_memory_total']) + int(ram.group(1))
- # reformat into a text friendly format
- facts['hw_memory_total'] = "{0} GB".format(facts['hw_memory_total'])
+ facts['hw_memory_total'] = "{0} GB".format(facts['hw_memory_total'])
module.exit_json(ansible_facts=facts)
-# this is magic, see lib/ansible/module_common.py
-#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
-main()
+# import module snippets
+from ansible.module_utils.basic import *
+
+if __name__ == "__main__":
+ main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment