Reputation: 51
I am trying to Stop and/or Disable a systemd service if it is running and/or enabled on the remote host.
Contents of tasks/main.yml:
---
- name: Populate service facts
service_facts:
- name: Display selected service
debug:
var: ansible_facts.services[serviceToDisplay]
loop: "{{ disable_services }}"
loop_control:
loop_var: serviceToDisplay
vars:
disable_services:
- cups.service
- cups
- name: Stop and Disable service if it is running or enabled
systemd:
name: cups.service
enabled: false
state: stopped
when:
- "serviceToDisable is defined"
- "serviceToDisable.status == 'enabled'"
loop: "{{ disable_services }}"
loop_control:
loop_var: serviceToDisable
vars:
disable_services:
- cups.service
- cups
become: true
...
Results:
PLAY [configServerGroup] *************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [radicale.jlhimpel.net]
TASK [example : Populate service facts] **********************************************************************************************************************
ok: [radicale.jlhimpel.net]
TASK [example : Display selected service] ********************************************************************************************************************
ok: [radicale.jlhimpel.net] => (item=cups.service) => {
"ansible_facts.services[serviceToDisplay]": {
"name": "cups.service",
"source": "systemd",
"state": "running",
"status": "enabled"
},
"ansible_loop_var": "serviceToDisplay",
"serviceToDisplay": "cups.service"
}
ok: [radicale.jlhimpel.net] => (item=cups) => {
"ansible_facts.services[serviceToDisplay]": {
"name": "cups",
"source": "sysv",
"state": "running"
},
"ansible_loop_var": "serviceToDisplay",
"serviceToDisplay": "cups"
}
TASK [example : Stop and Disable service if it is running or enabled] ****************************************************************************************
fatal: [radicale.jlhimpel.net]: FAILED! => {"msg": "The conditional check 'serviceToDisable.status == 'enabled'' failed. The error was: error while evaluating conditional (serviceToDisable.status == 'enabled'): 'str object' has no attribute 'status'\n\nThe error appears to be in '/home/jwhimpel/ansible/roles/example/tasks/main.yml': line 16, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Stop and Disable service if it is running or enabled\n ^ here\n"}
PLAY RECAP ***************************************************************************************************************************************************
radicale.jlhimpel.net : ok=3 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Ansible version: 2.9.14-1.fc32 on Fedora
How should I go about checking if the status == enabled or the state == running?
Note: The service may optionally not be present in ansible_facts.service. It may also optionally not be running and/or not enabled. I am attempting to make the tasks idempotent.
Thanks
Upvotes: 1
Views: 8069
Reputation: 6685
Your second task should be as below, please notice the name
clause and the when
section:
- name: Stop and Disable service if it is running or enabled
systemd:
name: "{{ ansible_facts.services[serviceToDisable]['name'] }}"
enabled: false
state: stopped
when:
- serviceToDisable is defined
- ansible_facts.services[serviceToDisable]['status'] == 'enabled'
loop: "{{ disable_services }}"
loop_control:
loop_var: serviceToDisable
vars:
disable_services:
- cups.service
- cups
become: true
Not sure why you have defined two times the same service (cups.service
and cups
), but I guess you have your good reasons.
Upvotes: 4