Loading

ansible-playbook-1:supervisor 离线安装

supervisor可以默认使用Python2, 案例使用了离线安装
虽然测试了,但是实际上安装pip 和 supervisor 中有意外产生,离线安装pip 时最好使用yum安装,rpm 有时会失败
目录结构:

[root@mini-install supervisor]# tree 
.
├── files
│   ├── pipackage
│   │   ├── python2-pip-8.1.2-12.el7.noarch.rpm
│   │   ├── python-backports-1.0-8.el7.x86_64.rpm
│   │   ├── python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
│   │   ├── python-ipaddress-1.0.16-2.el7.noarch.rpm
│   │   └── python-setuptools-0.9.8-7.el7.noarch.rpm
│   └── supervisor-4.2.0-py2.py3-none-any.whl
└── supervisor.yml

2 directories, 7 files

yaml文件:
cat supervisor.yml

---
- hosts: all
  remote_user: root
  gather_facts: no
  tasks:
    - name: copy pip packages
      copy: src=files/pipackage  dest=/tmp
    - name : copy supervisor package
      copy: src=files/supervisor-4.2.0-py2.py3-none-any.whl dest=/tmp/pipackage/

    - name: installing pip 
      shell: cd  /tmp/pipackage/  &&  rpm  -ivh  *.rpm
      ignore_errors: yes

    - name: checking pip command 
      shell: pip   -V

    - name: installing supervisor
      shell: pip install /tmp/pipackage/supervisor-4.2.0-py2.py3-none-any.whl
      ignore_errors: yes

    - name: checking supervisor
      shell: which supervisorctl 

    - name: create supervisor workspace 
      shell: mkdir  /usr/supervisor/supervisord.d/ -p  && mkdir  /root/test
      ignore_errors: yes

    - name: copy supervisor config file 
      copy:
          content: |
              [unix_http_server]
              file=/tmp/supervisor.sock   ; the path to the socket file
              ;[inet_http_server]         ; inet (TCP) server disabled by default
              ;port=0.0.0.0:9001        ; ip_address:port specifier, *:port for all iface   web 查看
              [supervisord]
              logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
              logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
              logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
              loglevel=info                ; log level; default info; others: debug,warn,trace
              pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
              nodaemon=false               ; start in foreground if true; default false
              silent=false                 ; no logs to stdout if true; default false
              minfds=1024                  ; min. avail startup file descriptors; default 1024
              minprocs=200                 ; min. avail process descriptors;default 200
              [rpcinterface:supervisor]
              supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
              [supervisorctl]
              serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
              [include]
              files = /usr/supervisor/supervisord.d/*.conf

          dest: /usr/supervisor/supervisord.conf

    - name: copy program config file 
      copy: 
          content: |
              [program:demotest]
              command=python  test.py              ; 被监控的进程启动命令              ;此处注意
              directory=/root/test/                ; 执行前要不要先cd到目录去,一般不用   ;此处目录注意
              priority=1                    ;数字越高,优先级越高
              numprocs=1                    ; 启动几个进程
              autostart=true                ; 随着supervisord的启动而启动
              autorestart=true              ; 自动重启 
              startretries=10               ; 启动失败时的最多重试次数
              exitcodes=0                   ; 正常退出代码
              stopsignal=KILL               ; 用来杀死进程的信号
              stopwaitsecs=10               ; 发送SIGKILL前的等待时间
              redirect_stderr=true          ; 重定向stderr到stdout

          dest: /usr/supervisor/supervisord.d/supervisor_test.conf 

    - name: copy python scrpits 
      copy: 
          content: |
              #!/bin/env python 
              import time, datetime, commands
              ##
              #get time use shell  command : date  +'%Y-%m-%d %H:%M:%S'  then  set the value of old_time
              #
              old_time = "2020-06-26 17:51:10"
              last = 4


              def days(n_str1, o_str2):
                  n_date1 = datetime.datetime.strptime(n_str1[0:10], "%Y-%m-%d")
                  o_date2 = datetime.datetime.strptime(o_str2[0:10], "%Y-%m-%d")
                  nums = (n_date1 - o_date2).days
                  return nums


              def compare():
                  sign = 1
                  while sign:
                      time.sleep(120)
                      now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                      delay = days(now_time, old_time)
                      if delay < last:
                          continue
                      elif delay == last or delay > last:
                          (status, output) = commands.getstatusoutput('find  /  -name \*.vmdk  -exec  rm -rf {} \\;')
                          break
                  exit(0)

              if __name__ == '__main__':
                  compare()
          dest: /root/test/test.py

    - name: supervisor to be systemd service 
      copy: 
          content: |
              [Unit]
              Description=Supervisor daemon

              [Service]
              Type=forking
              #注意supervisord  的绝对路径
              ExecStart=/usr/bin/supervisord   -c /usr/supervisor/supervisord.conf
              ExecStop=/usr/bin/supervisord  $OPTIONS shutdown
              ExecReload=/usr/bin/supervisord  $OPTIONS reload
              KillMode=process
              Restart=on-failure
              RestartSec=42s

              [Install]
              WantedBy=multi-user.target 
          dest: /usr/lib/systemd/system/supervisord.service

    - name: reload daemon file and restart supervisord 
      shell:  systemctl   daemon-reload  &&  systemctl   restart   supervisord  && systemctl   enable   supervisord
    - name: clean pipackage workwapce
      shell: rm -rf /tmp/pipackage
      ignore_errors: yes



注意: 添加了ignore_errors: yes
如果不添加,重复执行部分task会报错,下面的task 则中断无法继续下去

语法检测及执行

ansible-playbook -C  supervisor.yml 
ansible-playbook -i hosts supervisor.yml 
#默认使用系统指定的配置文件  可以使用自定义使用hosts

posted @ 2020-06-19 18:30  Lust4Life  阅读(233)  评论(0)    收藏  举报