| 1 |
# Test code for the systemd module. |
| 2 |
# (c) 2017, James Tanner <[email protected]> |
| 3 |
|
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
## |
| 20 |
## systemctl |
| 21 |
## |
| 22 |
|
| 23 |
- name: End if this system does not use systemd |
| 24 |
meta: end_host |
| 25 |
when: ansible_facts.service_mgr != 'systemd' |
| 26 |
|
| 27 |
- name: Include distribution specific variables |
| 28 |
include_vars: "{{ lookup('first_found', params) }}" |
| 29 |
vars: |
| 30 |
params: |
| 31 |
files: |
| 32 |
- "{{ ansible_facts.distribution }}.yml" |
| 33 |
- "{{ ansible_facts.os_family }}.yml" |
| 34 |
- default.yml |
| 35 |
paths: |
| 36 |
- vars |
| 37 |
|
| 38 |
- name: get a list of running services |
| 39 |
shell: systemctl | fgrep 'running' | awk '{print $1}' | sed 's/\.service//g' | fgrep -v '.' | egrep ^[a-z] |
| 40 |
register: running_names |
| 41 |
- debug: var=running_names |
| 42 |
|
| 43 |
- name: check running state |
| 44 |
systemd: |
| 45 |
name: "{{ running_names.stdout_lines|random }}" |
| 46 |
state: started |
| 47 |
register: systemd_test0 |
| 48 |
- debug: var=systemd_test0 |
| 49 |
- name: validate results for test0 |
| 50 |
assert: |
| 51 |
that: |
| 52 |
- 'systemd_test0.changed is defined' |
| 53 |
- 'systemd_test0.name is defined' |
| 54 |
- 'systemd_test0.state is defined' |
| 55 |
- 'systemd_test0.status is defined' |
| 56 |
- 'not systemd_test0.changed' |
| 57 |
- 'systemd_test0.state == "started"' |
| 58 |
|
| 59 |
- name: the module must fail when a service is not found |
| 60 |
systemd: |
| 61 |
name: '{{ fake_service }}' |
| 62 |
state: stopped |
| 63 |
register: result |
| 64 |
ignore_errors: yes |
| 65 |
|
| 66 |
- assert: |
| 67 |
that: |
| 68 |
- result is failed |
| 69 |
- result is search("Could not find the requested service " + fake_service) |
| 70 |
|
| 71 |
- name: the module must fail in check_mode as well when a service is not found |
| 72 |
systemd: |
| 73 |
name: '{{ fake_service }}' |
| 74 |
state: stopped |
| 75 |
register: result |
| 76 |
check_mode: yes |
| 77 |
ignore_errors: yes |
| 78 |
|
| 79 |
- assert: |
| 80 |
that: |
| 81 |
- result is failed |
| 82 |
- result is search("Could not find the requested service " + fake_service) |
| 83 |
|
| 84 |
- name: check that the module works even when systemd is offline (eg in chroot) |
| 85 |
systemd: |
| 86 |
name: "{{ running_names.stdout_lines|random }}" |
| 87 |
state: started |
| 88 |
environment: |
| 89 |
SYSTEMD_OFFLINE: 1 |
| 90 |
|
| 91 |
- name: Disable ssh 1 |
| 92 |
systemd: |
| 93 |
name: '{{ ssh_service }}' |
| 94 |
enabled: false |
| 95 |
register: systemd_disable_ssh_1 |
| 96 |
|
| 97 |
- name: Disable ssh 2 |
| 98 |
systemd: |
| 99 |
name: '{{ ssh_service }}' |
| 100 |
enabled: false |
| 101 |
register: systemd_disable_ssh_2 |
| 102 |
|
| 103 |
- name: Enable ssh 1 |
| 104 |
systemd: |
| 105 |
name: '{{ ssh_service }}' |
| 106 |
enabled: true |
| 107 |
register: systemd_enable_ssh_1 |
| 108 |
|
| 109 |
- name: Enable ssh 2 |
| 110 |
systemd: |
| 111 |
name: '{{ ssh_service }}' |
| 112 |
enabled: true |
| 113 |
register: systemd_enable_ssh_2 |
| 114 |
|
| 115 |
- assert: |
| 116 |
that: |
| 117 |
- systemd_disable_ssh_2 is not changed |
| 118 |
- systemd_enable_ssh_1 is changed |
| 119 |
- systemd_enable_ssh_2 is not changed |
| 120 |
|
| 121 |
- import_tasks: test_unit_template.yml |
| 122 |
- import_tasks: test_indirect_service.yml |
| 123 |
- import_tasks: test_enabled_runtime.yml |
| 124 |
- import_tasks: test_systemd_version.yml |
| 125 |
- import_tasks: test_mask.yml |