热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

django集成ansibe实现自动化

动态生成主机列表和相关参数defcreate_admin_domain(admin_node):workpathBASE_DIRtoolsansiblescripthosts_fi

动态生成主机列表和相关参数

def create_admin_domain(admin_node):workpath = BASE_DIR + '/tools/ansible/script'hosts_file = BASE_DIR + '/tools/ansible/host/' + createhostfile()yml_file = BASE_DIR + '/tools/ansible/yml/' + 'create_admin_domain.yml'domain_path = admin_node.domainpathapp_server_name=admin_node.app_server_namehost_ip = admin_node.asset_ipansible_ssh_user = admin_node.usernameansible_ssh_pass = admin_node.passwordwith open(hosts_file, 'w+') as f:f.writelines('[app_admin_domain]\n')domain_args = "%s-%s ansible_host=%s ansible_ssh_user=%s ansible_ssh_pass=%s domain_path=%s\n" % (host_ip,app_server_name,host_ip, ansible_ssh_user, ansible_ssh_pass, domain_path)f.writelines(domain_args)cmd = 'ansible-playbook -i %s %s --extra-vars "workpath=%s"' % (hosts_file, yml_file, workpath)print(cmd)p = Popen(cmd, stderr=PIPE, stdout=PIPE, shell=True)data = p.communicate()if p.returncode == 0:print("执行成功")else:print("执行失败")

动态生成参数代码

[app_admin_domain]
10.199.137.5-itsgl ansible_host=10.199.137.5 ansible_ssh_user=weblogic ansible_ssh_pass=weblogic domain_path=/its/weblogic/user_projects/domains/itsgl_domain

生成结果

 动态生成ansible的主机文件的时候 要注意主机别名在不同的主机清单文件中不能重复 否则只会执行第一个主机清单   后面的清单文件不会被执行

with open(hosts_file, 'w+') as f:f.writelines('[app_jmsmodule_servernode]\n')data_args="%s-jmsmodule-%s ansible_host=%s ansible_ssh_user=%s ansible_ssh_pass=%s" \" ip=%s port=%s datafile=%s\n" %(host_ip,generate_random_str(6),host_ip,ansible_ssh_user,ansible_ssh_pass,host_ip,port,jms.datafile)#%s-jmsmodule-%s 代表别名字符串 生成的字符串在每个hosts文件中必须是唯一的

View Code

 

编写yml文件

---- hosts: app_admin_domaintasks:- name: mkdir app_admin_domain directoryfile: path={{domain_path}}/ state=directory mode=0755- name: copy local war to admin pathcopy: src={{local_file}} dest={{domain_path}}/ backup=yes

View Code

---- hosts: app_jmsmodule_servernodetasks:- name: copy data file to remote hostcopy: src={{datafile}} dest=/its/weblogic/user_projects/domains/wlst/jmsmodule_data- name: create jmsmoduleshell: sh /its/weblogic/wlserver_10.3/common/bin/wlst.sh /its/weblogic/user_projects/domains/wlst/jmsmodule.py {{ip}} {{port}} /its/weblogic/user_projects/domains/wlst/jmsmodule_data

View Code

---- hosts: app_jms_servernodetasks:- name: create jms serversshell: sh /its/weblogic/wlserver_10.3/common/bin/wlst.sh /its/weblogic/user_projects/domains/wlst/jmsserver.py {{ip}} {{port}} {{jmsname}} {{jmstarget}}

View Code

---- hosts: app_server_domaintasks:- name: check {{domain_path}} is exitsshell: ls {{domain_path}}ignore_errors: Trueregister: result- name: mkdir {{domain_path}} directoryfile: path={{domain_path}}/ state=directory mode=0755when: result|failed- name: copy mb file to {{domain_path}}copy: src={{local_file}}/ dest={{domain_path}} backup=yes- name: excute cpdomain shellshell: cd {{domain_path}} && bash cpdomain.sh {{appname}} {{apport}}

yml一次执行多条shell

---- hosts: app_server_domaintasks:- name: upload itsmb_domaincopy: src={{local_file}} dest={{domain_parent}}- name: check {{domain_path}} is exitsshell: ls {{domain_path}}ignore_errors: Trueregister: result- name: mkdir {{domain_path}} directoryfile: path={{domain_path}}/ state=directory mode=0755when: result|failed- name: copy mbmodain file to {{domain_path}}shell: cp -r {{domain_parent}}/itsmb_domain/* {{domain_path}}- name: excute cpdomain shellshell: cd {{domain_path}} && bash cpdomain.sh {{appname}} {{apport}}

View Code

 

调试

(jade) [root@node3 jadehare]# ansible-playbook -i /mnt/hgfs/jadehare/tools/ansible/host/host_2018102310040032 /mnt/hgfs/jadehare/tools/ansible/yml/create_admin_domain.yml --extra-vars "workpath=/mnt/hgfs/jadehare/tools/ansible/script" -vvv
No config
file found; using defaultsPLAYBOOK: create_admin_domain.yml *********************************************************************
1 plays in /mnt/hgfs/jadehare/tools/ansible/yml/create_admin_domain.ymlPLAY [app_admin_domain] *******************************************************************************TASK [Gathering Facts] ********************************************************************************
Using module
file /root/.pyenv/versions/3.5.3/envs/jade/lib/python3.5/site-packages/ansible/modules/system/setup.py
10.199.137.5> ESTABLISH SSH CONNECTION FOR USER: weblogic
10.199.137.5> SSH: EXEC sshpass -d12 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o User=weblogic -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/471b94e774 ansible_host=10.199.137.5 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
10.199.137.5> (255, b'', b'ssh: Could not resolve hostname ansible_host=10.199.137.5: Name or service not known\r\n')
fatal: [ansible_host
=10.199.137.5]: UNREACHABLE! => {"changed": false,"msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname ansible_host=10.199.137.5: Name or service not known\r\n","unreachable": true
}to retry, use:
--limit @/mnt/hgfs/jadehare/tools/ansible/yml/create_admin_domain.retryPLAY RECAP ********************************************************************************************
ansible_host
=10.199.137.5 : ok=0 changed=0 unreachable=1 failed=0 (jade) [root@node3 jadehare]# ansible-playbook -i /mnt/hgfs/jadehare/tools/ansible/host/host_2018102310310691 /mnt/hgfs/jadehare/tools/ansible/yml/create_admin_domain.yml --extra-vars "workpath=/mnt/hgfs/jadehare/tools/ansible/script"PLAY [app_admin_domain] *********************************************************************************TASK [Gathering Facts] **********************************************************************************
ok: [
10.199.137.5]TASK [mkdir app_admin_domain directory] *****************************************************************
ok: [
10.199.137.5]TASK [copy local war to admin path] *********************************************************************
fatal: [
10.199.137.5]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'local_file' is undefined\n\nThe error appears to have been in '/mnt/hgfs/jadehare/tools/ansible/yml/create_admin_domain.yml': line 6, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n file: path={{domain_path}}/ state=directory mode=0755\n - name: copy local war to admin path\n ^ here\n"}to retry, use: --limit @/mnt/hgfs/jadehare/tools/ansible/yml/create_admin_domain.retryPLAY RECAP **********************************************************************************************
10.199.137.5 : ok=2 changed=0 unreachable=0 failed=1

View Code

 

问题

  1.you must install the sshpass program

只需要安装sshpass即可,CentOS系统的默认yum源里面并没有这个软件
在ansible主机上安装好即可$
wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz
$ tar -xvf sshpass.tar.gz
$ cd sshpass
-1.06
$ .
/configure
$
sudo make install

View Code

 

2.Failed to connect to the host via ssh: ssh: Could not resolve hostname ansible_host=10.199.137.5: Name or service not known

  在执行ansible或者ansible-playbook命令的时候 不能直接指定IP地址 必须使用别名

[beijing]
1.1.1.1-aa ansible_host=1.1.1.1
1.1.1.2-bb ansible_host=1.1.1.2
1.1.1.3-cc ansible_host=1.1.1.3[root]# ansible beijing -m ping
1.1.1.1-aa | SUCCESS => {"changed": false,"ping": "pong"
}
1.1.1.3-bb | SUCCESS => {"changed": false,"ping": "pong"
}
1.1.1.2-cc | SUCCESS => {"changed": false,"ping": "pong"单独使用host里1.1.1.1这个IP会显示没有符合
[root@ansible ansible]# ansible
1.1.1.1 -m ping[WARNING]: Could not match supplied host pattern, ignoring: 1.1.1.1 需要使用别名才能正常运行
[root@ansible ansible]# ansible
1.1.1.1-aa -m ping
1.1.1.1-aa | SUCCESS => {"changed": false,"ping": "pong"
}这就要求我们在动态生成主机清单文件的时候必须生成一个别名 否则会出现异常
格式如下
10.199.137.5-itsgl就是一个别名字符串
10.199.137.5-itsgl ansible_host=10.199.137.5

别名解决方案

 

3.ansible playbook判断指定的目录或者文件是否存在

---- hosts: app_server_domaintasks:- name: check {{domain_path}} is exitsshell: ls {{domain_path}}ignore_errors: Trueregister: result- name: mkdir {{domain_path}} directoryfile: path={{domain_path}}/ state=directory mode=0755when: result|failed- name: copy mb file to {{domain_path}}copy: src={{local_file}}/ dest={{domain_path}} backup=yeswhen: result|failed- name: excute cpdomain shellshell: sh {{domain_path}}/cpdomain.shwhen: result|failed

yml语法

ansible 的常用模块中没有判定当文件存在或者不存在时执行某个命令---- name: judge a file or dir is exitsshell: ls /home/sfyignore_errors: Trueregister: result- shell: echo "file exit"when: result|succeeded- shell: echo "file not exit"when: result|failed

View Code

 

django把复杂数据结构参数传递给后台脚本

    1.由django把数据内容写入指定的文件

    2.调用脚本的时候把文件路径传递给脚本

    3.脚本读取指定路径的文件内容

with open(jms.datafile,'w+') as f:dict={}dict["name"]=jms.jmsmodule_namedict["resources"]=[]resourcelist=JmsModuleSet.objects.all().filter(jmsmodule=jms.id)for res in resourcelist:dict["resources"].append({"resourcename":res.resource_name,"type":res.jmsmoduleset_type})dict["targets"]=[]taglist=jms.server_list.all()for tag in taglist:dict["targets"].append(tag.app_server_name)f.write(str(dict))writelogfile(jms.deploylogfile,"%s 写内容入完毕" %jms.datafile)

views中被内容写入文件

import osdef create_jmsmodule(ip,port,filename):f = open(filename, "r")re = f.read()jsmodule = eval(re)f.close()wls:/itsgl_domain/edit !> f=open("/wlst/jmsmodule_data","r")
wls:
/itsgl_domain/edit !> re=f.read()
wls:
/itsgl_domain/edit !> print re
{
'resources': [{'resourcename': 'aa1111', 'type': 'queue'}, {'resourcename': 'aaa2', 'type': 'connectFactory'}], 'targets': ['wtjscxnw', 'wtmhnw'], 'name': 'aaaa'}
wls:
/itsgl_domain/edit !> obj=eval(re)
wls:
/itsgl_domain/edit !> print obj
{
'targets': ['wtjscxnw', 'wtmhnw'], 'name': 'aaaa', 'resources': [{'type': 'queue', 'resourcename': 'aa1111'}, {'type': 'connectFactory', 'resourcename': 'aaa2'}]}
wls:
/itsgl_domain/edit !>

脚本读取文件内容

def jms_add(request):apps_id = request.GET.get('mid', '')jmsobj= JmsModule.objects.filter(id=apps_id)[0]if request.POST:apps_form = JmsModuleSetForm(request.POST)apps_form.instance.jmsmodule=jmsobj

django保存外键对象

 

 

ansible查看详细命令执行情况

(jade) [root@node3 jadehare]# ansible-playbook -i /mnt/hgfs/jadehare/tools/ansible/host/host_2018102611079748 /mnt/hgfs/jadehare/tools/ansible/yml/create_server_domain.yml --extra-vars "workpath=/mnt/hgfs/jadehare/tools/ansible/script" -vvv详细信息如下:
changed: [
10.199.137.5-createserver-6FUGa1] => {"changed": true,"cmd": "bash /its/weblogic/user_projects/domains/wtdsrwnw_domain/cpdomain.sh wtdsrwnw 8111","delta": "0:00:00.038308","end": "2018-10-26 11:16:14.366728","invocation": {"module_args": {"_raw_params": "bash /its/weblogic/user_projects/domains/wtdsrwnw_domain/cpdomain.sh wtdsrwnw 8111","_uses_shell": true,"chdir": null,"creates": null,"executable": null,"removes": null,"warn": true}},"rc": 0,"start": "2018-10-26 11:16:14.328420","stderr": "sed:无法读取 startWebLogic.sh:没有那个文件或目录\nsed:无法读取 bin/startWebLogic.sh:没有那个文件或目录\nsed:无法读取 bin/setDomainEnv.sh:没有那个文件或目录\nsed:无法读取 bin/stopWebLogic.sh:没有那个文件或目录\nsed:无法读取 bin/startManagedWebLogic.sh:没有那个文件或目录\nsed:无法读取 bin/stopManagedWebLogic.sh:没有那个文件或目录\nsed:无法读取 init-info/startscript.xml:没有那个文件或目录\nsed:无法读取 init-info/tokenValue.properties:没有那个文件或目录\nsed:无法读取 config/config.xml:没有那个文件或目录\nsed:无法读取 config/config.xml:没有那个文件或目录\nsed:无法读取 bin/setDomainEnv.sh:没有那个文件或目录","stderr_lines": ["sed:无法读取 startWebLogic.sh:没有那个文件或目录","sed:无法读取 bin/startWebLogic.sh:没有那个文件或目录","sed:无法读取 bin/setDomainEnv.sh:没有那个文件或目录","sed:无法读取 bin/stopWebLogic.sh:没有那个文件或目录","sed:无法读取 bin/startManagedWebLogic.sh:没有那个文件或目录","sed:无法读取 bin/stopManagedWebLogic.sh:没有那个文件或目录","sed:无法读取 init-info/startscript.xml:没有那个文件或目录","sed:无法读取 init-info/tokenValue.properties:没有那个文件或目录","sed:无法读取 config/config.xml:没有那个文件或目录","sed:无法读取 config/config.xml:没有那个文件或目录","sed:无法读取 bin/setDomainEnv.sh:没有那个文件或目录"],"stdout": "","stdout_lines": []
}
META: ran handlers
META: ran handlersPLAY RECAP
**********************************************************************************************
10.199.137.5-createserver-6FUGa1 : ok=4 changed=2 unreachable=0 failed=0

调试选项

 


转载于:https://www.cnblogs.com/yxh168/p/9835437.html


推荐阅读
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了一个程序,可以输出1000内能被3整除且个位数为6的所有整数。程序使用了循环和条件判断语句来筛选符合条件的整数,并将其输出。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • Day2列表、字典、集合操作详解
    本文详细介绍了列表、字典、集合的操作方法,包括定义列表、访问列表元素、字符串操作、字典操作、集合操作、文件操作、字符编码与转码等内容。内容详实,适合初学者参考。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • 如何提高PHP编程技能及推荐高级教程
    本文介绍了如何提高PHP编程技能的方法,推荐了一些高级教程。学习任何一种编程语言都需要长期的坚持和不懈的努力,本文提醒读者要有足够的耐心和时间投入。通过实践操作学习,可以更好地理解和掌握PHP语言的特异性,特别是单引号和双引号的用法。同时,本文也指出了只走马观花看整体而不深入学习的学习方式无法真正掌握这门语言,建议读者要从整体来考虑局部,培养大局观。最后,本文提醒读者完成一个像模像样的网站需要付出更多的努力和实践。 ... [详细]
  • Hibernate延迟加载深入分析-集合属性的延迟加载策略
    本文深入分析了Hibernate延迟加载的机制,特别是集合属性的延迟加载策略。通过延迟加载,可以降低系统的内存开销,提高Hibernate的运行性能。对于集合属性,推荐使用延迟加载策略,即在系统需要使用集合属性时才从数据库装载关联的数据,避免一次加载所有集合属性导致性能下降。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 本文介绍了2015年九月八日的js学习总结及相关知识点,包括参考书《javaScript Dom编程的艺术》、js简史、Dom、DHTML、解释型程序设计和编译型程序设计等内容。同时还提到了最佳实践是将标签放到HTML文档的最后,并且对语句和注释的使用进行了说明。 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
author-avatar
cathy李lee
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有