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

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
<ansible_host=10.199.137.5> ESTABLISH SSH CONNECTION FOR USER: weblogic
<ansible_host=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'"'"''
<ansible_host=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

django集成ansibe实现自动化相关推荐

  1. python钉钉扫码登录程序_使用python+django集成钉钉三方扫码登陆

    使用python+django集成钉钉三方扫码登陆 \(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\) 1. 进入钉钉开放平台--->点击左下角 ...

  2. django 集成个推_Django动态添加定时任务之djangocelery的使用

    定时任务和周期任务在我们日常工作中应用广泛,例如定时发布.周期巡检等,通常我们会借助Linux下的Crontab来实现,但如何将这一功能搬进我们自研的运维系统呢?借助django-celery即可轻松 ...

  3. Django + vue 开发接口自动化可视化平台

    Django + vue 开发接口自动化可视化平台 界面演示 接口详情 环境管理 变量管理 定时任务 定时任务结果 任务结果详情 UML 图表 #体验地址: http://1.12.224.200/ ...

  4. 保姆级别 附带源码 Django集成channels(一)实现简单聊天功能

    目录 前言 不想看我瞎BB可以直接跳到这里 1.WebSocket 1.1 ajax轮询 1.2 long poll 1.3 Websocket 2.Channels 2.1 WSGI 2.2 ASG ...

  5. Django集成Markdown编辑器【附源码】

    专注内容写作的你一定不要错过markdown 简单介绍 markdown是一种标记语言,通过简单的标记语法可以使普通的文本内容具有一定的格式,使用非常简单,学习成本极低 目前各大Blog平台都已支持m ...

  6. django如何调用php接口,使用django集成第三方api开发接口注意事项

    重要概念 如果您计划将Django应用程序与第三方REST API集成,请务必记住以下几点: 使用API很慢. 我们必须仔细实现它们,因为它是在服务器端执行的额外HTTP请求,因此它可以大大增加请求/ ...

  7. django 集成个推_持续集成CircleCI vs Travis CI vs Jenkins

    Continuous Integration. CircleCI vs Travis CI vs Jenkins​hackernoon.com 持续集成(CI)的定义及其主要目标 持续集成(CI)是一 ...

  8. Django集成celery实战小项目

    上一篇已经介绍了celery的基本知识,本篇以一个小项目为例,详细说明django框架如何集成celery进行开发. 本系列文章的开发环境: window 7 + python2.7 + pychar ...

  9. django项目转pyc_Python自动化运维系列:Django搭建小博客

    如何使用Django和Python怎么搭建一个小博客呢? 这是一个简单而困难的问题.简单的原因是,只要做过一次,基本上就能做到举一反三: 困难的原因是有没有用心和耐心去完成这个实验. 如果你成功了,那 ...

最新文章

  1. Struts 源码分析笔记1(尚无内容-请跳过,省得浪费时间)
  2. 细品慢酌QuickTest关键视图(5)脚本调试
  3. OC Swift混编-Swift.h File not found
  4. webpack实战之手写一个loader和plugin
  5. android实践练习_android 练习之路 (四)
  6. java + testng wsdl 测试_在测试中使用XPATH断言的策略
  7. rabbitmq 客户端golang实战
  8. BZOJ 1090: [SCOI2003]字符串折叠
  9. Windows和Linux DNS Cache清理
  10. 吐血推荐 | 5+1款源代码管理笔记本(全平台)
  11. wind10 使用Hyper-V安装centos7遇到的问题及解决方案
  12. Mac菜鸟进阶必学的10个Mac小技巧
  13. ava.lang.IllegalArgumentException: At least one base package must be specified at org.springframewo
  14. 11个主流AI聊天机器人平台,你绝不能错过!
  15. Java 绘制带圆角头像的二维码
  16. 玉米社:竞价推广优化怎么做?竞价推广优化技巧
  17. Linux添加环境变量,以配置MySQL环境怕变量为例
  18. ubuntu空间扩容--grub修复之boot-repair修复
  19. 新年巨献2021新版QQ选号网源码
  20. 【树莓派 有趣实践】寻找小项目

热门文章

  1. 【LeetCode-SQL每日一练】—— 620. 有趣的电影
  2. oracle扩容日志文件,调整Oracle Redo Logfile日志文件的大小
  3. CCIE理论-第十篇-IPV6 VS IPV4(带你们看看U.S.A的ISP的设备)
  4. 【POJ - 3177】Redundant Paths(边双连通分量,去重边)
  5. 【牛客 - 696D】小K的雕塑(dp,鸽巢原理,01背包类问题)
  6. *【CodeForces - 122D】Lucky Transformation(字符串问题,思维剪枝,优化,有坑,需注意的问题if的层次总结)
  7. 【HDU - 6016】Count the Sheep (思维,类似二分图)
  8. mysql配置日志老化配置_mysql配置-日志大小限制和自动删除
  9. oracle启动监听读取哪个文件,监听服务启动及数据文件恢复oracle数据库
  10. php qq接收不了,php imap接收qq邮件的问题