Skip to content

Instantly share code, notes, and snippets.

@weswhet
Last active July 31, 2018 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weswhet/46b65629471fd7348a4175a0fd70fc85 to your computer and use it in GitHub Desktop.
Save weswhet/46b65629471fd7348a4175a0fd70fc85 to your computer and use it in GitHub Desktop.
Custom salt state to make sure the computer name is set correctly.
# -*- coding: utf-8 -*-
'''
State for setting computer names on macOS.
.. code-block:: yaml
set_computer_name_to_wesbook:
system.set_computer_name:
- name: wesbook
'''
import salt.utils.platform
import logging
log = logging.getLogger(__name__)
__virtualname__ = 'system'
def __virtual__():
if salt.utils.platform.is_darwin():
return __virtualname__
return (False, 'states.mac_system is only available on macOS.')
def set_computer_name(name):
'''
Make sure a system computer name is set.
name
The name of the macOS computer name you wish to make sure is set.
'''
ret = {'name': name,
'result': True,
'changes': {},
'comment': ''}
# get our current catalog value.
old_name = __salt__['system.get_computer_name']()
# check if we are set correctly
if old_name == name:
ret['comment'] = 'system computer_name is already set correctly.'
return ret
# we are not set correctly so we need set it.
set_computer_name = __salt__['system.set_computer_name'](name)
if not set_computer_name:
ret['result'] = False
ret['comment'] = 'Failed to set system'\
'computer name "{0}"'.format(name)
else:
ret['comment'] = 'Successfully set system '\
'computer name to "{0}"'.format(name)
ret['changes'].update({'ComputerName': {'old': old_name,
'new': name}})
return ret
set_computer_name_to_wesbook:
system.set_computer_name:
- name: wesbook
set_computer_name_to_serialnumber-wesbook:
system.set_computer_name:
- name: {{ grains['system_serialnumber'] }}-wesbook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment