コード例 #1
0
ファイル: ansible_api.py プロジェクト: pygray/devops
    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """
            add hosts to a group
        """
        self.inventory.add_group(groupname)
        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")
            my_host = Host(name=hostname, port=hostport)
            self.variable_manager.set_host_variable(host=my_host, varname='ansible_ssh_host', value=hostip)
            self.variable_manager.set_host_variable(host=my_host, varname='ansible_ssh_port', value=hostport)

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

            # add to group
            self.inventory.add_host(host=hostname, group=groupname, port=hostport)
コード例 #2
0
    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """add new hosts to a group"""
        self.inventory.add_group(groupname)
        my_group = Group(name=groupname)

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

        # 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)
            self.variable_manager.set_host_variable(host=my_host, varname='ansible_ssh_host', value=hostip)
            self.variable_manager.set_host_variable(host=my_host, varname='ansible_ssh_pass', value=password)
            self.variable_manager.set_host_variable(host=my_host, varname='ansible_ssh_port', value=hostport)
            self.variable_manager.set_host_variable(host=my_host, varname='ansible_ssh_user', value=username)
            self.variable_manager.set_host_variable(host=my_host, varname='ansible_ssh_private_key_file', value=ssh_key)
            # my_host.set_variable('ansible_ssh_pass', password)
            # my_host.set_variable('ansible_ssh_private_key_file', ssh_key)

            # set other variables
            for key in host:
                if key not in ["hostname", "port", "username", "password"]:
                    self.variable_manager.set_host_variable(host=my_host, varname=key, value=host[key])

            # add to group
            self.inventory.add_host(host=hostname, group=groupname, port=hostport)
コード例 #3
0
    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """
            add hosts to a group
        """

        self.inventory.add_group(groupname)
        my_group = Group(name=groupname)

        # if group variables exists, add them to group if dict
        # groupvars={'var1': 'ansible', 'var2': 'saltstack'}
        if groupvars:
            for key, value in groupvars.iteritems():
                my_group.set_variable(key, value)


# if list hosts=resource [{"hostname": "192.168.1.111"}], if dict host=[{'username': u'root', 'ip': '192.168.1.11',}]
# add hosts to group,all hosts is list
        for host in hosts:
            # set connection variables
            hostname = host.get("hostname")
            # 拿IP  没有IP就用hostname代替,没有IP一般是新增的
            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)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_host',
                                                    value=hostip)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_pass',
                                                    value=password)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_port',
                                                    value=hostport)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_user',
                                                    value=username)
            self.variable_manager.set_host_variable(
                host=my_host,
                varname='ansible_ssh_private_key_file',
                value=ssh_key)
            # 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"]:
                    self.variable_manager.set_host_variable(host=my_host,
                                                            varname=key,
                                                            value=value)

            # add to group

            self.inventory.add_host(host=hostname,
                                    group=groupname,
                                    port=hostport)
            ghost = Host(name="192.168.8.119")
コード例 #4
0
    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """
        将主机添加到对应主机组中
        """
        self.inventory.add_group(groupname)
        my_group = Group(name=groupname)

        # 判断是否有主机组变量
        if groupvars:
            for key, value in groupvars.items():
                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)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_host',
                                                    value=hostip)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_pass',
                                                    value=password)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_port',
                                                    value=hostport)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_user',
                                                    value=username)
            self.variable_manager.set_host_variable(
                host=my_host,
                varname='ansible_ssh_private_key_file',
                value=ssh_key)

            # 判断是否还有除hostname,port,username,password之外的变量
            for key, value in host.items():
                if key not in ["hostname", "port", "username", "password"]:
                    self.variable_manager.set_host_variable(host=my_host,
                                                            varname=key,
                                                            value=value)

            # 添加到指定主机组
            self.inventory.add_host(host=hostname,
                                    group=groupname,
                                    port=hostport)
コード例 #5
0
    def _addGroupHosts(self, hosts, groupname=None, groupvars=None):
        self.inventory.add_group(group=groupname)
        group = Group(groupname)
        if groupvars:
            for k, v in groupvars.items():
                group.set_variable(k, v)

        # hosts=[{'hostid':'123','hostname':'h1','hostip':'192.168.188.20'}
        for host in hosts:
            hostid = host.get('hostid')
            hostname = host.get('hostname')
            hostip = host.get('hostip')
            username = host.get('username')
            password = host.get('password')
            port = host.get('port', 22)
            sshkey = host.get('sshkey')
            if hostname:
                self.inventory.add_host(
                    host=hostname, group=groupname
                )  # by default, indentify by hostname and need
                hostobj = self.inventory.get_host(
                    hostname=hostname)  # add host= , get hostname=

                self.variable_manager.set_host_variable(
                    host=hostobj, varname='ansible_ssh_host', value=hostip)
                self.variable_manager.set_host_variable(
                    host=hostobj, varname='ansible_ssh_port', value=port)
                self.variable_manager.set_host_variable(
                    host=hostobj, varname='ansible_ssh_user', value=username)
                self.variable_manager.set_host_variable(
                    host=hostobj, varname='ansible_ssh_pass', value=password)
                self.variable_manager.set_host_variable(
                    host=hostobj,
                    varname='ansible_ssh_private_key_file',
                    value=sshkey)

                # TODO: other vars such as become-method-user-pass
                # hostobj.set_variable('ansible_ssh_port',port)
                for k, v in host.items():
                    if k not in [
                            'hostip', 'port', 'username', 'password', 'sshkey'
                    ]:
                        hostobj.set_variable(k, v)
                        self.variable_manager.set_host_variable(host=hostobj,
                                                                varname=k,
                                                                value=v)
            else:
                logging.warning('resource error:cant get hostname from | %s' %
                                self.resource)
コード例 #6
0
    def add_dynamic_group(self, hosts, group_name, group_vars=None):
        self.inventory.add_group(group_name)
        my_group = Group(name=group_name)

        if group_vars is not None:
            for k, v in group_vars.items():
                my_group.set_variable(k, v)

        for host in hosts:
            hostname = host.get("hostname")
            hostip = host.get("ip", hostname)
            hostport = host.get("port", 22)
            username = host.get("username")
            password = host.get("password")
            ssh_key = host.get("ssh_key", "~/.ssh/id_rsa")
            my_host = Host(name=hostname, port=hostport)

            self.variable_manager.set_host_variable(host=my_host,
                                                    varname="ansible_ssh_host",
                                                    value=hostip)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_pass',
                                                    value=password)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_port',
                                                    value=hostport)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_user',
                                                    value=username)
            self.variable_manager.set_host_variable(
                host=my_host,
                varname='ansible_ssh_private_key_file',
                value=ssh_key)

            for key, value in host.items():
                if key not in ["hostname", "port", "username", "password"]:
                    self.variable_manager.set_host_variable(host=my_host,
                                                            varname=key,
                                                            value=value)

            self.inventory.add_host(host=hostname,
                                    group=group_name,
                                    port=hostport)
コード例 #7
0
    def my_add_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.items():
                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)

        self.inventory.add_group(my_group)
コード例 #8
0
    def add_dynamic_hosts(self, hostip_list, groupname=None, groupvars=None):
        """
            add hosts to a group
        """
        #如果有传入组名,则添加组,并创建Group实例
        if groupname:
            self.inv_obj.add_group(groupname)
            my_group = Group(name=groupname)
        #如果有传入组变量,则将组变量设置给上面创建的Group实例
        if groupvars:
            for key, value in groupvars.iteritems():
                my_group.set_variable(key, value)

        # add hosts to group
        # 如果有传入组名,则遍历主机列表,添加主机并且设置组
        if groupname:
            for hostip in hostip_list:
                self.inv_obj.add_host(host=hostip, group=groupname)
        #如果没有传入组名,则遍历主机列表,只添加主机
        else:
            for hostip in hostip_list:
                self.inv_obj.add_host(host=hostip)
コード例 #9
0
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import os
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.inventory.host import Host, Group
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
hosts_file = os.path.join(BASE_DIR, "hosts")
loader = DataLoader()  # 读取文件
Inventory = InventoryManager(loader=loader, sources=[
    hosts_file,
])  # 导入主机配置文件
Inventory.add_group('test_group')
my_group = Group(name='test_group')
my_group.set_variable(key="zhang", value="neng")

my_host = Host(name='192.168.1.11', port=22)
print my_host.get_vars()