当前位置: 首页>>代码示例>>Python>>正文


Python playbook.Playbook类代码示例

本文整理汇总了Python中ansible.playbook.Playbook的典型用法代码示例。如果您正苦于以下问题:Python Playbook类的具体用法?Python Playbook怎么用?Python Playbook使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Playbook类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load_data

    def load_data(self, ds, basedir, variable_manager=None, loader=None):
        '''
        Overrides the base load_data(), as we're actually going to return a new
        Playbook() object rather than a PlaybookInclude object
        '''

        # import here to avoid a dependency loop
        from ansible.playbook import Playbook

        # first, we use the original parent method to correctly load the object
        # via the munge/load_data system we normally use for other playbook objects
        new_obj = super(PlaybookInclude, self).load_data(ds, variable_manager, loader)

        # then we use the object to load a Playbook
        pb = Playbook(loader=loader)

        file_name = new_obj.include
        if not os.path.isabs(file_name):
            file_name = os.path.join(basedir, file_name)

        pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)

        # finally, playbook includes can specify a list of variables, which are simply
        # used to update the vars of each play in the playbook
        for entry in pb._entries:
            entry.vars.update(new_obj.vars)

        return pb
开发者ID:dataxu,项目名称:ansible,代码行数:28,代码来源:playbook_include.py

示例2: load_data

    def load_data(self, ds, basedir, variable_manager=None, loader=None):
        '''
        Overrides the base load_data(), as we're actually going to return a new
        Playbook() object rather than a PlaybookInclude object
        '''

        # import here to avoid a dependency loop
        from ansible.playbook import Playbook

        # first, we use the original parent method to correctly load the object
        # via the load_data/preprocess_data system we normally use for other
        # playbook objects
        new_obj = super(PlaybookInclude, self).load_data(ds, variable_manager, loader)

        # then we use the object to load a Playbook
        pb = Playbook(loader=loader)

        file_name = new_obj.include
        if not os.path.isabs(file_name):
            file_name = os.path.join(basedir, file_name)

        pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)

        # finally, update each loaded playbook entry with any variables specified
        # on the included playbook and/or any tags which may have been set
        for entry in pb._entries:
            entry.vars.update(new_obj.vars)
            entry.tags = list(set(entry.tags).union(new_obj.tags))

        return pb
开发者ID:ferhaty,项目名称:ansible,代码行数:30,代码来源:playbook_include.py

示例3: load_data

    def load_data(self, ds, basedir, variable_manager=None, loader=None):
        '''
        Overrides the base load_data(), as we're actually going to return a new
        Playbook() object rather than a PlaybookInclude object
        '''

        # import here to avoid a dependency loop
        from ansible.playbook import Playbook

        # first, we use the original parent method to correctly load the object
        # via the load_data/preprocess_data system we normally use for other
        # playbook objects
        new_obj = super(PlaybookInclude, self).load_data(ds, variable_manager, loader)

        all_vars = self.vars.copy()
        if variable_manager:
            all_vars.update(variable_manager.get_vars(loader=loader))

        templar = Templar(loader=loader, variables=all_vars)

        try:
            forward_conditional = False
            if not new_obj.evaluate_conditional(templar=templar, all_vars=all_vars):
                return None
        except AnsibleError:
            # conditional evaluation raised an error, so we set a flag to indicate
            # we need to forward the conditionals on to the included play(s)
            forward_conditional = True

        # then we use the object to load a Playbook
        pb = Playbook(loader=loader)

        file_name = templar.template(new_obj.include)
        if not os.path.isabs(file_name):
            file_name = os.path.join(basedir, file_name)

        pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)

        # finally, update each loaded playbook entry with any variables specified
        # on the included playbook and/or any tags which may have been set
        for entry in pb._entries:
            temp_vars = entry.vars.copy()
            temp_vars.update(new_obj.vars)
            param_tags = temp_vars.pop('tags', None)
            if param_tags is not None:
                entry.tags.extend(param_tags.split(','))
            entry.vars = temp_vars
            entry.tags = list(set(entry.tags).union(new_obj.tags))
            if entry._included_path is None:
                entry._included_path = os.path.dirname(file_name)

            # Check to see if we need to forward the conditionals on to the included
            # plays. If so, we can take a shortcut here and simply prepend them to
            # those attached to each block (if any)
            if forward_conditional:
                for task_block in entry.pre_tasks + entry.roles + entry.tasks + entry.post_tasks:
                    task_block.when = self.when[:] + task_block.when

        return pb
开发者ID:KMK-ONLINE,项目名称:ansible,代码行数:59,代码来源:playbook_include.py

示例4: load_data

    def load_data(self, ds, basedir, variable_manager=None, loader=None):
        '''
        Overrides the base load_data(), as we're actually going to return a new
        Playbook() object rather than a PlaybookInclude object
        '''

        # import here to avoid a dependency loop
        from ansible.playbook import Playbook
        from ansible.playbook.play import Play

        # first, we use the original parent method to correctly load the object
        # via the load_data/preprocess_data system we normally use for other
        # playbook objects
        new_obj = super(PlaybookInclude, self).load_data(ds, variable_manager, loader)

        all_vars = self.vars.copy()
        if variable_manager:
            all_vars.update(variable_manager.get_vars())

        templar = Templar(loader=loader, variables=all_vars)

        # then we use the object to load a Playbook
        pb = Playbook(loader=loader)

        file_name = templar.template(new_obj.import_playbook)
        if not os.path.isabs(file_name):
            file_name = os.path.join(basedir, file_name)

        pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)

        # finally, update each loaded playbook entry with any variables specified
        # on the included playbook and/or any tags which may have been set
        for entry in pb._entries:

            # conditional includes on a playbook need a marker to skip gathering
            if new_obj.when and isinstance(entry, Play):
                entry._included_conditional = new_obj.when[:]

            temp_vars = entry.vars.copy()
            temp_vars.update(new_obj.vars)
            param_tags = temp_vars.pop('tags', None)
            if param_tags is not None:
                entry.tags.extend(param_tags.split(','))
            entry.vars = temp_vars
            entry.tags = list(set(entry.tags).union(new_obj.tags))
            if entry._included_path is None:
                entry._included_path = os.path.dirname(file_name)

            # Check to see if we need to forward the conditionals on to the included
            # plays. If so, we can take a shortcut here and simply prepend them to
            # those attached to each block (if any)
            if new_obj.when:
                for task_block in (entry.pre_tasks + entry.roles + entry.tasks + entry.post_tasks):
                    task_block._attributes['when'] = new_obj.when[:] + task_block.when[:]

        return pb
开发者ID:ernstp,项目名称:ansible,代码行数:56,代码来源:playbook_include.py

示例5: test_playbook_executor__get_serialized_batches

    def test_playbook_executor__get_serialized_batches(self):
        fake_loader = DictDataLoader({
            'no_serial.yml': '''
            - hosts: all
              gather_facts: no
              tasks:
              - debug: var=inventory_hostname
            ''',
            'serial_int.yml': '''
            - hosts: all
              gather_facts: no
              serial: 2
              tasks:
              - debug: var=inventory_hostname
            ''',
            'serial_pct.yml': '''
            - hosts: all
              gather_facts: no
              serial: 20%
              tasks:
              - debug: var=inventory_hostname
            ''',
        })

        mock_inventory = MagicMock()
        mock_var_manager = MagicMock()

        # fake out options to use the syntax CLI switch, which will ensure
        # the PlaybookExecutor doesn't create a TaskQueueManager
        mock_options = MagicMock()
        mock_options.syntax.value = True

        pbe = PlaybookExecutor(
            playbooks=['no_serial.yml', 'serial_int.yml', 'serial_pct.yml'],
            inventory=mock_inventory,
            variable_manager=mock_var_manager,
            loader=fake_loader,
            options=mock_options,
            passwords=[],
        )

        playbook = Playbook.load(pbe._playbooks[0], variable_manager=mock_var_manager, loader=fake_loader)
        play = playbook.get_plays()[0]
        mock_inventory.get_hosts.return_value = ['host0','host1','host2','host3','host4','host5','host6','host7','host8','host9']
        self.assertEqual(pbe._get_serialized_batches(play), [['host0','host1','host2','host3','host4','host5','host6','host7','host8','host9']])

        playbook = Playbook.load(pbe._playbooks[1], variable_manager=mock_var_manager, loader=fake_loader)
        play = playbook.get_plays()[0]
        mock_inventory.get_hosts.return_value = ['host0','host1','host2','host3','host4','host5','host6','host7','host8','host9']
        self.assertEqual(pbe._get_serialized_batches(play), [['host0','host1'],['host2','host3'],['host4','host5'],['host6','host7'],['host8','host9']])

        playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader)
        play = playbook.get_plays()[0]
        mock_inventory.get_hosts.return_value = ['host0','host1','host2','host3','host4','host5','host6','host7','host8','host9']
        self.assertEqual(pbe._get_serialized_batches(play), [['host0','host1'],['host2','host3'],['host4','host5'],['host6','host7'],['host8','host9']])
开发者ID:dyim42,项目名称:dfw-meetup-20160312,代码行数:55,代码来源:test_playbook_executor.py

示例6: create_playbook

def create_playbook(playbook_path):
    """
    Load the given playbook and return the ansible playbook.

    :param playbook_path: Path to the playbook file.
    :type playbook_path: :class:`str`
    :returns: The loaded ansible playbook.
    :rtype: :class:`ansible.playbook.Playbook`
    """
    try:
        playbook = Playbook.load(
            playbook_path,
            loader=LOADER,
            variable_manager=VARIABLE_MANAGER,
        )

        scrape_playbook(playbook)
        return playbook
    except Exception as error:  # pylint: disable=broad-except
        logging.exception(
            "Unexpected error scrapping playbook %r: %r",
            playbook_path, error
        )

        raise argparse.ArgumentTypeError(error)
开发者ID:jenmud,项目名称:ansible-graph,代码行数:25,代码来源:__init__.py

示例7: test_basic_playbook

 def test_basic_playbook(self):
     fake_loader = DictDataLoader({
         "test_file.yml":"""
         - hosts: all
         """,
     })
     p = Playbook.load("test_file.yml", loader=fake_loader)
     plays = p.get_plays()
开发者ID:victron,项目名称:paramiko_ssh-i,代码行数:8,代码来源:test_playbook.py

示例8: load_data

    def load_data(self, ds, basedir, variable_manager=None, loader=None):
        '''
        Overrides the base load_data(), as we're actually going to return a new
        Playbook() object rather than a PlaybookInclude object
        '''

        # import here to avoid a dependency loop
        from ansible.playbook import Playbook

        # first, we use the original parent method to correctly load the object
        # via the load_data/preprocess_data system we normally use for other
        # playbook objects
        new_obj = super(PlaybookInclude, self).load_data(ds, variable_manager, loader)

        all_vars = dict()
        if variable_manager:
            all_vars = variable_manager.get_vars(loader=loader)

        templar = Templar(loader=loader, variables=all_vars)
        if not new_obj.evaluate_conditional(templar=templar, all_vars=all_vars):
            return None

        # then we use the object to load a Playbook
        pb = Playbook(loader=loader)

        file_name = new_obj.include
        if not os.path.isabs(file_name):
            file_name = os.path.join(basedir, file_name)

        pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)

        # finally, update each loaded playbook entry with any variables specified
        # on the included playbook and/or any tags which may have been set
        for entry in pb._entries:
            temp_vars = entry.vars.copy()
            temp_vars.update(new_obj.vars)
            param_tags = temp_vars.pop('tags', None)
            if param_tags is not None:
                entry.tags.extend(param_tags.split(','))
            entry.vars = temp_vars
            entry.tags = list(set(entry.tags).union(new_obj.tags))
            if entry._included_path is None:
                entry._included_path = os.path.dirname(file_name)

        return pb
开发者ID:source-foundry,项目名称:code-corpora,代码行数:45,代码来源:playbook_include.py

示例9: test_play_iterator

    def test_play_iterator(self):
        fake_loader = DictDataLoader({
            "test_play.yml": """
            - hosts: all
              gather_facts: false
              roles:
              - test_role
              pre_tasks:
              - debug: msg="this is a pre_task"
              tasks:
              - debug: msg="this is a regular task"
              post_tasks:
              - debug: msg="this is a post_task"
            """,
            '/etc/ansible/roles/test_role/tasks/main.yml': """
            - debug: msg="this is a role task"
            """,
        })

        p = Playbook.load('test_play.yml', loader=fake_loader)

        hosts = []
        for i in range(0, 10):
            host  = MagicMock()
            host.get_name.return_value = 'host%02d' % i
            hosts.append(host)

        inventory = MagicMock()
        inventory.get_hosts.return_value = hosts
        inventory.filter_hosts.return_value = hosts

        connection_info = ConnectionInformation(play=p._entries[0])

        itr = PlayIterator(
            inventory=inventory,
            play=p._entries[0],
            connection_info=connection_info,
            all_vars=dict(),
        )

        (host_state, task) = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNotNone(task)
        (host_state, task) = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNotNone(task)
        (host_state, task) = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNotNone(task)
        (host_state, task) = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNotNone(task)
        (host_state, task) = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNone(task)
开发者ID:ferhaty,项目名称:ansible,代码行数:55,代码来源:test_play_iterator.py

示例10: run

    def run(self):

        '''
        Run the given playbook, based on the settings in the play which
        may limit the runs to serialized groups, etc.
        '''

        signal.signal(signal.SIGINT, self._cleanup)

        result = 0
        try:
            for playbook_path in self._playbooks:
                pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
        
                # FIXME: playbook entries are just plays, so we should rename them
                for play in pb.get_entries():
                    self._inventory.remove_restriction()

                    # Create a temporary copy of the play here, so we can run post_validate
                    # on it without the templating changes affecting the original object.
                    all_vars = self._variable_manager.get_vars(loader=self._loader, play=play)
                    new_play = play.copy()
                    new_play.post_validate(all_vars, fail_on_undefined=False)

                    for batch in self._get_serialized_batches(new_play):
                        if len(batch) == 0:
                            self._tqm._callback.playbook_on_play_start(new_play.name)
                            self._tqm._callback.playbook_on_no_hosts_matched()
                            result = 0
                            break
                        # restrict the inventory to the hosts in the serialized batch
                        self._inventory.restrict_to_hosts(batch)
                        # and run it...
                        result = self._tqm.run(play=play)
                        if result != 0:
                            break

                    if result != 0:
                        # FIXME: do something here, to signify the playbook execution failed
                        self._cleanup()
                        return result
        except:
            self._cleanup()
            raise

        self._cleanup()
        return result
开发者ID:dataxu,项目名称:ansible,代码行数:47,代码来源:playbook_executor.py

示例11: test_playbook_iterator

    def test_playbook_iterator(self):
        fake_loader = DictDataLoader({
            "test_play.yml": """
            - hosts: all
              roles:
              - test_role
              pre_tasks:
              - debug: msg="this is a pre_task"
              tasks:
              - debug: msg="this is a regular task"
              post_tasks:
              - debug: msg="this is a post_task"
            """,
            '/etc/ansible/roles/test_role/tasks/main.yml': """
            - debug: msg="this is a role task"
            """,
        })

        p = Playbook.load('test_play.yml', loader=fake_loader)

        hosts = []
        for i in range(0, 10):
            host  = MagicMock()
            host.get_name.return_value = 'host%02d' % i
            hosts.append(host)

        inventory = MagicMock()
        inventory.get_hosts.return_value = hosts

        itr = PlaybookIterator(inventory, None, p)
        task = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNotNone(task)
        task = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNotNone(task)
        task = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNotNone(task)
        task = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNotNone(task)
        task = itr.get_next_task_for_host(hosts[0])
        print(task)
        self.assertIsNone(task)
开发者ID:conlini,项目名称:frozen-ansible,代码行数:45,代码来源:test_playbook_iterator.py

示例12: build_plays

def build_plays(loader, variable_manager, playbook_path, plays=[], hosts=[]):
    playbook = Playbook.load(playbook_path, variable_manager, loader)
    plays = []
    
    for play in playbook.get_plays():
        if play.get_name() in plays:
            plays.append(play)
            continue
        
        if play._ds['hosts'] in hosts:
            plays.append(play)
            continue

        for piece in play._ds['hosts']:
            if piece in hosts:
                plays.append(play)
                break

    return plays
开发者ID:vmindru,项目名称:terraform-provisioner-ansible,代码行数:19,代码来源:ansible-local.py

示例13: run

    def run(self, playbook):
        pb = Playbook.load(playbook, variable_manager=self.variable_manager, loader=self.loader)
        # only support one playbook.yml
        play = pb.get_plays()[0]

        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords={'conn_pass': None, 'become_pass': None},
                stdout_callback=self.callback,
            )
            return tqm.run(play), self.callback.result
        finally:
            if tqm is not None:
                tqm.cleanup()
开发者ID:nxintech,项目名称:Charlie,代码行数:19,代码来源:playbook_api_new.py

示例14: _execute_playbook

def _execute_playbook(playbook_name):
    """
    Execute a playbook stored in the *share_dir*.
    """
    install_dir = os.path.dirname(os.path.dirname(sys.executable))
    share_dir = os.path.join(install_dir, 'share', 'dws')
    playbook_path = os.path.join(share_dir, 'playbooks', playbook_name)
    if not os.path.exists(playbook_path):
        # When running directly from within src_dir.
        share_dir = os.path.join(install_dir, 'share')
        playbook_path = os.path.join(share_dir, 'playbooks', playbook_name)
    sysconf_dir = os.path.join(install_dir, 'etc')
    Options = namedtuple('Options', ['connection', 'module_path', 'forks',
        'become', 'become_method', 'become_user', 'check'])
    options = Options(connection='local',
        module_path=os.path.dirname(tero.__file__), forks=100, become=None,
        become_method=None, become_user=None, check=False)
    passwords = dict(vault_pass='secret')
    loader = DataLoader()
    variable_manager = VariableManager()
    inventory = Inventory(loader=loader, variable_manager=variable_manager,
        host_list=os.path.join(sysconf_dir, 'ansible', 'hosts'))
    variable_manager.set_inventory(inventory)
    playbook = Playbook.load(playbook_path,
        variable_manager=variable_manager, loader=loader)
    tqm = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords)
        for play in playbook.get_plays():
            result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()
开发者ID:djaodjin,项目名称:drop,代码行数:38,代码来源:duploades.py

示例15: test_play_iterator

    def test_play_iterator(self):
        # import epdb; epdb.st()
        fake_loader = DictDataLoader({
            "test_play.yml": """
            - hosts: all
              gather_facts: false
              roles:
              - test_role
              pre_tasks:
              - debug: msg="this is a pre_task"
              tasks:
              - debug: msg="this is a regular task"
              - block:
                - debug: msg="this is a block task"
                - block:
                  - debug: msg="this is a sub-block in a block"
                rescue:
                - debug: msg="this is a rescue task"
                - block:
                  - debug: msg="this is a sub-block in a rescue"
                always:
                - debug: msg="this is an always task"
                - block:
                  - debug: msg="this is a sub-block in an always"
              post_tasks:
              - debug: msg="this is a post_task"
            """,
            '/etc/ansible/roles/test_role/tasks/main.yml': """
            - name: role task
              debug: msg="this is a role task"
            - block:
              - name: role block task
                debug: msg="inside block in role"
              always:
              - name: role always task
                debug: msg="always task in block in role"
            - include: foo.yml
            - name: role task after include
              debug: msg="after include in role"
            - block:
              - name: starting role nested block 1
                debug:
              - block:
                - name: role nested block 1 task 1
                  debug:
                - name: role nested block 1 task 2
                  debug:
                - name: role nested block 1 task 3
                  debug:
              - name: end of role nested block 1
                debug:
              - name: starting role nested block 2
                debug:
              - block:
                - name: role nested block 2 task 1
                  debug:
                - name: role nested block 2 task 2
                  debug:
                - name: role nested block 2 task 3
                  debug:
              - name: end of role nested block 2
                debug:
            """,
            '/etc/ansible/roles/test_role/tasks/foo.yml': """
            - name: role included task
              debug: msg="this is task in an include from a role"
            """
        })

        mock_var_manager = MagicMock()
        mock_var_manager._fact_cache = dict()
        mock_var_manager.get_vars.return_value = dict()

        p = Playbook.load('test_play.yml', loader=fake_loader, variable_manager=mock_var_manager)

        hosts = []
        for i in range(0, 10):
            host = MagicMock()
            host.name = host.get_name.return_value = 'host%02d' % i
            hosts.append(host)

        mock_var_manager._fact_cache['host00'] = dict()

        inventory = MagicMock()
        inventory.get_hosts.return_value = hosts
        inventory.filter_hosts.return_value = hosts

        play_context = PlayContext(play=p._entries[0])

        itr = PlayIterator(
            inventory=inventory,
            play=p._entries[0],
            play_context=play_context,
            variable_manager=mock_var_manager,
            all_vars=dict(),
        )

        # pre task
        (host_state, task) = itr.get_next_task_for_host(hosts[0])
        self.assertIsNotNone(task)
#.........这里部分代码省略.........
开发者ID:awiddersheim,项目名称:ansible,代码行数:101,代码来源:test_play_iterator.py


注:本文中的ansible.playbook.Playbook类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。