Example #1
0
    def get_play_prereqs_2(self, options):
        loader = DataLoader()

        if self.vault_pass:
            loader.set_vault_password(self.vault_pass)

        variable_manager = VariableManager()
        variable_manager.extra_vars = self.extra_vars
        variable_manager.options_vars = {
            'ansible_version': self.version_info(ansible_version)
        }

        # Add this to avoid the Ansible bug:  no host vars as host is not in inventory
        # In version 2.0.1 it must be fixed
        ansible.inventory.HOSTS_PATTERNS_CACHE = {}

        inventory = Inventory(loader=loader,
                              variable_manager=variable_manager,
                              host_list=options.inventory)
        variable_manager.set_inventory(inventory)

        if self.host:
            inventory.subset(self.host)
        # let inventory know which playbooks are using so it can know the
        # basedirs
        inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))

        return loader, inventory, variable_manager
Example #2
0
    def _get_pb(self):
        if not self.pb:
            inventory = ansible.inventory.Inventory(self.inventory)
            inventory.subset(self.subset)

            stats = callbacks.AggregateStats()
            self.playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
            # 任务执行日志会通过sys.stdout输出
            runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
            
            self.pb = ansible.playbook.PlayBook(
            		playbook=self.playbook,
            		inventory=inventory,
                    only_tags=self.only_tags,
                    skip_tags=self.skip_tags,
            		stats=stats,
            		callbacks=self.playbook_cb,
            		runner_callbacks=runner_cb
            		#check=True
            		)
        return self.pb
Example #3
0
    def launch_playbook_v1(self):
        ''' run ansible-playbook operations v1.X'''
        # create parser for CLI options
        parser = utils.base_parser(
            constants=C,
            usage="%prog playbook.yml",
            connect_opts=True,
            runas_opts=True,
            subset_opts=True,
            check_opts=True,
            diff_opts=True
        )

        options, _ = parser.parse_args([])

        sshpass = None
        if self.pk_file:
            options.private_key_file = self.pk_file
        else:
            sshpass = self.passwd

        if self.user:
            remote_user = self.user
        else:
            remote_user = options.remote_user

        if not os.path.exists(self.playbook_file):
            raise errors.AnsibleError(
                "the playbook: %s could not be found" % self.playbook_file)
        if not os.path.isfile(self.playbook_file):
            raise errors.AnsibleError(
                "the playbook: %s does not appear to be a file" % self.playbook_file)

        num_retries = 0
        return_code = 4
        hosts_with_errors = []
        while return_code != 0 and num_retries < self.retries:
            time.sleep(5 * num_retries)
            num_retries += 1
            return_code = 0

            if self.inventory_file:
                inventory = ansible.inventory.Inventory(self.inventory_file)
            else:
                inventory = ansible.inventory.Inventory(options.inventory)

            if self.host:
                inventory.subset(self.host)
            # let inventory know which playbooks are using so it can know the
            # basedirs
            inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))

            stats = AggregateStats()
            playbook_cb = PlaybookCallbacks(
                verbose=utils.VERBOSITY, output=self.output)
            runner_cb = PlaybookRunnerCallbacks(
                stats, verbose=utils.VERBOSITY, output=self.output)

            pb = ansible.playbook.PlayBook(
                playbook=self.playbook_file,
                module_path=options.module_path,
                inventory=inventory,
                forks=self.threads,
                remote_user=remote_user,
                remote_pass=sshpass,
                callbacks=playbook_cb,
                runner_callbacks=runner_cb,
                stats=stats,
                extra_vars=self.extra_vars,
                private_key_file=options.private_key_file,
                only_tags=['all']
            )

            try:
                failed_hosts = []
                unreachable_hosts = []

                pb.run()

                hosts = sorted(pb.stats.processed.keys())
                display(banner("PLAY RECAP"), output=self.output)
                playbook_cb.on_stats(pb.stats)

                for h in hosts:
                    t = pb.stats.summarize(h)
                    if t['failures'] > 0:
                        failed_hosts.append(h)
                    if t['unreachable'] > 0:
                        unreachable_hosts.append(h)

                hosts_with_errors = failed_hosts + unreachable_hosts

                for h in hosts:
                    t = pb.stats.summarize(h)

                    display("%s : %s %s %s %s" % (
                        hostcolor(h, t),
                        colorize('ok', t['ok'], 'green'),
                        colorize('changed', t['changed'], 'yellow'),
                        colorize('unreachable', t['unreachable'], 'red'),
                        colorize('failed', t['failures'], 'red')),
                        screen_only=True, output=self.output
                    )

                    display("%s : %s %s %s %s" % (
                        hostcolor(h, t, False),
                        colorize('ok', t['ok'], None),
                        colorize('changed', t['changed'], None),
                        colorize('unreachable', t['unreachable'], None),
                        colorize('failed', t['failures'], None)),
                        log_only=True, output=self.output
                    )

                if len(failed_hosts) > 0:
                    return_code = 2
                if len(unreachable_hosts) > 0:
                    return_code = 3

            except errors.AnsibleError, e:
                display("ERROR: %s" % e, color='red', output=self.output)
                return_code = 1

            if return_code != 0:
                display("ERROR executing playbook (%s/%s)" %
                        (num_retries, self.retries), color='red', output=self.output)
Example #4
0
    def launch_playbook_v2(self):
        ''' run ansible-playbook operations v2.X'''
        # create parser for CLI options
        parser = CLI.base_parser(
            usage="%prog playbook.yml",
            connect_opts=True,
            meta_opts=True,
            runas_opts=True,
            subset_opts=True,
            check_opts=True,
            inventory_opts=True,
            runtask_opts=True,
            vault_opts=True,
            fork_opts=True,
            module_opts=True,
        )

        options, _ = parser.parse_args([])

        sshpass = None
        if not options.become_user:
            options.become_user = "******"

        if self.pk_file:
            options.private_key_file = self.pk_file
        else:
            sshpass = self.passwd

        passwords = {'conn_pass': sshpass, 'become_pass': sshpass}

        if self.user:
            options.remote_user = self.user

        if not os.path.exists(self.playbook_file):
            raise errors.AnsibleError(
                "the playbook: %s could not be found" % self.playbook_file)
        if not os.path.isfile(self.playbook_file):
            raise errors.AnsibleError(
                "the playbook: %s does not appear to be a file" % self.playbook_file)

        variable_manager = VariableManager()
        variable_manager.extra_vars = self.extra_vars

        if self.inventory_file:
            options.inventory = self.inventory_file

        options.forks = self.threads

        loader = DataLoader()
        # Add this to avoid the Ansible bug:  no host vars as host is not in inventory
        # In version 2.0.1 it must be fixed
        ansible.inventory.HOSTS_PATTERNS_CACHE = {}

        inventory = ansible.inventory.Inventory(
            loader=loader, variable_manager=variable_manager, host_list=options.inventory)
        variable_manager.set_inventory(inventory)

        if self.host:
            inventory.subset(self.host)
        # let inventory know which playbooks are using so it can know the
        # basedirs
        inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))

        num_retries = 0
        return_code = 4
        results = None

        while return_code != 0 and num_retries < self.retries:
            time.sleep(5 * num_retries)
            num_retries += 1
            return_code = 0

            try:
                # create the playbook executor, which manages running the plays
                # via a task queue manager
                pbex = IMPlaybookExecutor(playbooks=[self.playbook_file],
                                          inventory=inventory,
                                          variable_manager=variable_manager,
                                          loader=loader,
                                          options=options,
                                          passwords=passwords,
                                          output=self.output)

                return_code = pbex.run()

            except errors.AnsibleError, e:
                display("ERROR: %s" % e, output=self.output)
                return_code = 1

            if return_code != 0:
                display("ERROR executing playbook (%s/%s)" %
                        (num_retries, self.retries), output=self.output)
Example #5
0
    def launch_playbook_v1(self):
        ''' run ansible-playbook operations v1.X'''
        # create parser for CLI options
        parser = utils.base_parser(constants=C,
                                   usage="%prog playbook.yml",
                                   connect_opts=True,
                                   runas_opts=True,
                                   subset_opts=True,
                                   check_opts=True,
                                   diff_opts=True)

        options, _ = parser.parse_args([])

        sshpass = None
        if self.pk_file:
            options.private_key_file = self.pk_file
        else:
            sshpass = self.passwd

        if self.user:
            remote_user = self.user
        else:
            remote_user = options.remote_user

        if not os.path.exists(self.playbook_file):
            raise errors.AnsibleError("the playbook: %s could not be found" %
                                      self.playbook_file)
        if not os.path.isfile(self.playbook_file):
            raise errors.AnsibleError(
                "the playbook: %s does not appear to be a file" %
                self.playbook_file)

        num_retries = 0
        return_code = 4
        hosts_with_errors = []
        while return_code != 0 and num_retries < self.retries:
            time.sleep(5 * num_retries)
            num_retries += 1
            return_code = 0

            if self.inventory_file:
                inventory = ansible.inventory.Inventory(self.inventory_file)
            else:
                inventory = ansible.inventory.Inventory(options.inventory)

            if self.host:
                inventory.subset(self.host)
            # let inventory know which playbooks are using so it can know the
            # basedirs
            inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))

            stats = AggregateStats()
            playbook_cb = PlaybookCallbacks(verbose=utils.VERBOSITY,
                                            output=self.output)
            runner_cb = PlaybookRunnerCallbacks(stats,
                                                verbose=utils.VERBOSITY,
                                                output=self.output)

            pb = ansible.playbook.PlayBook(
                playbook=self.playbook_file,
                module_path=options.module_path,
                inventory=inventory,
                forks=self.threads,
                remote_user=remote_user,
                remote_pass=sshpass,
                callbacks=playbook_cb,
                runner_callbacks=runner_cb,
                stats=stats,
                extra_vars=self.extra_vars,
                private_key_file=options.private_key_file,
                only_tags=['all'])

            try:
                failed_hosts = []
                unreachable_hosts = []

                pb.run()

                hosts = sorted(pb.stats.processed.keys())
                display(banner("PLAY RECAP"), output=self.output)
                playbook_cb.on_stats(pb.stats)

                for h in hosts:
                    t = pb.stats.summarize(h)
                    if t['failures'] > 0:
                        failed_hosts.append(h)
                    if t['unreachable'] > 0:
                        unreachable_hosts.append(h)

                hosts_with_errors = failed_hosts + unreachable_hosts

                for h in hosts:
                    t = pb.stats.summarize(h)

                    display("%s : %s %s %s %s" %
                            (hostcolor(h, t), colorize('ok', t['ok'], 'green'),
                             colorize('changed', t['changed'], 'yellow'),
                             colorize('unreachable', t['unreachable'], 'red'),
                             colorize('failed', t['failures'], 'red')),
                            screen_only=True,
                            output=self.output)

                    display(
                        "%s : %s %s %s %s" %
                        (hostcolor(h, t, False), colorize('ok', t['ok'], None),
                         colorize('changed', t['changed'], None),
                         colorize('unreachable', t['unreachable'], None),
                         colorize('failed', t['failures'], None)),
                        log_only=True,
                        output=self.output)

                if len(failed_hosts) > 0:
                    return_code = 2
                if len(unreachable_hosts) > 0:
                    return_code = 3

            except errors.AnsibleError, e:
                display("ERROR: %s" % e, color='red', output=self.output)
                return_code = 1

            if return_code != 0:
                display("ERROR executing playbook (%s/%s)" %
                        (num_retries, self.retries),
                        color='red',
                        output=self.output)
Example #6
0
    def launch_playbook_v2(self):
        ''' run ansible-playbook operations v2.X'''
        # create parser for CLI options
        parser = CLI.base_parser(
            usage="%prog playbook.yml",
            connect_opts=True,
            meta_opts=True,
            runas_opts=True,
            subset_opts=True,
            check_opts=True,
            inventory_opts=True,
            runtask_opts=True,
            vault_opts=True,
            fork_opts=True,
            module_opts=True,
        )

        options, _ = parser.parse_args([])

        sshpass = None
        if not options.become_user:
            options.become_user = "******"

        if self.pk_file:
            options.private_key_file = self.pk_file
        else:
            sshpass = self.passwd

        passwords = {'conn_pass': sshpass, 'become_pass': sshpass}

        if self.user:
            options.remote_user = self.user

        if not os.path.exists(self.playbook_file):
            raise errors.AnsibleError("the playbook: %s could not be found" %
                                      self.playbook_file)
        if not os.path.isfile(self.playbook_file):
            raise errors.AnsibleError(
                "the playbook: %s does not appear to be a file" %
                self.playbook_file)

        variable_manager = VariableManager()
        variable_manager.extra_vars = self.extra_vars

        if self.inventory_file:
            options.inventory = self.inventory_file

        options.forks = self.threads

        loader = DataLoader()
        # Add this to avoid the Ansible bug:  no host vars as host is not in inventory
        # In version 2.0.1 it must be fixed
        ansible.inventory.HOSTS_PATTERNS_CACHE = {}

        inventory = ansible.inventory.Inventory(
            loader=loader,
            variable_manager=variable_manager,
            host_list=options.inventory)
        variable_manager.set_inventory(inventory)

        if self.host:
            inventory.subset(self.host)
        # let inventory know which playbooks are using so it can know the
        # basedirs
        inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))

        num_retries = 0
        return_code = 4
        results = None

        while return_code != 0 and num_retries < self.retries:
            time.sleep(5 * num_retries)
            num_retries += 1
            return_code = 0

            try:
                # create the playbook executor, which manages running the plays
                # via a task queue manager
                pbex = IMPlaybookExecutor(playbooks=[self.playbook_file],
                                          inventory=inventory,
                                          variable_manager=variable_manager,
                                          loader=loader,
                                          options=options,
                                          passwords=passwords,
                                          output=self.output)

                return_code = pbex.run()

            except errors.AnsibleError, e:
                display("ERROR: %s" % e, output=self.output)
                return_code = 1

            if return_code != 0:
                display("ERROR executing playbook (%s/%s)" %
                        (num_retries, self.retries),
                        output=self.output)
Example #7
0
    def __init__(self,
                 name=None,
                 ablefile=None,
                 callbacks=None,
                 runner_callbacks=None,
                 stats=None,
                 data=None,
                 params={},
                 global_data={},
                 global_params={},
                 default_vars={},
    ):

        """
        name:             name of command
        ablefile:         instance of ablefile
        data:             dictionary with command data
        global_data:      dictionary of global data
        callbacks         output callbacks for the playbook
        runner_callbacks: more callbacks, this time for the runner API
        stats:            holds aggregrate data about events occuring to each host
        default_vars:     default variables
        extra_vars:       additional variables from command options and arguments
        """

        if data is None or ablefile is None:
            raise Exception('missing required arguments')

        self.name = name

        self.ablefile = ablefile
        self.data = data
        self.extra_vars = params
        self.global_data = global_data
        self.global_params = global_params
        self.default_vars = default_vars

        self.basedir = self.ablefile.basedir

        self.settings = self._get_settings(data, global_data, global_params)
        self.settings = self._set_passwords(self.settings)

        self.only_plays = self.settings.pop('only_plays', None)
        self.skip_plays = self.settings.pop('skip_plays', None)

        if self.only_plays:
            self.only_plays = self.only_plays.split(",")
        if self.skip_plays:
            self.skip_plays = self.skip_plays.split(",")

        inventory = Inventory(self.settings.pop('inventory'))
        inventory.subset(self.settings.pop('subset'))
        if len(inventory.list_hosts()) == 0:
            raise errors.AnsibleError("provided hosts list is empty")
        # let inventory know which ablefile is using so it can know the basedirs

        inventory.set_playbook_basedir(self.basedir)

        self.groups = self.get_groups(self.data, self.settings)
        self.plays = self.get_plays(self.data, self.groups)
        self.tasks = None

        super(Command, self).__init__(
            playbook=ablefile.filename,
            callbacks=callbacks,
            runner_callbacks=runner_callbacks,
            stats=stats,
            inventory=inventory,
            transport=self.settings.pop('connection'),
            extra_vars=self.extra_vars,
            **self.settings
        )
from ansible import callbacks
from ansible import utils

utils.VERBOSITY = 3

ANSIBLE_HOSTS = 'local'
INVENTORY_FILE = 'servers.yml'
PLAYBOOK = 'playbook.yml'
CONFIG_RECIPES_FILE = 'script-config.yml'

playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
stats = callbacks.AggregateStats()
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

inventory = inventory.Inventory(INVENTORY_FILE)
inventory.subset(ANSIBLE_HOSTS)

inventory.set_playbook_basedir(os.path.dirname(PLAYBOOK))
hosts = inventory.list_hosts(ANSIBLE_HOSTS)

if len(hosts)==0:
    raise Exception('Could not find any host to match "%s" in the inventory "%s" file' % (ANSIBLE_HOSTS, INVENTORY_FILE))

stream = open(CONFIG_RECIPES_FILE, 'r')
configurations = yaml.load(stream)

for recipe in configurations:
    print recipe['only_tags']

    dictionary = dict(
            inventory=inventory,
Example #9
0
from ansible import callbacks
from ansible import utils

utils.VERBOSITY = 3

ANSIBLE_HOSTS = 'local'
INVENTORY_FILE = 'servers.yml'
PLAYBOOK = 'playbook.yml'
CONFIG_RECIPES_FILE = 'script-config.yml'

playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
stats = callbacks.AggregateStats()
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

inventory = inventory.Inventory(INVENTORY_FILE)
inventory.subset(ANSIBLE_HOSTS)

inventory.set_playbook_basedir(os.path.dirname(PLAYBOOK))
hosts = inventory.list_hosts(ANSIBLE_HOSTS)

if len(hosts) == 0:
    raise Exception(
        'Could not find any host to match "%s" in the inventory "%s" file' %
        (ANSIBLE_HOSTS, INVENTORY_FILE))

stream = open(CONFIG_RECIPES_FILE, 'r')
configurations = yaml.load(stream)

for recipe in configurations:
    print recipe['only_tags']