service
online help: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html
offline help: ansible-doc service
## PLAYBOOK ansible-service.yml
---
- name: ansible service module - example 1
hosts: all
become: True
tasks:
- name: Ensure service is enabled and started
service:
name: vboxadd
enabled: yes
state: started
- name: Verify service status
shell: service vboxadd status | head -3
register: results1
- debug: msg="{{ results1.stdout_lines }}"
## - debug: vars=results1.stdout_lines # alternative way to output the variable registered
## OUTPUT
PLAY [ansible service module - example 1] *********************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************
ok: [mgmt7_int]
ok: [rhel6_int]
ok: [debian11_int]
ok: [rhel7_int]
ok: [mgmt8_int]
ok: [ubuntu20_int]
ok: [rhel8_int]
TASK [Ensure service is enabled and started] ******************************************************************************************************************
ok: [debian11_int]
ok: [rhel6_int]
ok: [mgmt7_int]
ok: [rhel7_int]
ok: [mgmt8_int]
ok: [ubuntu20_int]
ok: [rhel8_int]
TASK [Verify service status] **********************************************************************************************************************************
changed: [debian11_int]
changed: [rhel6_int]
changed: [rhel7_int]
changed: [mgmt7_int]
changed: [mgmt8_int]
changed: [ubuntu20_int]
changed: [rhel8_int]
TASK [debug] **************************************************************************************************************************************************
ok: [debian11_int] => {
"msg": [
"● vboxadd.service",
" Loaded: loaded (/opt/VBoxGuestAdditions-6.1.28/init/vboxadd; enabled; vendor preset: enabled)",
" Active: active (exited) since Tue 2021-10-26 08:59:34 +08; 7h ago"
]
}
ok: [mgmt7_int] => {
"msg": [
"● vboxadd.service",
" Loaded: loaded (/opt/VBoxGuestAdditions-6.0.22/init/vboxadd; enabled; vendor preset: disabled)",
" Active: active (exited) since Tue 2021-10-26 12:09:38 +08; 4h 14min ago"
]
}
ok: [mgmt8_int] => {
"msg": [
"● vboxadd.service",
" Loaded: loaded (/opt/VBoxGuestAdditions-6.0.22/init/vboxadd; enabled; vendor preset: disabled)",
" Active: active (exited) since Tue 2021-10-26 09:01:43 +08; 7h ago"
]
}
ok: [rhel6_int] => {
"msg": [
"The VirtualBox Additions are currently running."
]
}
ok: [rhel7_int] => {
"msg": [
"● vboxadd.service",
" Loaded: loaded (/opt/VBoxGuestAdditions-6.0.22/init/vboxadd; enabled; vendor preset: disabled)",
" Active: active (exited) since Tue 2021-10-26 14:52:36 +08; 1h 31min ago"
]
}
ok: [rhel8_int] => {
"msg": [
"● vboxadd.service",
" Loaded: loaded (/opt/VBoxGuestAdditions-6.0.22/init/vboxadd; enabled; vendor preset: disabled)",
" Active: active (exited) since Tue 2021-10-26 09:03:27 +08; 7h ago"
]
}
ok: [ubuntu20_int] => {
"msg": [
"● vboxadd.service",
" Loaded: loaded (/opt/VBoxGuestAdditions-6.1.10/init/vboxadd; enabled; vendor preset: enabled)",
" Active: active (exited) since Tue 2021-10-26 09:03:11 +08; 7h ago"
]
}
PLAY RECAP ****************************************************************************************************************************************************
debian11_int : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
mgmt7_int : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
mgmt8_int : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
rhel6_int : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
rhel7_int : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
rhel8_int : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ubuntu20_int : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
References
ansible.builtin plugins and modules index
ansible collections index
ansible loops
ansible conditionals
ansible playbook error handling
ansible playbook tests
ansible playbook blocks
ansible playbook roles
ansible playbook files
Please enable JavaScript to view the comments powered by Disqus.