From 8c5efbb254783a505c90c574826ef7c03594cfb8 Mon Sep 17 00:00:00 2001 From: Andrew Schoen Date: Mon, 13 Jul 2015 10:47:21 -0500 Subject: [PATCH] Allow subclasses of task.Ansible to provide an inventory group name Subclasses of task.Ansible can now provide an inventory group name to assign all nodes to when creating a dynamic inventory. This PR also makes ansible.CephLab use the group 'testnodes'. Without this, teuthology can not run the testnodes.yml playbook against it's locked nodes using a dynamic inventory as it's restricted to the testnodes group. Signed-off-by: Andrew Schoen --- teuthology/task/ansible.py | 13 ++++++++++++- teuthology/test/task/test_ansible.py | 28 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/teuthology/task/ansible.py b/teuthology/task/ansible.py index d20a44472..405eadfb2 100644 --- a/teuthology/task/ansible.py +++ b/teuthology/task/ansible.py @@ -94,6 +94,9 @@ class Ansible(Task): 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) @@ -177,7 +180,11 @@ class Ansible(Task): 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) @@ -288,11 +295,15 @@ class CephLab(Ansible): - 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() diff --git a/teuthology/test/task/test_ansible.py b/teuthology/test/task/test_ansible.py index a0be36bb8..2cf8902d6 100644 --- a/teuthology/test/task/test_ansible.py +++ b/teuthology/test/task/test_ansible.py @@ -208,7 +208,10 @@ class TestAnsibleTask(TestTask): 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 = [ @@ -435,3 +438,26 @@ class TestCephLabTask(TestTask): 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', + ] -- 2.47.3