directly to ansible-playbook.
vars: A dict of vars to be passed to ansible-playbook via the
--extra-vars flag
+ cleanup: If present, the given or generated playbook will be run
+ again during teardown with a 'cleanup' var set to True.
+ This will allow the playbook to clean up after itself,
+ if the playbook supports this feature.
Examples:
return args
def teardown(self):
+ self._cleanup()
if self.generated_inventory:
os.remove(self.inventory)
if self.generated_playbook:
os.remove(self.playbook_file.name)
super(Ansible, self).teardown()
+ def _cleanup(self):
+ """
+ If the ``cleanup`` key exists in config the same playbook will be
+ run again during the teardown step with the var ``cleanup`` given with
+ a value of ``True``. If supported, this will allow the playbook to
+ cleanup after itself during teardown.
+ """
+ if self.config.get("cleanup"):
+ log.info("Running ansible cleanup...")
+ extra = dict(cleanup=True)
+ if self.config.get('vars'):
+ self.config.get('vars').update(extra)
+ else:
+ self.config['vars'] = extra
+ self.execute_playbook()
+ else:
+ log.info("Skipping ansible cleanup...")
+
class CephLab(Ansible):
__doc__ = """
task.teardown()
assert m_remove.called_once_with('fake')
+ def test_teardown_cleanup_with_vars(self):
+ task_config = dict(
+ playbook=[],
+ cleanup=True,
+ vars=dict(yum_repos="testing"),
+ )
+ task = Ansible(self.ctx, task_config)
+ task.inventory = "fake"
+ task.generated_playbook = True
+ task.playbook_file = Mock()
+ task.playbook_file.name = 'fake'
+ with patch.object(Ansible, 'execute_playbook') as m_execute:
+ with patch.object(ansible.os, 'remove'):
+ task.teardown()
+ task._build_args()
+ assert m_execute.called
+ assert 'cleanup' in task.config['vars']
+ assert 'yum_repos' in task.config['vars']
+
+ def test_teardown_cleanup_with_no_vars(self):
+ task_config = dict(
+ playbook=[],
+ cleanup=True,
+ )
+ task = Ansible(self.ctx, task_config)
+ task.inventory = "fake"
+ task.generated_playbook = True
+ task.playbook_file = Mock()
+ task.playbook_file.name = 'fake'
+ with patch.object(Ansible, 'execute_playbook') as m_execute:
+ with patch.object(ansible.os, 'remove'):
+ task.teardown()
+ task._build_args()
+ assert m_execute.called
+ assert 'cleanup' in task.config['vars']
+
class TestCephLabTask(TestTask):
def setup(self):