示例#1
0
文件: tasks.py 项目: qt-pay/devops-4
def task_test():
    host_data = [
        {
            'hostname': 'k8s1',
            'ip': '192.168.223.111',
            'port': 22,
            'username': '******',
            'password': '******',
            'groups': ['k8s'],
        },
        {
            'hostname': 'k8s2',
            'ip': '192.168.223.112',
            'port': 22,
            'username': '******',
            'password': '******',
            'groups': ['k8s'],
            'become': {
                'method': 'su',
                'user': '******',
                'pass': '******',
            },
        },
        {
            'hostname': 'k8s3',
            'ip': '192.168.223.113',
            'port': 22,
            'username': '******',
            'password': '******',
            'private_key': '/tmp/private_key',
            'become': {
                'method': 'sudo',
                'user': '******',
                'pass': None,
            },
            'groups': ['k8s', 'server'],
            'vars': {
                'love': 'yes',
                'test': 'no'
            },
        },
    ]
    private_key_file = './id_rsa'
    remote_user = '******'
    extra_vars = {'var': 'test'}
    inventory = BaseInventory(host_data)
    ansible_api = AnsibleAPI(
        private_key_file=private_key_file,
        extra_vars=extra_vars,
        remote_user=remote_user,
        dynamic_inventory=inventory,
        # forks=4,
    )
    playbook_yml = '/home/workspace/devops/util/hello.yml'
    ansible_api.run_playbook(playbook_yml=playbook_yml)
    # ansible_api.run_module(module_name='setup', module_args='', hosts='k8s')
    # ansible_api.run_module(module_name='shell', module_args='echo "1" >> /tmp/test.txt', hosts='k8s')
    ansible_api.get_result()
示例#2
0
文件: tasks.py 项目: qt-pay/devops-4
def task_run_playbook(hosts,
                      group,
                      data,
                      user,
                      user_agent,
                      client,
                      issuperuser=False):
    host_data = list()
    _hosts = ''
    for host in hosts:
        _hosts += '{}_{}_{}\n'.format(host['hostname'], host['ip'],
                                      host['username'])
        hostinfo = dict()
        hostinfo['hostname'] = host['hostname']
        hostinfo['ip'] = host['ip']
        hostinfo['port'] = host['port']
        hostinfo['username'] = host['username']
        hostinfo['password'] = host['password']
        hostinfo['groups'] = host['groups'] if host['groups'] else None
        if issuperuser:
            if host['superusername']:
                hostinfo['become'] = {
                    'method': 'su',
                    'user': host['superusername'],
                    'pass': host['superpassword']
                }
        host_data.append(hostinfo)
    inventory = BaseInventory(host_data)
    callback = PlayBookCallbackModule(group,
                                      playbook=data['playbook_name'],
                                      user=user,
                                      user_agent=user_agent,
                                      client=client,
                                      _hosts=_hosts)
    ansible_api = AnsibleAPI(dynamic_inventory=inventory, callback=callback)
    playbook_name = data.get('playbook_name', '')
    playbook = data.get('playbook', '')
    now = time.time()
    tmp_date = time.strftime("%Y-%m-%d", time.localtime(int(now)))
    if not os.path.isdir(os.path.join(settings.SCRIPT_ROOT, tmp_date)):
        os.makedirs(os.path.join(settings.SCRIPT_ROOT, tmp_date))
    script_file = settings.SCRIPT_DIR + '/' + tmp_date + '/' + gen_rand_char(
        16) + '_' + playbook_name
    playbook_file = settings.MEDIA_ROOT + '/' + script_file
    with open(playbook_file, 'w+') as f:
        f.write(playbook)
    ansible_api.run_playbook(playbook_yml=playbook_file,
                             group=group,
                             script=script_file)