当前位置:首页 > 服务端 > django集成ansibe实现自动化

django集成ansibe实现自动化

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

django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
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.domainpath
    app_server_name=admin_node.app_server_name
    host_ip = admin_node.asset_ip
    ansible_ssh_user = admin_node.username
    ansible_ssh_pass = admin_node.password
    with 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("执行失败")
动态生成参数代码
django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
[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的主机文件的时候 要注意主机别名在不同的主机清单文件中不能重复 否则只会执行第一个主机清单   后面的清单文件不会被执行

django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
 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文件

django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
---
  - hosts: app_admin_domain
    tasks:
    - name: mkdir app_admin_domain directory
      file: path={{domain_path}}/ state=directory mode=0755
    - name: copy local war to admin path
      copy: src={{local_file}} dest={{domain_path}}/ backup=yes
View Code
django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
---
  - hosts: app_jmsmodule_servernode
    tasks:
    - name: copy data file to remote host
      copy: src={{datafile}} dest=/its/weblogic/user_projects/domains/wlst/jmsmodule_data
    - name: create jmsmodule
      shell: 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
django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
---
  - hosts: app_jms_servernode
    tasks:
    - name: create jms servers
      shell: sh /its/weblogic/wlserver_10.3/common/bin/wlst.sh  /its/weblogic/user_projects/domains/wlst/jmsserver.py {{ip}} {{port}} {{jmsname}} {{jmstarget}}
View Code
django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
---
  - hosts: app_server_domain
    tasks:
    - name: check {{domain_path}} is exits
      shell: ls {{domain_path}}
      ignore_errors: True
      register: result
    - name: mkdir {{domain_path}} directory
      file: path={{domain_path}}/ state=directory mode=0755
      when: result|failed
    - name: copy mb file to {{domain_path}}
      copy: src={{local_file}}/ dest={{domain_path}} backup=yes
    - name: excute cpdomain shell
      shell: cd {{domain_path}} && bash cpdomain.sh {{appname}} {{apport}}
yml一次执行多条shell
django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
---
  - hosts: app_server_domain
    tasks:
    - name: upload itsmb_domain
      copy: src={{local_file}} dest={{domain_parent}}
    - name: check {{domain_path}} is exits
      shell: ls {{domain_path}}
      ignore_errors: True
      register: result
    - name: mkdir {{domain_path}} directory
      file: path={{domain_path}}/ state=directory mode=0755
      when: result|failed
    - name: copy mbmodain file to {{domain_path}}
      shell: cp -r {{domain_parent}}/itsmb_domain/*  {{domain_path}}
    - name: excute cpdomain shell
      shell: cd {{domain_path}} && bash cpdomain.sh {{appname}} {{apport}}
View Code

 

调试

django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
(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 defaults

PLAYBOOK: create_admin_domain.yml *********************************************************************
1 plays in /mnt/hgfs/jadehare/tools/ansible/yml/create_admin_domain.yml

PLAY [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.retry

PLAY 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.retry

PLAY RECAP **********************************************************************************************
10.199.137.5               : ok=2    changed=0    unreachable=0    failed=1   
View Code

 

问题

  1.you must install the sshpass program

django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
只需要安装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地址 必须使用别名

django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
[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判断指定的目录或者文件是否存在

django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
---
  - hosts: app_server_domain
    tasks:
    - name: check {{domain_path}} is exits
      shell: ls {{domain_path}}
      ignore_errors: True
      register: result
    - name: mkdir {{domain_path}} directory
      file: path={{domain_path}}/ state=directory mode=0755
      when: result|failed
    - name: copy mb file to {{domain_path}}
      copy: src={{local_file}}/ dest={{domain_path}} backup=yes
      when: result|failed
    - name: excute cpdomain shell
      shell: sh {{domain_path}}/cpdomain.sh
      when: result|failed
yml语法
django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
ansible 的常用模块中没有判定当文件存在或者不存在时执行某个命令

---

- name: judge a file or dir is exits
  shell: ls /home/sfy
  ignore_errors: True
  register: result

- shell: echo "file exit"
  when: result|succeeded

- shell: echo "file not exit"
  when: result|failed
View Code

 

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

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

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

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

django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
    with open(jms.datafile,'w+') as f:
        dict={}
        dict["name"]=jms.jmsmodule_name
        dict["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中被内容写入文件
django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
import os

def 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 !> 
脚本读取文件内容
django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
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保存外键对象

 django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记

 

ansible查看详细命令执行情况

django集成ansibe实现自动化 _ JavaClub全栈架构师技术笔记
(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 handlers

PLAY RECAP **********************************************************************************************
10.199.137.5-createserver-6FUGa1 : ok=4    changed=2    unreachable=0    failed=0 
调试选项

 

作者:不懂123
来源链接:https://www.cnblogs.com/yxh168/p/9835437.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/124655.html

分享给朋友: