2017-10-09 10 views
0

터미널 호스트에 서비스가 있는지 확인하고 싶습니다.서비스가 Ansible에 있는지 확인하는 방법은 무엇입니까?

그래서 방금 플레이 북을 아래와 같이 작성했습니다.

--- 

- hosts: '{{ host }}' 
    become: yes 
    vars: 
    servicename: 
    tasks: 

    - name: Check if Service Exists 
    stat: 'path=/etc/init.d/{{ servicename }}' 
    register: servicestatus 
    with_items: '{{ servicename }}' 

    - name: Show service service status 
    debug: 
     msg: '{{ servicename }} is exists.' 
    with_items: '{{ servicename }}' 
    when: servicestatus.stat.exists 

그런 다음이 플레이 북을 이미 Nginx를 실행중인 호스트에 실행하려고합니다.

ansible-playbook cheknginxservice.yml -i /etc/ansible/hosts -e 'host=hostname' -e 'servicename=nginx' 

그리고 나는 이와 같은 오류가 발생했습니다.

FAILED! => {"failed": true, "msg": "The conditional check 'servicestatus.stat.exists' failed. The error was: error while evaluating conditional (servicestatus.stat.exists): 'dict object' has no attribute 'stat'\n\nThe error appears to have been in '/home/centos/cheknginxservice.yml': line 13, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n with_items: '{{ servicename }}'\n - name: Show service servicestatus\n ^here\n"} 
     to retry, use: --limit @/home/centos/cheknginxservice.retry 

따라서 문제는 관련된 조건을 사용할 때 통계 모듈에 관한 것입니다.

답변

2

with_items을 왜 사용하고 있습니까? 여러 서비스를 전달할 계획입니까? with_items을 사용하면 결과가 목록이되므로 중요합니다. with_items을 제거하면 제대로 작동합니다. 여러 서비스를 전달하려면 with_items을 반복하고 servicename 대신 item을 사용해야합니다.

- name: Check if Service Exists 
    stat: 'path=/etc/init.d/{{ servicename }}' 
    register: servicestatus 

    - name: Show service service status 
    debug: 
     msg: '{{ servicename }} is exists.' 
    when: servicestatus.stat.exists 

서비스의 상태를 확인하기위한 고유 한 방법은 없습니다. shell 모듈을 사용할 수 있습니다. 공지 사항 sudo을 사용했습니다. 사례가 다를 수 있습니다.

- name: check for service status 
    shell: sudo service {{ servicename }} status 
    ignore_errors: true 
    register: servicestatus 

    - name: Show service service status 
    debug: 
     msg: '{{ servicename }} exists.' 
    when: servicestatus.rc | int == 0 
+0

감사합니다. 나는 정확히 "with_items"가 정확히 무엇인지 모릅니다. 그래서, 나는 이전의 작업에서 많은 시간을 사용합니다. –

+0

나는 호스트 터미널에서 "apt-get remove nginx"명령을 사용하여 nginx 서비스를 제거하려고 시도합니다. 그러나 /etc/init.d에있는 "nginx"파일은 여전히 ​​존재합니다. 그래서, 내 작업은 여전히 ​​존재하는 출력을 가지고 있습니다. 책임자를 통해 존재하는 서비스를 확인하는 또 다른 방법이 있습니까? 감사. –

+0

@TutchaponSirisaeng이 답변에 대한 업데이트를 참조하십시오. – helloV