Esempio n. 1
0
    def load_values(self):
        self.is_git_repo = self.__git_data('rev-parse --git-dir') == '.git'

        self.playbook = self.play.playbook
        self.inventory = self.playbook.inventory
        self.hosts = self.inventory.get_hosts()

        self.vars = self.playbook.extra_vars
        self.vars.update(self.inventory.get_variables(self.hosts[0].name))

        self.slack_endpoint = self.vars.get('slack_endpoint')
        self.disabled = self.vars.get('disable_slack')

        self.playbook_name = os.path.basename(self.playbook.filename)
        self.host_list = self.inventory.host_list
        self.subset = ', '.join(
            self.inventory._subset) if self.inventory._subset else "--"
        self.tags = ', '.join(
            self.playbook.only_tags) if self.playbook.only_tags else "--"
        self.skip_tags = ', '.join(
            self.playbook.skip_tags) if self.playbook.skip_tags else "--"
        self.user = self.playbook.remote_user
        self.is_test = self.playbook.check
        self.run_id = datetime.now().isoformat()
        self.run_host = self.__get_run_host()
        self.ansible_version = utils.version_info(True)['string']

        if self.is_git_repo:
            git_sha = self.__git_data('log -n1 --pretty=format:%H')
            git_decoration = self.__git_data('log -n1 --pretty=format:%d')
            git_dirty = self.__git_data('status --porcelain')

            self.git_sha = git_sha()[0]
            self.git_decoration = git_decoration()[0]
            self.git_dirty = git_dirty()
            self.git_repo_name = self.__git_repo_name()

        if self.disabled:
            utils.warning('Not posting to slack with disable_slack=yes')
Esempio n. 2
0
    def load_values(self):
        self.is_git_repo    = self.__git_data('rev-parse --git-dir') == '.git'

        self.playbook       = self.play.playbook
        self.inventory      = self.playbook.inventory
        self.hosts          = self.inventory.get_hosts()

        self.vars           = self.playbook.extra_vars
        self.vars.update(self.inventory.get_variables(self.hosts[0].name))

        self.slack_endpoint = self.vars.get('slack_endpoint')
        self.disabled       = self.vars.get('disable_slack')

        self.playbook_name  = os.path.basename(self.playbook.filename)
        self.host_list      = self.inventory.host_list
        self.subset         = ', '.join(self.inventory._subset) if self.inventory._subset else "--"
        self.tags           = ', '.join(self.playbook.only_tags) if self.playbook.only_tags else "--"
        self.skip_tags      = ', '.join(self.playbook.skip_tags) if self.playbook.skip_tags else "--"
        self.user           = self.playbook.remote_user
        self.is_test        = self.playbook.check
        self.run_id         = datetime.now().isoformat()
        self.run_host       = self.__get_run_host()
        self.ansible_version = utils.version_info(True)['string']

        if self.is_git_repo:
            git_sha = self.__git_data('log -n1 --pretty=format:%H')
            git_decoration = self.__git_data('log -n1 --pretty=format:%d')
            git_dirty = self.__git_data('status --porcelain')

            self.git_sha        = git_sha()[0]
            self.git_decoration = git_decoration()[0]
            self.git_dirty      = git_dirty()
            self.git_repo_name  = self.__git_repo_name()

        if self.disabled:
            utils.warning('Not posting to slack with disable_slack=yes')
Esempio n. 3
0
    def __init__(self,
        playbook         = None,
        host_list        = C.DEFAULT_HOST_LIST,
        module_path      = None,
        forks            = C.DEFAULT_FORKS,
        timeout          = C.DEFAULT_TIMEOUT,
        remote_user      = C.DEFAULT_REMOTE_USER,
        remote_pass      = C.DEFAULT_REMOTE_PASS,
        sudo_pass        = C.DEFAULT_SUDO_PASS,
        remote_port      = None,
        transport        = C.DEFAULT_TRANSPORT,
        private_key_file = C.DEFAULT_PRIVATE_KEY_FILE,
        callbacks        = None,
        runner_callbacks = None,
        stats            = None,
        sudo             = False,
        sudo_user        = C.DEFAULT_SUDO_USER,
        extra_vars       = None,
        only_tags        = None,
        skip_tags        = None,
        subset           = C.DEFAULT_SUBSET,
        inventory        = None,
        check            = False,
        diff             = False,
        any_errors_fatal = False,
        su               = False,
        su_user          = False,
        su_pass          = False,
        vault_password   = False,
        force_handlers   = False,
    ):

        """
        playbook:         path to a playbook file
        host_list:        path to a file like /etc/ansible/hosts
        module_path:      path to ansible modules, like /usr/share/ansible/
        forks:            desired level of parallelism
        timeout:          connection timeout
        remote_user:      run as this user if not specified in a particular play
        remote_pass:      use this remote password (for all plays) vs using SSH keys
        sudo_pass:        if sudo==True, and a password is required, this is the sudo password
        remote_port:      default remote port to use if not specified with the host or play
        transport:        how to connect to hosts that don't specify a transport (local, paramiko, etc)
        callbacks         output callbacks for the playbook
        runner_callbacks: more callbacks, this time for the runner API
        stats:            holds aggregrate data about events occurring to each host
        sudo:             if not specified per play, requests all plays use sudo mode
        inventory:        can be specified instead of host_list to use a pre-existing inventory object
        check:            don't change anything, just try to detect some potential changes
        any_errors_fatal: terminate the entire execution immediately when one of the hosts has failed
        force_handlers:   continue to notify and run handlers even if a task fails
        """

        self.SETUP_CACHE = SETUP_CACHE
        self.VARS_CACHE  = VARS_CACHE

        arguments = []
        if playbook is None:
            arguments.append('playbook')
        if callbacks is None:
            arguments.append('callbacks')
        if runner_callbacks is None:
            arguments.append('runner_callbacks')
        if stats is None:
            arguments.append('stats')
        if arguments:
            raise Exception('PlayBook missing required arguments: %s' % ', '.join(arguments))

        if extra_vars is None:
            extra_vars = {}
        if only_tags is None:
            only_tags = [ 'all' ]
        if skip_tags is None:
            skip_tags = []

        self.check            = check
        self.diff             = diff
        self.module_path      = module_path
        self.forks            = forks
        self.timeout          = timeout
        self.remote_user      = remote_user
        self.remote_pass      = remote_pass
        self.remote_port      = remote_port
        self.transport        = transport
        self.callbacks        = callbacks
        self.runner_callbacks = runner_callbacks
        self.stats            = stats
        self.sudo             = sudo
        self.sudo_pass        = sudo_pass
        self.sudo_user        = sudo_user
        self.extra_vars       = extra_vars
        self.global_vars      = {}
        self.private_key_file = private_key_file
        self.only_tags        = only_tags
        self.skip_tags        = skip_tags
        self.any_errors_fatal = any_errors_fatal
        self.su               = su
        self.su_user          = su_user
        self.su_pass          = su_pass
        self.vault_password   = vault_password
        self.force_handlers   = force_handlers

        self.callbacks.playbook = self
        self.runner_callbacks.playbook = self

        if inventory is None:
            self.inventory    = ansible.inventory.Inventory(host_list)
            self.inventory.subset(subset)
        else:
            self.inventory    = inventory

        if self.module_path is not None:
            utils.plugins.module_finder.add_directory(self.module_path)

        self.basedir     = os.path.dirname(playbook) or '.'
        utils.plugins.push_basedir(self.basedir)

        # let inventory know the playbook basedir so it can load more vars
        self.inventory.set_playbook_basedir(self.basedir)

        vars = extra_vars.copy()
        vars['playbook_dir'] = os.path.abspath(self.basedir)
        if self.inventory.basedir() is not None:
            vars['inventory_dir'] = self.inventory.basedir()

        if self.inventory.src() is not None:
            vars['inventory_file'] = self.inventory.src()

        self.filename = playbook
        (self.playbook, self.play_basedirs) = self._load_playbook_from_file(playbook, vars)
        ansible.callbacks.load_callback_plugins()
        ansible.callbacks.set_playbook(self.callbacks, self)

        self._ansible_version = utils.version_info(gitinfo=True)
Esempio n. 4
0
    def __init__(
        self,
        playbook=None,
        host_list=C.DEFAULT_HOST_LIST,
        module_path=None,
        forks=C.DEFAULT_FORKS,
        timeout=C.DEFAULT_TIMEOUT,
        remote_user=C.DEFAULT_REMOTE_USER,
        remote_pass=C.DEFAULT_REMOTE_PASS,
        remote_port=None,
        transport=C.DEFAULT_TRANSPORT,
        private_key_file=C.DEFAULT_PRIVATE_KEY_FILE,
        callbacks=None,
        runner_callbacks=None,
        stats=None,
        extra_vars=None,
        only_tags=None,
        skip_tags=None,
        subset=C.DEFAULT_SUBSET,
        inventory=None,
        check=False,
        diff=False,
        any_errors_fatal=False,
        vault_password=False,
        force_handlers=False,
        # privelege escalation
        become=C.DEFAULT_BECOME,
        become_method=C.DEFAULT_BECOME_METHOD,
        become_user=C.DEFAULT_BECOME_USER,
        become_pass=None,
    ):
        """
        playbook:         path to a playbook file
        host_list:        path to a file like /etc/ansible/hosts
        module_path:      path to ansible modules, like /usr/share/ansible/
        forks:            desired level of parallelism
        timeout:          connection timeout
        remote_user:      run as this user if not specified in a particular play
        remote_pass:      use this remote password (for all plays) vs using SSH keys
        remote_port:      default remote port to use if not specified with the host or play
        transport:        how to connect to hosts that don't specify a transport (local, paramiko, etc)
        callbacks         output callbacks for the playbook
        runner_callbacks: more callbacks, this time for the runner API
        stats:            holds aggregrate data about events occurring to each host
        inventory:        can be specified instead of host_list to use a pre-existing inventory object
        check:            don't change anything, just try to detect some potential changes
        any_errors_fatal: terminate the entire execution immediately when one of the hosts has failed
        force_handlers:   continue to notify and run handlers even if a task fails
        """

        self.SETUP_CACHE = SETUP_CACHE
        self.VARS_CACHE = VARS_CACHE

        arguments = []
        if playbook is None:
            arguments.append('playbook')
        if callbacks is None:
            arguments.append('callbacks')
        if runner_callbacks is None:
            arguments.append('runner_callbacks')
        if stats is None:
            arguments.append('stats')
        if arguments:
            raise Exception('PlayBook missing required arguments: %s' %
                            ', '.join(arguments))

        if extra_vars is None:
            extra_vars = {}
        if only_tags is None:
            only_tags = ['all']
        if skip_tags is None:
            skip_tags = []

        self.check = check
        self.diff = diff
        self.module_path = module_path
        self.forks = forks
        self.timeout = timeout
        self.remote_user = remote_user
        self.remote_pass = remote_pass
        self.remote_port = remote_port
        self.transport = transport
        self.callbacks = callbacks
        self.runner_callbacks = runner_callbacks
        self.stats = stats
        self.extra_vars = extra_vars
        self.global_vars = {}
        self.private_key_file = private_key_file
        self.only_tags = only_tags
        self.skip_tags = skip_tags
        self.any_errors_fatal = any_errors_fatal
        self.vault_password = vault_password
        self.force_handlers = force_handlers

        self.become = become
        self.become_method = become_method
        self.become_user = become_user
        self.become_pass = become_pass

        self.callbacks.playbook = self
        self.runner_callbacks.playbook = self

        if inventory is None:
            self.inventory = ansible.inventory.Inventory(host_list)
            self.inventory.subset(subset)
        else:
            self.inventory = inventory

        if self.module_path is not None:
            utils.plugins.module_finder.add_directory(self.module_path)

        self.basedir = os.path.dirname(playbook) or '.'
        utils.plugins.push_basedir(self.basedir)

        # let inventory know the playbook basedir so it can load more vars
        self.inventory.set_playbook_basedir(self.basedir)

        vars = extra_vars.copy()
        vars['playbook_dir'] = os.path.abspath(self.basedir)
        if self.inventory.basedir() is not None:
            vars['inventory_dir'] = self.inventory.basedir()

        if self.inventory.src() is not None:
            vars['inventory_file'] = self.inventory.src()

        self.filename = playbook
        (self.playbook,
         self.play_basedirs) = self._load_playbook_from_file(playbook, vars)
        ansible.callbacks.load_callback_plugins()
        ansible.callbacks.set_playbook(self.callbacks, self)

        self._ansible_version = utils.version_info(gitinfo=True)