class AnsibleHelper(object):

    Options = namedtuple('Options',
                         ['connection', 'module_path', 'forks', 'become_user',
                          'become', 'become_method', 'check', 'extra_vars',
                          'listhosts', 'listtasks', 'listtags', 'syntax',
                         ])

    def __init__(self, inventory_file):
        self.variable_manager = vm = VariableManager()
        self.loader = DataLoader()
        self.inventory = Inventory(loader=self.loader,
                                   variable_manager=vm,
                                   host_list=inventory_file)
        vm.set_inventory(self.inventory)
        self.options_args = dict(connection='smart',
                                 module_path=None,
                                 forks=100,
                                 become=None,
                                 become_method=None,
                                 become_user=None,
                                 check=False,
                                 listhosts=False,
                                 listtasks=False,
                                 listtags=False,
                                 syntax=False,
                                 extra_vars={})
        self._run_vars = None

    def run_playbook(self, playbook):
        options = self.Options(**self.options_args)
        vm = self.variable_manager
        vm.extra_vars = load_extra_vars(loader=self.loader, options=options)
        pbe = PlaybookExecutor(
            playbooks=[playbook],
            inventory=self.inventory,
            variable_manager=vm,
            loader=self.loader,
            options=options,
            passwords=dict(conn_pass='******'),
        )
        res = pbe.run()
        self._run_vars = vm.get_vars(self.loader)
        return res

    def get_vars(self, *args, **kwargs):
        self.inventory.get_vars(*args, **kwargs)

    def get_hosts(self, *args, **kwargs):
        self.inventory.ger_hosts(*args, **kwargs)

    def get_groups(self, *args, **kwargs):
        self.inventory.get_groups(*args, **kwargs)

    @property
    def run_vars(self):
        return self._run_vars
Example #2
0
def inventory_html():
    loader = DataLoader()
    variable_manager = VariableManager()
    sa_inv = Inventory(loader=loader,
                       variable_manager=variable_manager,
                       host_list='%s/hosts' % app.config.get('ANSIBLE_PATH'))
    # print sa_inv.serialize()
    if request.args.get('group'):
        hosts = sa_inv.get_hosts(request.args.get('group'))
        group_vars = sa_inv.get_group_variables(
            groupname=request.args.get('group'))
    else:
        hosts = sa_inv.get_hosts()
        group_vars = None

    if request.args.get('host'):
        host_vars = sa_inv.get_host_vars(
            host=sa_inv.get_host(request.args.get('host')))
    else:
        host_vars = None

    return render_template('inventory.html',
                           group=request.args.get('group'),
                           host=request.args.get('host'),
                           groups=sa_inv.get_groups(),
                           hosts=hosts,
                           group_vars=group_vars,
                           host_vars=host_vars)
Example #3
0
class Inventory(object):
    def __init__(self, inventory_path):
        """
        The Inventory class provides methods to extract information from the
        specified ansible inventory file.

        :param inventory_path: The path to the inventory file
        :type inventory_path: String
        """
        from ansible.inventory import Inventory as AnsibleInventory
        from ansible.parsing.dataloader import DataLoader
        from ansible.vars import VariableManager

        self._inventory = AnsibleInventory(loader=DataLoader(),
                                           variable_manager=VariableManager(),
                                           host_list=inventory_path)

    def get_ansible_inventory(self):
        return self._inventory

    def get_hosts(self, pattern='all'):
        return self._inventory.get_hosts(pattern)

    def get_groups(self):
        return self._inventory.get_groups()
class InventoryWrapper:
    def __init__(self, host_list):
        self.loader = DataLoader()
        # this code is a bit ugly, because Ansible 2.4 switched the order
        # of the object and the pattern (InventoryManager is a argument to
        # VariableManager, and vice-versa on version <2.3)
        if ANSIBLE_24:
            self.im = InventoryManager(loader=self.loader,
                                       sources=[host_list, ])
            self.vm = VariableManager(loader=self.loader, inventory=self.im)
        else:
            self.vm = VariableManager()
            self.im = Inventory(self.loader, self.vm, host_list)

    def get_loader(self):
        return self.loader

    def get_variable_manager(self):
        return VariableManagerWrapper(self.vm)

    def get_groups(self):
        if ANSIBLE_24:
            return self.im.groups
        return self.im.get_groups()

    def get_hosts(self, group):
        if ANSIBLE_24:
            return self.im.get_hosts(pattern=group)
        return self.im.get_hosts(group)

    def get_host(self, host):
        return self.im.get_host(host)

    def refresh_inventory(self):
        return self.im.refresh_inventory()
Example #5
0
 def render_GET(self, request):
     i = Inventory()
     inv = []
     try:
         hosts = i.get_hosts()
         groups = i.get_groups()
     except:
         return self.error_page(request)
     inv.extend([{"name": x.name, "type":"host"} for x in sorted(hosts)])
     inv.extend([{"name": x.name, "type":"group"} for x in sorted(groups)])
     return jsonify(request, inv)
Example #6
0
def notifyCanary():

    inven = Inventory(host_list=path["hosts"]);
    groups = inven.get_groups();
    for group in groups:
        # print group.get_variables()
        if group.name == "canary":
            hosts = inven.get_hosts(group.name)
            for host in hosts:
                host_ip = host.get_variables()['ansible_ssh_host']
                url = 'http://'+proxy_server+':'+proxy_port+"/canary"
                payload = {'host': host_ip}
                headers = {'content-type': 'application/json'}
                response = requests.post(url, data=payload, headers=headers)
Example #7
0
def extract_list_hosts_git(revision, path):
    """ Extract the hosts list from git, after deduplciating it
        and resolving variables """
    result = []
    if revision == '0000000000000000000000000000000000000000':
        return result
    try:
        host_content = subprocess.check_output(
            ['git', 'show', '%s:hosts' % revision], cwd=path)
    # usually, this is done when we can't check the list of hosts
    except subprocess.CalledProcessError:
        return result

    # beware, not portable on windows
    tmp_dir = tempfile.mkdtemp()
    tmp_file = tempfile.NamedTemporaryFile('w+', dir=tmp_dir)
    tmp_file.write(host_content)
    tmp_file.flush()
    os.fsync(tmp_file.fileno())

    variable_manager = VariableManager()
    loader = DataLoader()

    inventory = Inventory(loader=loader,
                          variable_manager=variable_manager,
                          host_list=tmp_file.name)
    for group in inventory.get_groups():
        for host in inventory.get_hosts(group):
            vars_host = variable_manager.get_vars(loader, host=host)
            result.append({
                'name':
                vars_host.get('ansible_ssh_host', host.name),
                'ssh_args':
                vars_host.get('ansible_ssh_common_args', ''),
                'connection':
                vars_host.get('ansible_connection', 'ssh')
            })

    # for some reason, there is some kind of global cache that need to be
    # cleaned
    inventory.refresh_inventory()
    shutil.rmtree(tmp_dir)

    return result
Example #8
0
class Inventory(object):
    def __init__(self, inventory_path):
        """
        The Inventory class provides methods to extract information from the
        specified ansible inventory file.

        :param inventory_path: The path to the inventory file
        :type inventory_path: String
        """
        from ansible.inventory import Inventory as AnsibleInventory

        self._inventory = AnsibleInventory(host_list=inventory_path)

    def get_ansible_inventory(self):
        return self._inventory

    def get_hosts(self, pattern='all'):
        return self._inventory.get_hosts(pattern)

    def get_groups(self):
        return self._inventory.get_groups()
Example #9
0
class AnsibleInventoryManager():
    """Class to handle the ansible inventory file.
    This class will allow you to add groups/hosts and remove them from the inventory file"""
    #FROM http://www.ansibleworks.com/docs/patterns.html
    ALLOWED_VARIABLES = ['ansible_ssh_host',
                         'ansible_ssh_port',
                         'ansible_ssh_user',
                         'ansible_ssh_pass',
                         'ansible_connection',
                         'ansible_ssh_private_key_file',
                         'ansible_syslog_facility',
                         'ansible_python_interpreter',
                         ]
    # NOTE:Works for anything such as ruby or perl and works just like
    # ansible_python_interpreter.
    # This replaces shebang of modules which will run on that host.

    def __init__(self, inventory_file="/etc/ansible/hosts"):
        self.__inventory_file = inventory_file
        self.__dirty = False
        if not os.path.exists(self.__inventory_file):
            raise AnsibleInventoryManagerFileNotFound("File: %s Not found or not accessible" % self.__inventory_file)
        self.__inventory = Inventory(inventory_file)

    def get_hosts(self):
        """return the list of hosts
        Returns a list host ips"""
        host_list = [host.name for host in self.__inventory.get_hosts()]
        return host_list

    def get_groups(self):
        """return the groups
        Returns a list of objects: ansible.inventory.group.Group"""
        return self.__inventory.get_groups()

    def get_groups_for_host(self,host):
        """return the groups list where the given host appears"""
        return self.__inventory.groups_for_host(host)

    def get_group(self, groupname):
        """Returns the given group"""
        return self.__inventory.get_group(groupname)

    def delete_host(self,host_ip,group=""):
        """Removes a host from a given group
        if group is empty, removes the host from all the groups
        """
        self.__dirty = True
        groups = []
        if group == "":
            groups = [group.name for group in self.get_groups_for_host(host_ip)]
        else:
            groups.append(group)
        for group in groups:
            grp = self.__inventory.get_group(group)
            new_host_list = [host for host in grp.get_hosts() if host.name != host_ip]
            grp.hosts = new_host_list

    def add_host(self, host_ip, add_to_root=True, group_list=[], var_list={}):
        """Add a host ip to the ansible host file
        This is a simple function to add hosts to
        add_to_root = Adds the host to the root group (unnamed)
        groups_list: List of groupnames where the host should appears.
        var_list: Variable list. see allowed_variables."""
        #root group in unnamed, but in the inventory object
        # is the 'ungrouped' group
        self.__dirty = True
        new_host = Host(host_ip)
        for key,value in var_list.iteritems():
            if self.is_allowed_variable(key):
                new_host.set_variable(key,value)
        if add_to_root:
            if 'ungrouped' not in group_list:
                group_list.append('ungrouped')

        #Check groups. The ansible inventory should contain each of the groups.
        for group in group_list:
            if not self.__inventory.get_group(group):
                new_group= Group(group)
                self.__inventory.add_group(new_group)
            grp = self.__inventory.get_group(group)
            host_names = [host.name for host in grp.get_hosts()]
            if new_host.name not in host_names:
                grp.add_host(new_host)

    def is_dirty(self):
        return self.__dirty

    def is_allowed_variable(self, variable):
        """Checks if the given variable is an allowed variable"""
        if variable in self.ALLOWED_VARIABLES:
            return True
        elif re.match("ansible_(.+)_interpreter", variable):
            return True
        return False

    def save_inventory(self, backup_file=""):
        """Saves the inventory file. If a backup_file is given,
        a backup will be done before re-write the file"""

        try:
            if backup_file != "":
                copyfile(self.__inventory_file, backup_file)
            data = ""
            for group in self.__inventory.get_groups():
                ingroup = False
                if group.name == "all":
                    continue
                elif group.name != "ungrouped":
                    data += "[%s]\n" % group.name
                    ingroup = True
                strvars = ""
                for host in group.get_hosts():
                    for key, value in host.get_variables().iteritems():
                        if key in AnsibleInventoryManager.ALLOWED_VARIABLES:
                            strvars += "%s=%s    " % (key, value)
                    if ingroup:
                        data += "\t%s\t%s\n" % (host.name, strvars)
                    else:
                        data += "%s\t%s\n" % (host.name, strvars)
            ansiblehostfile = open(self.__inventory_file, "w")
            ansiblehostfile.write(data)
            ansiblehostfile.close()
        except Exception, e:
            error("Error doing the %s backup: %s" % (self.__inventory_file, str(e)))
Example #10
0
#!/usr/bin/env python
#  Ansible: initialize needed objects

from ansible.inventory import Inventory
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager

variable_manager = VariableManager()
loader = DataLoader()

#  Ansible: Load inventory
inventory = Inventory(
    loader=loader,
    variable_manager=variable_manager,
    host_list='./ansible/inventory',  # Substitute your filename here
)

data = dict()
max = 0
for group in inventory.get_groups():
    for host in inventory.get_group(group).hosts:
        if not host.name in data:
            max =  max if max > len(host.name) else len(host.name)
            data[host.name]=host

print "%s   %s  %s" % ( 'host'.ljust(max) , 'private'.ljust(15),'private'.ljust(15) )
print '-' * 80
for host in data:
    print "%s   %s  %s" % ( data[host].name.ljust(max) , data[host].vars['ansible_ssh_host'].ljust(15),data[host].vars['private_ip'].ljust(15))

Example #11
0
def _main():
    global tab_size

    parser = argparse.ArgumentParser(
        description="Convert the Ansible inventory from INI to yML format", )
    parser.add_argument("-i",
                        dest="inventory_file",
                        help="Inform the Ansible Vault Password",
                        required=True)
    parser.add_argument(
        "-d",
        dest="dest_inventory_path",
        help=
        "Destination to save the inventory files (Default will be on $PWD)",
        required=False)
    parser.add_argument(
        "-g",
        dest="group",
        help="Filter by specific GROUP",
    )
    parser.add_argument(
        "-p",
        dest="vault_password",
        help="Inform the Ansible Vault Password or file with password",
    )
    parser.add_argument(
        "-t",
        dest="tab_size",
        type=int,
        help="Number of tab spaces",
        default=4,
    )

    args = parser.parse_args()
    tab_size = args.tab_size

    try:
        #  Ansible: initialize needed objects
        variable_manager = VariableManager()
        loader = DataLoader()

        if args.vault_password:
            if os.path.isfile(args.vault_password):
                with open(args.vault_password, "r") as f:
                    args.vault_password = f.readlines()[0].rstrip()
                    f.close()

            print("** Passing Vault Password: '******'") % (args.vault_password)
            loader.set_vault_password(args.vault_password)

        #  Ansible: Load inventory
        inventory = Inventory(
            loader=loader,
            variable_manager=variable_manager,
            host_list=args.inventory_file,
        )
    except Exception as e:
        print("** ERROR: Missing the '--vault-password'??\n %s\n") % str(e)
        sys.exit(-1)

    try:
        groups = {}

        if args.group:
            _group = inventory.get_group(args.group)
            if not _group:
                print("** ERROR: No such group '%s' in inventory file '%s', exiting.") % \
                    (args.group, args.inventory_file)
                sys.exit(-1)

            groups[args.group] = _group
        else:
            groups = inventory.get_groups()

        for group in groups:
            if group == "all":
                continue

            # start the file
            output = ""
            output += "###################################################################################################\n"
            output += ("# Converted from '%s[%s]'\n") % (args.inventory_file,
                                                         group)
            output += "###################################################################################################\n"
            output += ("%s:\n") % group

            group_ref = inventory.get_group(group)

            # children or hosts?
            if group_ref.child_groups:
                output += ident_as_usual(1, "children")
                for group_name in sorted(group_ref.child_groups):
                    output += ident_as_usual(2, group_name.get_name())
            else:
                group_ips = group_ref.hosts
                if not group_ips:
                    continue
                output += ""
                output += ident_as_usual(1, "hosts")
                for group_ip in sorted(group_ips):
                    if filter_vars(group_ip):
                        continue
                    output += ident_as_usual(2, group_ip.get_name())
                    _ip_host_vars = inventory.get_host_vars(
                        group_ip, return_results=True)  # group_vars/$group
                    _ip_vars = inventory.get_vars(
                        to_native(group_ip))  # host key1=var1
                    group_ip_vars = dict(_ip_host_vars.items() +
                                         _ip_vars.items())
                    if not group_ip_vars:
                        continue
                    for k, v in group_ip_vars.items():
                        if filter_vars(k):
                            continue
                        output += ident_as_the_key(3, k, v)

            # group_vars/$service
            output += "\n"
            output += ident_as_usual(1, "vars")
            group_vars = inventory.get_group_vars(group_ref,
                                                  return_results=True)
            if group_vars:
                for k, v in sorted(group_vars.items()):
                    if filter_vars(k):
                        continue
                    output += ident_as_the_key(2, k, v)
            output += "\n"
            output += ("# End for %s\n") % group

            if not args.dest_inventory_path:
                print(output)
            else:
                dest = args.dest_inventory_path + "/" + group + ".yml"
                print("Generating " + dest)

                try:
                    dest_dir = os.path.dirname(dest)
                    os.makedirs(dest_dir)
                except OSError as e:
                    if e.errno != errno.EEXIST:
                        raise

                f = file(dest, "w+")
                f.write(output)
                f.close()

        # Should save the 'group_vars/all' ?
        src = "group_vars/all"
        if args.dest_inventory_path and os.path.isfile(src):
            dest = args.dest_inventory_path + "/" + src
            print("Copying '%s' to '%s'") % (src, dest)
            try:
                dest_dir = os.path.dirname(dest)
                os.makedirs(dest_dir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
            shutil.copyfile(src, dest)

    except Exception as e:
        print("** ERROR: %s\n") % str(e)
        traceback.print_exc()