def v2_playbook_on_start(self, playbook): path = os.path.abspath(playbook._file_name) # Potentially sanitize some user-specified keys for parameter in app.config['ARA_IGNORE_PARAMETERS']: if parameter in cli_options: msg = "Not saved by ARA as configured by ARA_IGNORE_PARAMETERS" cli_options[parameter] = msg log.debug('Starting playbook %s', path) self.playbook = models.Playbook( ansible_version=ansible_version, path=path, options=cli_options ) self.playbook.start() db.session.add(self.playbook) db.session.commit() file_ = self.get_or_create_file(path) file_.is_playbook = True # Cache the playbook data in memory for ara_record/ara_read current_app._cache['playbook'] = self.playbook.id
def v2_playbook_on_start(self, playbook): path = os.path.abspath(playbook._file_name) if self._options is not None: options = self._options.__dict__.copy() else: options = {} # Potentially sanitize some user-specified keys for parameter in app.config['ARA_IGNORE_PARAMETERS']: if parameter in options: msg = "Parameter not saved by ARA due to configuration" options[parameter] = msg LOG.debug('starting playbook %s', path) self.playbook = models.Playbook(ansible_version=ansible_version, path=path, options=options) self.playbook.start() db.session.add(self.playbook) file_ = self.get_or_create_file(path) file_.is_playbook = True # We need to persist the playbook id so it can be used by the modules data = {'playbook': {'id': self.playbook.id}} tmpfile = os.path.join(app.config['ARA_TMP_DIR'], 'ara.json') with open(tmpfile, 'w') as file: file.write(jsonutils.dumps(data))
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 v2_playbook_on_start(self, playbook): playbook_path = playbook._file_name LOG.debug('starting playbook %s', playbook_path) self.playbook = models.Playbook(path=playbook_path) self.playbook.start() db.session.add(self.playbook)
def v2_playbook_on_start(self, playbook): path = os.path.abspath(playbook._file_name) LOG.debug('starting playbook %s', path) self.playbook = models.Playbook(path=path) self.playbook.start() db.session.add(self.playbook) file_ = self.get_or_create_file(path) file_.is_playbook = True
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 v2_playbook_on_start(self, playbook): path = os.path.abspath(playbook._file_name) LOG.debug('starting playbook %s', path) self.playbook = models.Playbook(ansible_version=ansible_version, path=path) self.playbook.start() db.session.add(self.playbook) file_ = self.get_or_create_file(path) file_.is_playbook = True # We need to persist the playbook id so it can be used by the modules data = {'playbook': {'id': self.playbook.id}} tmpfile = os.path.join(app.config['ARA_TMP_DIR'], 'ara.json') with open(tmpfile, 'w') as file: file.write(json.dumps(data))
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
def model(self): return m.Playbook(ansible_version=ansible_version, complete=self.complete, path=self.path)