]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
Allow subclasses of task.Ansible to provide an inventory group name 569/head
authorAndrew Schoen <aschoen@redhat.com>
Mon, 13 Jul 2015 15:47:21 +0000 (10:47 -0500)
committerAndrew Schoen <aschoen@redhat.com>
Tue, 14 Jul 2015 16:46:03 +0000 (11:46 -0500)
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 <aschoen@redhat.com>
teuthology/task/ansible.py
teuthology/test/task/test_ansible.py

index d20a44472c02fb01831ac5d0d20f3883799b6434..405eadfb22b93a99d3b6a1903611207dbfdd380d 100644 (file)
@@ -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()
index a0be36bb881fdb582f0ef4c561b4765def8c995f..2cf8902d63a2b83963ad1824daf9576b144a8356 100644 (file)
@@ -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',
+        ]