django集成ansibe实现自动化

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

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("执行失败")
动态生成参数代码
[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_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
---
  - 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
---
  - 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
---
  - 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
---
  - 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

 

调试

(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

只需要安装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_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语法
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.脚本读取指定路径的文件内容

    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中被内容写入文件
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 !> 
脚本读取文件内容
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 handlers

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

 

posted @ 2018-10-26 10:31  不懂123  阅读(869)  评论(0编辑  收藏  举报