ansible-config_template/library/name2int
Kevin Carter 2c9c5f005b
first commit
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
2016-01-25 18:07:53 -06:00

80 lines
2.0 KiB
Python

#!/usr/bin/python
# (c) 2014, Kevin Carter <kevin.carter@rackspace.com>
#
# Copyright 2014, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hashlib
import platform
# import module snippets
from ansible.module_utils.basic import *
DOCUMENTATION = """
---
module: name2int
version_added: "1.6.6"
short_description:
- hash a host name and return an integer
description:
- hash a host name and return an integer
options:
name:
description:
- login username
required: true
author: Kevin Carter
"""
EXAMPLES = """
# Create a new container
- name2int:
name: "Some-hostname.com"
"""
class HashHostname(object):
def __init__(self, module):
"""Generate an integer from a name."""
self.module = module
def return_hashed_host(self, name):
hashed_name = hashlib.md5(name).hexdigest()
hash_int = int(hashed_name, 32)
real_int = int(hash_int % 300)
return real_int
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(
required=True
)
),
supports_check_mode=False
)
try:
sm = HashHostname(module=module)
int_value = sm.return_hashed_host(platform.node())
resp = {'int_value': int_value}
module.exit_json(changed=True, **resp)
except Exception as exp:
resp = {'stderr': exp}
module.fail_json(msg='Failed Process', **resp)
if __name__ == '__main__':
main()