我是 ansible 新手。现在我尝试重新启动我的 Tomcat 服务。看起来 ansible 无法检测服务状态(已启动/已停止)并且无法(重新)启动它。请告诉我如何进行诊断?
参见此处(rotor-app-01.dev
是 Tomcat 主机。brutus-HP-ENVY-17
是我的本地主机):
[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 ]
现在 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 状态:
[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
Tomcat 是向下!但在 ansible 状态中,该服务被标记为已更改. 检查服务脚本(可以启动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 ]
服务单正确!以下是一些信息:
[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
这是我的 ansible 剧本的一部分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
答案1
解决了:
CentOS 上的默认行为sudo
是问题的根源。该sudo
命令需要 TTY。我已Defaults requiretty
在文件中禁用该选项/etc/sudoers
并解决了此问题。
答案2
以下是我停止和重新启动服务的操作(以 postfix 为例)。
任务文件.yml:
- service: name=postfix state=restarted
剧本.yml:
- name: My Playbook
hosts: my_hostgroup
tasks:
- include ./taskfile.yml
become: yes
become_method: sudo
become_user: root
对我来说效果很好