如何检查 Ansible 是否存在任何服务?
How to check any services exists by 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
所以,我认为问题出在使用涉及的条件时的stat模块。
为什么要使用 with_items
?您打算通过多项服务?这很重要,因为如果您使用 with_items
,结果将是一个列表。只需删除 with_items
即可。如果要传递多个服务,则必须循环 with_items
并使用 item
而不是 servicename
.
- 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
Ansible 中没有本地方法来检查服务的状态。您可以使用 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
我想检查终端主机中是否存在该服务。
所以,我只是做了下面的剧本。
---
- 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
所以,我认为问题出在使用涉及的条件时的stat模块。
为什么要使用 with_items
?您打算通过多项服务?这很重要,因为如果您使用 with_items
,结果将是一个列表。只需删除 with_items
即可。如果要传递多个服务,则必须循环 with_items
并使用 item
而不是 servicename
.
- 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
Ansible 中没有本地方法来检查服务的状态。您可以使用 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