Ansible não pode verificar o status do serviço e reiniciá-lo

2

Eu sou novo em ansible. Agora eu tento reiniciar meu serviço Tomcat. Parece que ansible não pode detectar o status do serviço (iniciado / parado) e não pode (re) iniciá-lo. Por favor, diga como fazer o diagnóstico?

Veja aqui ( rotor-app-01.dev é o host Tomcat. brutus-HP-ENVY-17 é meu host local):

[email protected]:~
$ sudo service tomcat stop
Using CATALINA_BASE:   /opt/vendor/tomcat
Using CATALINA_HOME:   /opt/vendor/tomcat
Using CATALINA_TMPDIR: /opt/vendor/tomcat/temp
Using JRE_HOME:        /home/vendor/jdk1.6.0_45
Using CLASSPATH:       /home/vendor/jdk1.6.0_45/lib/tools.jar:/opt/vendor/tomcat/bin/bootstrap.jar:/opt/vendor/webapps/app/config
Using CATALINA_PID:    /var/run/vendor/tomcat.pid
Tomcat is stopped                                          [  OK  ]

[email protected]:~
$ sudo service tomcat status
Tomcat is stopped                                          [  OK  ]

Agora o Tomcat está parado. Vamos começar!

brutus@brutus-HP-ENVY-17: ~/projects/rotor/devops
$ ansible-playbook -b -i dev.ini site.yaml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [rotor-app-01.dev]
ok: [rotor-db-01.dev]

TASK [common : create /opt/devops] *********************************************
ok: [rotor-db-01.dev] => (item={u'path': u'/opt'})
ok: [rotor-app-01.dev] => (item={u'path': u'/opt'})
ok: [rotor-db-01.dev] => (item={u'path': u'/opt/devops'})
ok: [rotor-app-01.dev] => (item={u'path': u'/opt/devops'})

TASK [common : create environment marker: dev] *********************************
ok: [rotor-app-01.dev]
ok: [rotor-db-01.dev]

PLAY [app] *********************************************************************

TASK [setup] *******************************************************************
ok: [rotor-app-01.dev]

TASK [app : include] ***********************************************************
included: /home/brutus/projects/rotor/devops/roles/app/tasks/tomcat_install.yaml for rotor-app-01.dev

TASK [app : Install Tomcat] ****************************************************
ok: [rotor-app-01.dev] => {
    "msg": "TODO: Install Tomcat"
}

TASK [app : Copy catalina.sh] **************************************************
ok: [rotor-app-01.dev]

TASK [app : include] ***********************************************************
included: /home/brutus/projects/rotor/devops/roles/app/tasks/webapps.yaml for rotor-app-01.dev

TASK [app : Remove files from /opt/vendor/webapps/app/web/WEB-INF/plugins/plugins] ***
ok: [rotor-app-01.dev]

TASK [app : include] ***********************************************************
included: /home/brutus/projects/rotor/devops/roles/app/tasks/tomcat_as_service.yaml for rotor-app-01.dev

TASK [app : Copy Tomcat service script] ****************************************
ok: [rotor-app-01.dev]

TASK [app : Copy Tomcat settings] **********************************************
ok: [rotor-app-01.dev]

TASK [app : Create run folder] *************************************************
ok: [rotor-app-01.dev]

TASK [app : Start Tomcat service] **********************************************
changed: [rotor-app-01.dev]

PLAY RECAP *********************************************************************
rotor-app-01.dev           : ok=14   changed=1    unreachable=0    failed=0
rotor-db-01.dev            : ok=3    changed=0    unreachable=0    failed=0

O manual é jogado. Vamos verificar o status do Tomcat:

[email protected]:~
$ sudo service tomcat status
Tomcat is stopped                                          [  OK  ]

[email protected]:~
$ ps -ef | grep java
user     58396 56290  0 16:52 pts/1    00:00:00 grep java

O Tomcat está para baixo ! Mas no status ansible, o serviço é marcado como alterado . Verifique o script de serviço (ele pode iniciar o Tomcat?)

[email protected]:~
$ sudo service tomcat start
find: '/opt/vendor/tomcat/webapps/*': No such file or directory
find: '/opt/vendor/tomcat/temp/*': No such file or directory
Using CATALINA_BASE:   /opt/vendor/tomcat
Using CATALINA_HOME:   /opt/vendor/tomcat
Using CATALINA_TMPDIR: /opt/vendor/tomcat/temp
Using JRE_HOME:        /home/vendor/jdk1.6.0_45
Using CLASSPATH:       /home/vendor/jdk1.6.0_45/lib/tools.jar:/opt/vendor/tomcat/bin/bootstrap.jar:/opt/vendor/webapps/app/config
Using CATALINA_PID:    /var/run/vendor/tomcat.pid
Tomcat is started                                          [  OK  ]

[email protected]:~
$ sudo service tomcat status
Tomcat is running                                          [  OK  ]

O script de serviço está correto! Um pouco de informação:

[email protected]:~
$ uname -a
Linux rotor-app-01 2.6.32-573.18.1.el6.x86_64 #1 SMP Tue Feb 9 22:46:17 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

brutus@brutus-HP-ENVY-17: ~/projects/rotor/devops
$ ansible --version
ansible 2.0.2.0
  config file = 
  configured module search path = Default w/o overrides

Isso faz parte do meu ansioso manual de exercícios tomcat_as_service.yaml :

---
- name: 'Copy Tomcat service script'
  copy: src='service/tomcat.sh' dest='/etc/init.d/tomcat' mode='0755' owner=root group=root
- name: 'Copy Tomcat settings'
  template: src=service/tomcat.txt dest=/etc/default/tomcat mode='0644' owner=root group=root
- name: 'Create run folder'
  file: path=/var/run/vendor/ state=directory mode='0755' owner=vendor group=vendor
- name: 'Start Tomcat service'
  service: name=tomcat state=started sleep=60 enabled=yes

O /etc/init.d/tomcat.sh :

#!/bin/sh
### BEGIN INIT INFO
# Provides:          tomcat
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      nginx
# Should-Stop:       nginx
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Apache Tomcat.
### END INIT INFO

. /etc/environment
. /lib/lsb/init-functions
. /etc/default/tomcat

shutdown () {
    if [ -e $CATALINA_PID ]
    then
        PID='cat $CATALINA_PID'
        sudo -u vendor CATALINA_PID="$CATALINA_PID" CATALINA_HOME="$CATALINA_HOME" sh $CATALINA_HOME/bin/shutdown.sh
        for i in 'seq 1 60'
        do
            if [ -e /proc/$PID ]
            then
                if [ "$i" = "60" ]
                then
                    log_warning_msg "Killing $PID"
                    kill -9 $PID
                else
                    log_success_msg "Try... $i"
                    sleep 1
                fi
            fi

        done
    else
        log_warning_msg "No pid file"
        sh $CATALINA_HOME/bin/shutdown.sh
    fi

    log_success_msg "Tomcat is stopped"
}

startup () {
    find $CATALINA_HOME/webapps/* -maxdepth 0 -type d -exec rm -rvf '{}' \;
    find $CATALINA_HOME/temp/* -maxdepth 0 -exec rm -rvf '{}' \;
    sudo -u vendor CATALINA_PID="$CATALINA_PID" CATALINA_HOME="$CATALINA_HOME" sh $CATALINA_HOME/bin/startup.sh
    log_success_msg "Tomcat is started"
}

CATALINA_STATUS="stopped"
if [ -f $CATALINA_PID ]
then
    test -e /proc/'cat $CATALINA_PID' && CATALINA_STATUS="running" || CATALINA_STATUS="dead"
fi

case $1 in
start)
        if [ "$CATALINA_STATUS" = running ]
        then
            log_warning_msg "Tomcat is already started"
            exit 0
        fi
        startup
        ;;
stop)
        shutdown
        ;;
restart|reload)
        shutdown
        startup
        ;;
status)
        log_success_msg "Tomcat is $CATALINA_STATUS"

        case $CATALINA_STATUS in
        running)
            exit 0;;
        dead)
            exit 1;;
        stopped)
            exit 3;;
        esac
esac
exit 0
    
por dev.brutus 26.04.2016 / 16:32

2 respostas

1

RESOLVIDO:

O comportamento padrão sudo no CentOS é a raiz do problema. O comando sudo requer um TTY. Desativei a opção Defaults requiretty no meu arquivo /etc/sudoers e resolvi esse problema.

Veja esta discussão: link

    
por 11.05.2016 / 17:52
0

Aqui está o que eu faço para parar e reiniciar um serviço (usando o postfix como exemplo).

taskfile.yml:

- service: name=postfix state=restarted

playbook.yml:

- name: My Playbook
  hosts: my_hostgroup
  tasks:
    - include ./taskfile.yml
      become: yes
      become_method: sudo
      become_user: root

E isso funciona bem para mim

    
por 27.04.2016 / 20:33