web-dev-qa-db-ja.com

Ansibleはサービスのステータスを確認して再起動できません

私は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はdown!ただし、ansibleステータスでは、サービスはchangedとしてマークされます。サービススクリプトを確認します(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

これは私のansibleplaybookの一部です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
2
dev.brutus

解決済み:

CentOSでのSudoのデフォルトの動作が問題の原因です。 SudoコマンドにはTTYが必要です。 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-パスワード

1
dev.brutus

サービスを停止および再起動するために私がすることは次のとおりです(例として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

そしてそれは私にとってうまくいきます

0
Alex