Ansible使用playbook自动化编译安装Nginx(4)


[root@cml3 ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address          Foreign Address        State      PID/Program name             
tcp        0      0 0.0.0.0:80              0.0.0.0:*              LISTEN      29264/nginx: master
tcp        0      0 0.0.0.0:8080            0.0.0.0:*              LISTEN      29264/nginx: master
     

四、定义日志文件
下面是介绍如何定义日志:(在ansible1.9.1版本之后有个bug所以定义不了日志文件只能降版本到1.9.1了)
1、ansible倒回去版本:1.9.1
需要安装gcc、python-devel
[root@cml1 ~]#yum install python-pip
[root@cml1 ~]#pip install ansible==1.9.1

2、callback插件:
[root@cml1tasks]# vim /etc/ansible/ansible.cfg
callback_plugins  = /usr/share/ansible/plugins/callback
bin_ansible_callbacks= True

3、在callback目录下创建日志处理文件:
[root@cml1tasks]# cd /usr/share/ansible/plugins/callback
[root@cml1callback]# ls
log.py  log.pyc
[root@cml1callback]# cat log.py
import os
import time
import json
 
TIME_FORMAT="%b%d %Y %H:%M:%S"
MSG_FORMAT="%(now)s- %(category)s - %(data)s\n\n"
 
if notos.path.exists("/var/log/ansible/hosts"):
  os.makedirs("/var/log/ansible/hosts")
 
def log(host,category, data):
    if type(data) == dict:
        if 'verbose_override' in data:
            # avoid logging extraneous datafrom facts
            data = 'omitted'
        else:
            data = data.copy()
            invocation = data.pop('invocation',None)
            data = json.dumps(data)
            if invocation is not None:
                data = json.dumps(invocation) +" => %s " % data
 
    path =os.path.join("/var/log/ansible/hosts", host)
    now = time.strftime(TIME_FORMAT,time.localtime())
    fd =open(path, "a")
    fd.write(MSG_FORMAT % dict(now=now,category=category, data=data))
    fd.close()
 
classCallbackModule(object):
    """
    logs playbook results, per host, in/var/log/ansible/hosts
    """
 
    def on_any(self, *args, **kwargs):
        pass
 
    def runner_on_failed(self, host, res,ignore_errors=False):
        log(host, 'FAILED', res)
 
    def runner_on_ok(self, host, res):
        log(host, 'OK', res)
 
    def runner_on_skipped(self, host,item=None):
        log(host, 'SKIPPED', '...')
 
    def runner_on_unreachable(self, host, res):
        log(host, 'UNREACHABLE', res)
 
    def runner_on_no_hosts(self):
        pass
 
    def runner_on_async_poll(self, host, res,jid, clock):
        pass
 
    def runner_on_async_ok(self, host, res,jid):
        pass
 
    def runner_on_async_failed(self, host, res,jid):
        log(host, 'ASYNC_FAILED', res)
 
    def playbook_on_start(self):
        pass
 
    def playbook_on_notify(self, host,handler):
        pass
 
    def playbook_on_no_hosts_matched(self):
        pass
 
    def playbook_on_no_hosts_remaining(self):
        pass
 
    def playbook_on_task_start(self, name,is_conditional):
        pass
 
    def playbook_on_vars_prompt(self, varname,private=True, prompt=None, encrypt=None, confirm=False, salt_size=None,salt=None, default=None):
        pass
 
    def playbook_on_setup(self):
        pass
 
    def playbook_on_import_for_host(self, host,imported_file):
        log(host, 'IMPORTED', imported_file)
 
    def playbook_on_not_import_for_host(self,host, missing_file):
        log(host, 'NOTIMPORTED', missing_file)
 
    def playbook_on_play_start(self, name):
        pass
 
    def playbook_on_stats(self, stats):
        pass

下面关于Ansible的文章您也可能喜欢,不妨参考下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3a6a64fd3dece1aa67af101697dbb15b.html