Я новичок в ансибле. Теперь я пытаюсь перезапустить службу Tomcat. Похоже, что ansible не может определить статус службы (запущен / остановлен) и не может (повторно) запустить ее. Подскажите пожалуйста, как сделать диагностику?
Посмотреть здесь (rotor-app-01.dev
это хост Tomcat. brutus-HP-ENVY-17
это мой локальный хост):
user@rotor-app-01.dev:~
$ 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 ]
user@rotor-app-01.dev:~
$ sudo service tomcat status
Tomcat is stopped [ OK ]
Теперь Tomcat остановлен. Начнем!
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
Играется в пьесу. Давайте проверим статус Tomcat:
user@rotor-app-01.dev:~
$ sudo service tomcat status
Tomcat is stopped [ OK ]
user@rotor-app-01.dev:~
$ ps -ef | grep java
user 58396 56290 0 16:52 pts/1 00:00:00 grep java
Tomcat - это вниз! Но в статусе доступный сервис помечен как изменено. Проверьте служебный скрипт (может ли он запустить Tomcat?)
user@rotor-app-01.dev:~
$ 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 ]
user@rotor-app-01.dev:~
$ sudo service tomcat status
Tomcat is running [ OK ]
Сервисный скрипт правильный! Немного информации:
user@rotor-app-01.dev:~
$ 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
Это часть моей инструкции по применению 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
В /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
РЕШЕНО:
В sudo
поведение по умолчанию в CentOS является корнем проблемы. В sudo
команде требуется телетайп. Я отключил Defaults requiretty
вариант в моем /etc/sudoers
файл и решили эту проблему.
См. Это обсуждение: https://unix.stackexchange.com/questions/122616/why-do-i-need-a-tty-to-run-sudo-if-i-can-sudo-without-a-password
Вот что я делаю для остановки и перезапуска службы (на примере postfix).
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
И это хорошо работает для меня