def v2_playbook_on_task_start(self, task, is_conditional, is_handler=False): self.close_task() log.debug('Starting task %s (action %s)', task.name, task.action) pathspec = task.get_path() if pathspec: path, lineno = pathspec.split(':', 1) lineno = int(lineno) file_ = self.get_or_create_file(path) else: path = self.playbook.path lineno = 1 file_ = self.get_or_create_file(self.playbook.path) self.task = models.Task( name=task.get_name(), sortkey=next(self.task_counter), action=task.action, play=self.play, playbook=self.playbook, tags=jsonutils.dumps(task._attributes['tags']), file=file_, lineno=lineno, is_handler=is_handler) self.task.start() db.session.add(self.task) db.session.commit()
def v2_playbook_on_task_start(self, task, is_conditional, is_handler=False): self.close_task() LOG.debug('starting task %s (action %s)', task.name, task.action) pathspec = task.get_path() if pathspec: path, lineno = pathspec.split(':', 1) lineno = int(lineno) else: path = None lineno = None self.task = models.Task( name=task.name, sortkey=next(self.task_counter), action=task.action, play=self.play, playbook=self.playbook, path=path, lineno=lineno, is_handler=is_handler) self.task.start() db.session.add(self.task)
def ansible_run(complete=True, gather_facts=True): '''Simulate a simple Ansible run by creating the expected database objects. This roughly approximates the following playbook: - hosts: host-<int> gather_facts: true tasks: - test-action: Where `<int>` is a random integer generated each time this function is called. Set the `complete` parameter to `False` to simulate an aborted Ansible run. Set the `gathered_facts` parameter to `False` to simulate a run with no facts gathered. ''' playbook = m.Playbook(path='testing.yml') play = m.Play(playbook=playbook, name='test play') task = m.Task(play=play, playbook=playbook, action='test-action') host = m.Host(name='host-%04d' % random.randint(0, 9999)) host.playbooks.append(playbook) result = m.TaskResult(task=task, status='ok', host=host, result='this is a test') ctx = dict(playbook=playbook, play=play, task=task, host=host, result=result) if gather_facts: facts = m.HostFacts(host=host, values='{"fact": "value"}') ctx['facts'] = facts for obj in ctx.values(): if hasattr(obj, 'start'): obj.start() db.session.add(obj) db.session.commit() if complete: stats = m.Stats(playbook=playbook, host=host) ctx['stats'] = stats db.session.add(stats) ctx['playbook'].complete = True for obj in ctx.values(): if hasattr(obj, 'stop'): obj.stop() db.session.commit() return ctx
def model(self): return m.Task(action=self.action, lineno=self.lineno, name=self.name, playbook=self.playbook, play=self.play, file=self.file, file_id=self.file_id)
def model(self): return m.Task(action=self.action, tags=json.dumps(self.tags), lineno=self.lineno, name=self.name, playbook=self.playbook, play=self.play, file=self.file, file_id=self.file_id)
def setUp(self): super(TestModels, self).setUp() self.playbook = m.Playbook(path='testing.yml') self.play = m.Play( name='test play', playbook=self.playbook, ) self.task = m.Task( name='test task', play=self.play, playbook=self.playbook, ) self.data = m.Data(playbook=self.playbook, key='test key', value='test value') self.host = m.Host( name='localhost', playbook=self.playbook, ) self.host_facts = m.HostFacts(host=self.host, values=json.dumps('{"fact": "value"}')) self.task_result = m.TaskResult( task=self.task, status='ok', host=self.host, ) self.stats = m.Stats( playbook=self.playbook, host=self.host, changed=0, failed=0, skipped=0, unreachable=0, ok=0, ) for obj in [ self.playbook, self.play, self.task, self.data, self.host, self.task_result, self.stats ]: m.db.session.add(obj) m.db.session.commit()
def ansible_run(self, complete=True): '''Simulate a simple Ansible run by creating the expected database objects. This roughly approximates the following playbook: - hosts: localhost tasks: - test-action: Set the `complete` parameter to `False` to simulate an aborted Ansible run. ''' playbook = m.Playbook(path='testing.yml') play = m.Play(playbook=playbook, name='test play') task = m.Task(play=play, playbook=playbook, action='test-action') host = m.Host(name='localhost') host.playbooks.append(playbook) result = m.TaskResult(task=task, status='ok', host=host, result='this is a test') self.ctx = dict(playbook=playbook, play=play, task=task, host=host, result=result) for obj in self.ctx.values(): if hasattr(obj, 'start'): obj.start() db.session.add(obj) db.session.commit() if complete: stats = m.Stats(playbook=playbook, host=host) self.ctx['stats'] = stats db.session.add(stats) for obj in self.ctx.values(): if hasattr(obj, 'stop'): obj.stop() db.session.commit()
def setUp(self): m.db.create_all() self.playbook = m.Playbook(path='testing.yml') self.play = m.Play( name='test play', playbook=self.playbook, ) self.task = m.Task( name='test task', play=self.play, playbook=self.playbook, ) self.host = m.Host(name='localhost', ) self.task_result = m.TaskResult( task=self.task, status='ok', host=self.host, ) self.stats = m.Stats( playbook=self.playbook, host=self.host, changed=0, failed=0, skipped=0, unreachable=0, ok=0, ) for obj in [ self.playbook, self.play, self.task, self.host, self.task_result, self.stats ]: m.db.session.add(obj) m.db.session.commit()
def ansible_run(complete=True, gather_facts=True, ara_record=False): '''Simulate a simple Ansible run by creating the expected database objects. This roughly approximates the following playbook: - hosts: host-<int> gather_facts: true tasks: - test-action: when: not ara_record - ara_record: key: 'test key' value: 'test value' when: ara_record Where `<int>` is a random integer generated each time this function is called. Set the `complete` parameter to `False` to simulate an aborted Ansible run. Set the `gathered_facts` parameter to `False` to simulate a run with no facts gathered. Set the `ara_record` parameter to `True` to simulate a run with an ara_record task. ''' playbook = m.Playbook(path='testing.yml') play = m.Play(playbook=playbook, name='test play') host = m.Host(name='host-%04d' % random.randint(0, 9999), playbook=playbook) if ara_record: task = m.Task(play=play, playbook=playbook, action='ara_record') msg = 'Data recorded in ARA for this playbook.' else: task = m.Task(play=play, playbook=playbook, action='test-action') msg = 'This is a test' result = m.TaskResult(task=task, status='ok', host=host, result=msg) ctx = dict(playbook=playbook, play=play, task=task, host=host, result=result) if gather_facts: facts = m.HostFacts(host=host, values='{"fact": "value"}') ctx['facts'] = facts if ara_record: data = m.Data(playbook=playbook, key='test key', value='test value') ctx['data'] = data for obj in ctx.values(): if hasattr(obj, 'start'): obj.start() db.session.add(obj) db.session.commit() if complete: stats = m.Stats(playbook=playbook, host=host) ctx['stats'] = stats db.session.add(stats) ctx['playbook'].complete = True for obj in ctx.values(): if hasattr(obj, 'stop'): obj.stop() db.session.commit() return ctx