Example #1
0
 def testExtendAddParams(self):
     """
     Test that we can add new facts
     """
     fact_dirs = ['f_extend/out_setup', 'f_extend/extend']
     ansible = ansiblecmdb.Ansible(fact_dirs)
     software = ansible.hosts['debian.dev.local']['software']
     self.assertIn('Apache2', software)
Example #2
0
 def testExtendOverrideParams(self):
     """
     Test that we can override a native fact
     """
     fact_dirs = ['f_extend/out_setup', 'f_extend/extend']
     ansible = ansiblecmdb.Ansible(fact_dirs)
     env_editor = ansible.hosts['debian.dev.local']['ansible_facts'][
         'ansible_env']['EDITOR']
     self.assertEqual(env_editor, 'nano')
Example #3
0
class AnsibleHostsTestCase(unittest.TestCase):
    """
    Test that AnsibleHosts class required functionality
    """
    fact_dirs = ['f_ansiblehosts/out']
    inventories = ['f_ansiblehosts/hosts']
    ansible = ansiblecmdb.Ansible(fact_dirs, inventories)

    def testAnsibleHostsLen(self):
        self.assertEqual(len(self.ansible.hosts), 10)

    def testAnsibleHostsGetItem(self):
        self.assertIn('web01.local', self.ansible.hosts)
        self.assertTrue(self.ansible.hosts['web01.local'])
        self.assertTrue(self.ansible.hosts.get('web01.local'))
        self.assertFalse(self.ansible.hosts.get('nonexistent'))

    def testAnsibleHostsIterable(self):
        try:
            iterator = iter(self.ansible.hosts.items())
        except TypeError:
            self.fail('AnsibleHosts object is not iterable')

    def testAnsibleHostsSetItem(self):
        self.ansible.hosts['newhost'] = dict(name='newhost', hostvars={})
        self.assertIn('newhost', self.ansible.hosts)

    def testAnsibleHostsUpdate(self):
        update_value = dict({'hostvars': {'department': 'finance'}})
        self.ansible.hosts.update_host('web01.local', update_value)
        self.assertEqual(
            self.ansible.hosts['web01.local']['hostvars']['department'],
            'finance')

        # update dictionary method will reset the value
        update_item = dict(
            {'web01.local': {
                'hostvars': {
                    'location': 'us-east'
                }
            }})
        self.ansible.hosts.update(update_item)
        self.assertEqual(
            self.ansible.hosts['web01.local']['hostvars']['location'],
            'us-east')
        self.assertNotIn('department',
                         self.ansible.hosts['web01.local']['hostvars'])

        # update_host method will only update individual tree leaves
        update_value = dict({'hostvars': {'department': 'finance'}})
        self.ansible.hosts.update_host('web01.local', update_value)
        self.assertEqual(
            self.ansible.hosts['web01.local']['hostvars']['department'],
            'finance')
        self.assertEqual(
            self.ansible.hosts['web01.local']['hostvars']['location'],
            'us-east')
Example #4
0
 def testExpandHostDef(self):
     """
     Verify that host ranges are properly expanded. E.g. db[01-03].local ->
     db01.local, db02.local, db03.local.
     """
     fact_dirs = ['f_hostparse/out']
     inventories = ['f_hostparse/hosts']
     ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
     self.assertIn('web02.dev.local', ansible.hosts)
     self.assertIn('fe03.dev02.local', ansible.hosts)
Example #5
0
 def testChildGroupVars(self):
     """
     Test that all vars applied against a child group are set on the hosts.
     """
     fact_dirs = ['f_hostparse/out']
     inventories = ['f_hostparse/hosts']
     ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
     host_vars = ansible.hosts['db.dev.local']['hostvars']
     self.assertEqual(host_vars['function'], 'db')
     self.assertEqual(host_vars['dtap'], 'dev')
Example #6
0
 def testFactCache(self):
     fact_dirs = ['f_factcache/out']
     inventories = ['f_factcache/hosts']
     ansible = ansiblecmdb.Ansible(fact_dirs, inventories, fact_cache=True)
     host_vars = ansible.hosts['debian.dev.local']['hostvars']
     groups = ansible.hosts['debian.dev.local']['groups']
     ansible_facts = ansible.hosts['debian.dev.local']['ansible_facts']
     self.assertIn('dev', groups)
     self.assertEqual(host_vars['dtap'], 'dev')
     self.assertIn('ansible_env', ansible_facts)
Example #7
0
 def testChildGroupHosts(self):
     """
     Test that children groups contain all hosts they should.
     """
     fact_dirs = ['f_hostparse/out']
     inventories = ['f_hostparse/hosts']
     ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
     groups = ansible.hosts['db.dev.local']['groups']
     self.assertIn('db', groups)
     self.assertIn('dev', groups)
     self.assertIn('dev_local', groups)
Example #8
0
 def testHostsDir(self):
     """
     Verify that we can specify a directory as the hosts inventory file and
     that all files are parsed.
     """
     fact_dirs = ['f_inventory/out']
     inventories = ['f_inventory/hostsdir']
     ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
     host_vars = ansible.hosts['db.dev.local']['hostvars']
     groups = ansible.hosts['db.dev.local']['groups']
     self.assertEqual(host_vars['function'], 'db')
     self.assertIn('db', groups)
Example #9
0
 def testMixedDir(self):
     """
     Verify that a mixed dir of hosts files and dynamic inventory scripts is
     parsed correctly.
     """
     fact_dirs = ['f_inventory/out']
     inventory = 'f_inventory/mixeddir'
     ansible = ansiblecmdb.Ansible(fact_dirs, inventory)
     # results from dynamic inventory
     self.assertIn("host4.example.com", ansible.hosts)
     self.assertIn("moocow.example.com", ansible.hosts)
     # results from normal hosts file.
     self.assertIn("web03.dev.local", ansible.hosts)
Example #10
0
 def testDynInv(self):
     """
     Verify that we can specify a path to a dynamic inventory as the
     inventory file, and it will be executed, it's output parsed and added
     as available hosts.
     """
     fact_dirs = ['f_inventory/out']  # Reuse f_hostparse
     inventories = ['f_inventory/dyninv.py']
     ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
     self.assertIn('host5.example.com', ansible.hosts)
     host_vars = ansible.hosts['host5.example.com']['hostvars']
     groups = ansible.hosts['host5.example.com']['groups']
     self.assertEqual(host_vars['b'], False)
     self.assertIn("atlanta", groups)
Example #11
0
    }
    params.update(parse_user_params(options.params))
    if options.columns is not None:
        params['columns'] = options.columns.split(',')
    if options.exclude_columns is not None:
        params['exclude_columns'] = options.exclude_columns.split(',')

    # Log some debug information
    log.debug('data_dir = {0}'.format(data_dir))
    log.debug('tpl_dir = {0}'.format(tpl_dir))
    log.debug('static_dir = {0}'.format(static_dir))
    log.debug('inventory files = {0}'.format(hosts_files))
    log.debug('template params = {0}'.format(params))

    ansible = ansiblecmdb.Ansible(args,
                                  hosts_files,
                                  options.fact_cache,
                                  debug=options.debug)

    # Render a template with the gathered host info
    renderer = render.Render(options.template, ['.', tpl_dir])
    if renderer.tpl_file is None:
        sys.stderr.write("Template '{0}' not found at any of \n  {1}\n".format(
            options.template, "\n  ".join(renderer.tpl_possibilities)))
        sys.exit(1)

    # Make sure we always output in UTF-8, regardless of the user's locale /
    # terminal encoding. This is different in Python 2 and 3.
    try:
        output = renderer.render(ansible.hosts, params)
        if output:
            if sys.version_info[0] == 3: