Last active
July 31, 2018 00:08
-
-
Save weswhet/46b65629471fd7348a4175a0fd70fc85 to your computer and use it in GitHub Desktop.
Custom salt state to make sure the computer name is set correctly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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