key: value
"""
+ # set this in subclasses to provide a group to
+ # assign hosts to for dynamic inventory creation
+ inventory_group = None
def __init__(self, ctx, config):
super(Ansible, self).__init__(ctx, config)
hosts = self.cluster.remotes.keys()
hostnames = [remote.hostname for remote in hosts]
hostnames.sort()
- hosts_str = '\n'.join(hostnames + [''])
+ inventory = []
+ if self.inventory_group:
+ inventory.append('[{0}]'.format(self.inventory_group))
+ inventory.extend(hostnames + [''])
+ hosts_str = '\n'.join(inventory)
hosts_file = NamedTemporaryFile(prefix="teuth_ansible_hosts_",
delete=False)
hosts_file.write(hosts_str)
- ansible:
repo: {git_base}ceph-cm-ansible.git
playbook: cephlab.yml
+
+ If a dynamic inventory is used, all hosts will be assigned to the
+ group 'testnodes'.
""".format(git_base=teuth_config.ceph_git_base_url)
# Set the name so that Task knows to look up overrides for
# 'ansible.cephlab' instead of just 'cephlab'
name = 'ansible.cephlab'
+ inventory_group = 'testnodes'
def __init__(self, ctx, config):
config = config or dict()
assert task.generated_inventory is True
assert task.inventory == hosts_file_path
hosts_file_obj.seek(0)
- assert hosts_file_obj.readlines() == ['remote1\n', 'remote2\n']
+ assert hosts_file_obj.readlines() == [
+ 'remote1\n',
+ 'remote2\n',
+ ]
def test_generate_playbook(self):
playbook = [
m_file.return_value = fake_playbook_obj
task.get_playbook()
assert task.playbook_file.name == playbook
+
+
+ def test_generate_hosts_file(self):
+ self.task_config.update(dict(
+ playbook=[]
+ ))
+ task = self.klass(self.ctx, self.task_config)
+ hosts_file_path = '/my/hosts/file'
+ hosts_file_obj = StringIO()
+ hosts_file_obj.name = hosts_file_path
+ with patch.object(ansible, 'NamedTemporaryFile') as m_NTF:
+ m_NTF.return_value = hosts_file_obj
+ task.generate_hosts_file()
+ m_NTF.assert_called_once_with(prefix="teuth_ansible_hosts_",
+ delete=False)
+ assert task.generated_inventory is True
+ assert task.inventory == hosts_file_path
+ hosts_file_obj.seek(0)
+ assert hosts_file_obj.readlines() == [
+ '[testnodes]\n',
+ 'remote1\n',
+ 'remote2\n',
+ ]