def test_save_inventory(self): """ Test save process """ # Copy real data into temp_file temp_inventory_file = mktemp(prefix='test_inventory') temp_bkp_inventory_file = mktemp(prefix='test_bkp_inventory') try: copyfile(TEST_FILES_PATH + "hosts3", temp_inventory_file) ansible_file = AnsibleInventoryManager( inventory_file=temp_inventory_file) ansible_file.save_inventory(backup_file=temp_bkp_inventory_file) bk_ansible_file = AnsibleInventoryManager( inventory_file=temp_bkp_inventory_file) self.assertEqual(ansible_file.get_hosts(), bk_ansible_file.get_hosts()) self.assertEqual( [group.name for group in ansible_file.get_groups()], [group.name for group in bk_ansible_file.get_groups()]) ansible_file.add_host("host7") ansible_file.save_inventory(backup_file=temp_bkp_inventory_file) bk_ansible_file = AnsibleInventoryManager( inventory_file=temp_bkp_inventory_file) self.assertNotEqual(ansible_file.get_hosts(), bk_ansible_file.get_hosts()) self.assertEqual( [group.name for group in ansible_file.get_groups()], [group.name for group in bk_ansible_file.get_groups()]) finally: os.unlink(temp_inventory_file) os.unlink(temp_bkp_inventory_file)
def ansible_add_ip_to_inventory(system_ip): try: aim = AnsibleInventoryManager() aim.add_host(system_ip) aim.save_inventory() except Exception, msg: api_log.error(str(msg)) return False, 'Error adding ip to ansible inventory'
def ansible_add_ip_to_inventory(system_ip): try: from ansiblemethods.ansibleinventory import AnsibleInventoryManager aim = AnsibleInventoryManager() aim.add_host(system_ip) aim.save_inventory() except Exception, msg: api_log.error(str(msg)) return False, 'Error adding ip to ansible inventory'
def ansible_add_system(local_system_id, remote_system_ip, password): """ Add a new system. Create and set the crypto files and update the ansible inventory manager """ from ansiblemethods.ansibleinventory import AnsibleInventoryManager result = False response = None # sanity check if not os.path.isfile('/var/ossim/ssl/local/ssh_capubkey.pem'): response = "Cannot access public key file" return (result, response) success, message = ansible_remove_key_from_known_host_file( "127.0.0.1", remote_system_ip) if not success: return success, message evars = { "remote_system_ip": "%s" % remote_system_ip, "local_system_id": "%s" % local_system_id } response = ansible.run_playbook(playbook=PLAYBOOKS['SET_CRYPTO_FILES'], host_list=[remote_system_ip], extra_vars=evars, ans_remote_user="******", ans_remote_pass=password, use_sudo=True) if response[remote_system_ip]['unreachable'] == 0 and \ response[remote_system_ip]['failures'] == 0: result = True response = "System with IP %s added correctly" % (remote_system_ip) else: result = False api_log.error(str(response)) response = "Cannot add system with IP %s. " % (remote_system_ip) + \ "Please verify that the system is reachable " + \ "and the password is correct." # Add the system to the Ansible Inventory aim = AnsibleInventoryManager() aim.add_host(remote_system_ip) aim.save_inventory() return (result, response)
def ansible_add_system(local_system_id, remote_system_ip, password): """ Add a new system. Create and set the crypto files and update the ansible inventory manager """ from ansiblemethods.ansibleinventory import AnsibleInventoryManager result = False response = None # sanity check if not os.path.isfile('/var/ossim/ssl/local/ssh_capubkey.pem'): response = "Cannot access public key file" return (result, response) success, message = ansible_remove_key_from_known_host_file( "127.0.0.1", remote_system_ip) if not success: return success, message evars = {"remote_system_ip": "%s" % remote_system_ip, "local_system_id": "%s" % local_system_id} response = ansible.run_playbook(playbook=PLAYBOOKS['SET_CRYPTO_FILES'], host_list=[remote_system_ip], extra_vars=evars, ans_remote_user="******", ans_remote_pass=password, use_sudo=True) if response[remote_system_ip]['unreachable'] == 0 and \ response[remote_system_ip]['failures'] == 0: result = True response = "System with IP %s added correctly" % (remote_system_ip) else: result = False api_log.error(str(response)) response = "Cannot add system with IP %s. " % (remote_system_ip) + \ "Please verify that the system is reachable " + \ "and the password is correct." # Add the system to the Ansible Inventory aim = AnsibleInventoryManager() aim.add_host(remote_system_ip) aim.save_inventory() return (result, response)
def test_save_inventory(self): """ Test save process """ # Copy real data into temp_file temp_inventory_file = mktemp(prefix='test_inventory') temp_bkp_inventory_file = mktemp(prefix='test_bkp_inventory') try: copyfile(TEST_FILES_PATH + "hosts3", temp_inventory_file) ansible_file = AnsibleInventoryManager(inventory_file=temp_inventory_file) ansible_file.save_inventory(backup_file=temp_bkp_inventory_file) bk_ansible_file = AnsibleInventoryManager(inventory_file=temp_bkp_inventory_file) self.assertEqual(ansible_file.get_hosts(), bk_ansible_file.get_hosts()) self.assertEqual([group.name for group in ansible_file.get_groups()], [group.name for group in bk_ansible_file.get_groups()]) ansible_file.add_host("host7") ansible_file.save_inventory(backup_file=temp_bkp_inventory_file) bk_ansible_file = AnsibleInventoryManager(inventory_file=temp_bkp_inventory_file) self.assertNotEqual(ansible_file.get_hosts(), bk_ansible_file.get_hosts()) self.assertEqual([group.name for group in ansible_file.get_groups()], [group.name for group in bk_ansible_file.get_groups()]) finally: os.unlink(temp_inventory_file) os.unlink(temp_bkp_inventory_file)