Added initial stub for where playbooks will go, moved to common

constants file so as to not repeat constants between CLI and lib.
This commit is contained in:
Michael DeHaan
2012-02-23 23:26:16 -05:00
parent be9fdc8ef1
commit 6eda2cf383
5 changed files with 136 additions and 44 deletions

View File

@@ -28,15 +28,7 @@ import traceback
# non-core
import paramiko
DEFAULT_HOST_LIST = '/etc/ansible/hosts'
DEFAULT_MODULE_PATH = '/usr/share/ansible'
DEFAULT_MODULE_NAME = 'ping'
DEFAULT_PATTERN = '*'
DEFAULT_FORKS = 3
DEFAULT_MODULE_ARGS = ''
DEFAULT_TIMEOUT = 60
DEFAULT_REMOTE_USER = 'root'
DEFAULT_REMOTE_PASS = None
import constants as C
def _executor_hook(x):
''' callback used by multiprocessing pool '''
@@ -46,15 +38,15 @@ def _executor_hook(x):
class Runner(object):
def __init__(self,
host_list=DEFAULT_HOST_LIST,
module_path=DEFAULT_MODULE_PATH,
module_name=DEFAULT_MODULE_NAME,
module_args=DEFAULT_MODULE_ARGS,
forks=DEFAULT_FORKS,
timeout=DEFAULT_TIMEOUT,
pattern=DEFAULT_PATTERN,
remote_user=DEFAULT_REMOTE_USER,
remote_pass=DEFAULT_REMOTE_PASS,
host_list=C.DEFAULT_HOST_LIST,
module_path=C.DEFAULT_MODULE_PATH,
module_name=C.DEFAULT_MODULE_NAME,
module_args=C.DEFAULT_MODULE_ARGS,
forks=C.DEFAULT_FORKS,
timeout=C.DEFAULT_TIMEOUT,
pattern=C.DEFAULT_PATTERN,
remote_user=C.DEFAULT_REMOTE_USER,
remote_pass=C.DEFAULT_REMOTE_PASS,
verbose=False):

10
lib/ansible/constants.py Normal file
View File

@@ -0,0 +1,10 @@
DEFAULT_HOST_LIST = '/etc/ansible/hosts'
DEFAULT_MODULE_PATH = '/usr/share/ansible'
DEFAULT_MODULE_NAME = 'ping'
DEFAULT_PATTERN = '*'
DEFAULT_FORKS = 3
DEFAULT_MODULE_ARGS = ''
DEFAULT_TIMEOUT = 60
DEFAULT_REMOTE_USER = 'root'
DEFAULT_REMOTE_PASS = None

74
lib/ansible/playbook.py Executable file
View File

@@ -0,0 +1,74 @@
# Copyright (c) 2012 Michael DeHaan <michael.dehaan@gmail.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import ansible
import ansible.constants as C
import json
import yaml
# TODO: make a constants file rather than
# duplicating these
class PlayBook(object):
'''
runs an ansible playbook, given as a datastructure
or YAML filename
'''
def __init__(self,
playbook =None,
host_list =C.DEFAULT_HOST_LIST,
module_path =C.DEFAULT_MODULE_PATH,
forks =C.DEFAULT_FORKS,
timeout =C.DEFAULT_TIMEOUT,
remote_user =C.DEFAULT_REMOTE_USER,
remote_pass =C.DEFAULT_REMOTE_PASS,
verbose=False):
# runner is reused between calls
self.runner = ansible.Runner(
host_list=host_list,
module_path=module_path,
forks=forks,
timeout=timeout,
remote_user=remote_user,
remote_pass=remote_pass,
verbose=verbose
)
if type(playbook) == str:
playbook = yaml.load(file(playbook).read())
def run(self):
pass
# r = Runner(
# host_list = DEFAULT_HOST_LIST,
# module_name='ping',
# module_args='',
# pattern='*',
# forks=3
# )
# print r.run()