示例#1
0
   def add_dynamic_group(self, hosts, groupname, groupvars=None):  
       """ 
           add hosts to a group 
       """  
       my_group = Group(name=groupname)  
 
       # if group variables exists, add them to group  
       if groupvars:  
           for key, value in groupvars.iteritems():  
               my_group.set_variable(key, value)  
 
       # add hosts to group  
       for host in hosts:  
           # set connection variables  
           hostname = host.get("hostname")  
           hostip = host.get('ip', hostname)  
           hostport = host.get("port")  
           username = host.get("username")  
           password = host.get("password")  
           ssh_key = host.get("ssh_key")  
           my_host = Host(name=hostname, port=hostport)  
           my_host.set_variable('ansible_ssh_host', hostip)  
           my_host.set_variable('ansible_ssh_port', hostport)  
           my_host.set_variable('ansible_ssh_user', username)  
           my_host.set_variable('ansible_ssh_pass', password)  
           my_host.set_variable('ansible_ssh_private_key_file', ssh_key)  
 
           # set other variables  
           for key, value in host.iteritems():  
               if key not in ["hostname", "port", "username", "password"]:  
                   my_host.set_variable(key, value)  
           # add to group  
           my_group.add_host(my_host)  
 
       self.inventory.add_group(my_group)  
    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """dynamic inventory group"""
        my_group = Group(name=groupname)
        if groupvars:
            for key, value in groupvars.items():
                my_group.set_variable(key, value)
        for host in hosts:
            # set connection variables
            hostname = host.get("hostname")
            hostip = host.get('ip', hostname)
            hostport = host.get("port")
            username = host.get("username")
            password = host.get("password")
            ssh_key = host.get("ssh_key")
            my_host = Host(name=hostname, port=hostport)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)
            my_host.set_variable('ansible_ssh_private_key_file', ssh_key)
            for key, value in host.items():
                if key not in ["hostname", "port", "username", "password"]:
                    my_host.set_variable(key, value)
            my_group.add_host(my_host)

        self.inventory.add_group(my_group)
示例#3
0
    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """
            add hosts to a group
        """
        my_group = Group(name=groupname)

        # if group variables exists, add them to group
        if groupvars:
            for key, value in groupvars.iteritems():
                my_group.set_variable(key, value)

                # add hosts to group
        for host in hosts:
            # set connection variables
            hostname = host.get("hostname")
            hostip = host.get('ip', hostname)
            hostport = host.get("port")
            username = host.get("username")
            password = host.get("password")
            ssh_key = host.get("ssh_key")
            my_host = Host(name=hostname, port=hostport)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)
            my_host.set_variable('ansible_ssh_private_key_file', ssh_key)
            # set other variables
            for key, value in host.items():
                if key not in ["hostname", "port", "username", "password"]:
                    my_host.set_variable(key, value)
                    # add to group
            my_group.add_host(my_host)
            # print("my_group--->", my_group.hosts)
        self.inventory.add_group(my_group)
示例#4
0
    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """
        add hosts to a group
        :param hosts:
        :param groupname:
        :param groupvars:
        :return:
        """
        my_group = Group(name=groupname)

        # if group variables exists, add them to group
        if groupvars:
            for key, value in groupvars.iteritems():
                my_group.set_variable(key, value)

        # add hosts to group
        for host in hosts:
            # set connection variables
            hostname = host.get("hostname")
            hostip = host.get('ip', hostname)
            hostport = host.get("port")
            username = host.get("username")
            password = host.get("password")
            if username == 'root':
                keyfile = "/root/.ssh/id_rsa"
            else:
                keyfile = "/home/{user}/.ssh/id_rsa".format(user=username)
            ssh_key = host.get("ssh_key", keyfile)
            my_host = Host(name=hostname, port=hostport)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)
            my_host.set_variable('ansible_ssh_private_key_file', ssh_key)

            # set other variables
            for key, value in host.iteritems():
                if key not in ["hostname", "port", "username", "password"]:
                    my_host.set_variable(key, value)

            # add to group
            my_group.add_host(my_host)
        self.inventory.add_group(my_group)
示例#5
0
    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """ 
            add hosts to a group 
        """
        my_group = Group(name=groupname)
        # if group variables exists, add them to group
        if groupvars:
            for key, value in groupvars.iteritems():
                my_group.set_variable(key, value)

        # add hosts to group
        for host in hosts:
            # set connection variables
            hostip = host.get("ip")
            #             hostname = host.get("ip",hostip)
            hostport = host.get("port")
            username = host.get("username")
            password = host.get("password")
            connection = host.get("connection", 'smart')
            sudo_pass = host.get("sudo_passwd")
            if username == 'root': ssh_key = "/root/.ssh/id_rsa"
            else: ssh_key = "/home/{user}/.ssh/id_rsa".format(user=username)
            if not os.path.exists(ssh_key): ssh_key = host.get("ssh_key")
            my_host = Host(name=hostip, port=hostport)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)
            my_host.set_variable('ansible_sudo_pass', sudo_pass)
            my_host.set_variable('ansible_ssh_private_key_file', ssh_key)
            my_host.set_variable('ansible_connection', connection)

            # set other variables
            for key, value in host.iteritems():
                if key not in ["ip", "port", "username", "password"]:
                    my_host.set_variable(key, value)
            # add to group
            my_group.add_host(my_host)
        try:
            self.add_group(my_group)
        except Exception as ex:
            logger.error(msg="ansible添加资产组失败: {ex}".format(ex=ex))
示例#6
0
    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """
        动态创建主机组
        :param hosts: 主机列表
        :param groupname: 组机组名
        :param groupvars: 组参数
        :return: 
        """
        my_group = Group(name=groupname)  # 创建主机组对象
        if groupvars:
            # 设置主机组参数
            for key, value in groupvars.items():
                my_group.set_variable(key, value)
        for host in hosts:
            # 设置链接参数
            hostname = host.get("hostname")
            hostip = host.get('ip', hostname)
            hostport = host.get("port")
            username = host.get("username")
            password = host.get("password")
            ssh_key = host.get("ssh_key")
            my_host = Host(name=hostname, port=hostport)  # 创建主机对象
            # 设置主机参数
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)
            my_host.set_variable('ansible_sudo_pass', password)
            my_host.set_variable('ansible_sudo', 'yes')
            my_host.set_variable('ansible_ssh_private_key_file', ssh_key)
            for key, value in host.items():
                if key not in ["hostname", "port", "username", "password"]:
                    my_host.set_variable(key, value)
            my_group.add_host(my_host)  # 将主机对象添加到主机组对象中

        self.inventory.add_group(my_group)  # 将主机组添加到主机配置清单中
示例#7
0
    def __init__(self, groups, playbook, private_key_file, display, become_pass={}, extraVars={}):
        self.extraVars = extraVars

        self.options = Options()
        self.options.private_key_file = private_key_file
        self.options.verbosity = display.verbosity
        self.options.connection = 'ssh'  # Need a connection type 'smart' or 'ssh'
        self.options.become = True
        self.options.become_method = 'sudo'
        self.options.become_user = '******'

        # Executor appears to have it's own
        # verbosity object/setting as well
        playbook_executor.verbosity = self.options.verbosity

        # Become Pass Needed if not logging in as user root
        passwords = {'become_pass':become_pass}

        # Gets data from YAML/JSON files
        self.loader = DataLoader()
        self.loader.set_vault_password(os.environ.get('VAULT_PASS', 'xiaoy_pandan'))

        # All the variables from all the various places
        self.variable_manager = VariableManager()
        self.variable_manager.extra_vars = self.extraVars

        # Set inventory, using most of above objects
        self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=[])
        self.variable_manager.set_inventory(self.inventory)

        # Set groups info
        for info in groups:
            group = Group(info['name'])
            self.inventory.add_group(group)

            hostInfos = info.get('hosts')
            for hostInfo in hostInfos:
                hostname = hostInfo.get('hostname')
                hostip = hostInfo.get('ip', hostname)
                hostport = hostInfo.get('port')
                username = hostInfo.get('username')
                password = hostInfo.get('password')
                ssh_key = hostInfo.get('ssh_key')
                host = Host(name=hostname, port=hostport)
                host.set_variable('ansible_ssh_host', hostip)
                host.set_variable('ansible_ssh_port', hostport)
                host.set_variable('ansible_ssh_user', username)
                host.set_variable('ansible_ssh_pass', password)
                host.set_variable('ansible_ssh_private_key_file', ssh_key)
                for key, value in hostInfo.iteritems():
                    if key not in ['hostname', 'port', 'username', 'password', 'ssh_key']:
                        host.set_variable(key, value)
                group.add_host(host)

            varInfos = info.get('vars')
            if varInfos:
                for key, value in varInfos.iteritems():
                    group.set_variable(key, value)

        # Playbook to run. Assumes it is
        # local to this python file
        pb_dir = os.path.dirname(__file__)
        playbook = '%s/%s' % (pb_dir, playbook)

        # Setup playbook executor, but don't run until run() called
        self.pbex = playbook_executor.PlaybookExecutor(
            playbooks=[playbook],
            inventory=self.inventory,
            variable_manager=self.variable_manager,
            loader=self.loader,
            options=self.options,
            passwords=passwords)
        self.display = display
        self.callback = CallbackModule(self.display)
        self.pbex._tqm._stdout_callback = self.callback