How do I write Ansible task for ‘systemctl set-default graphical.target’ without shell/command modules
How do I write Ansible task for ‘systemctl set-default graphical.target’ without shell/command modules
https://stackoverflow.com/questions/64701241/how-do-i-write-ansible-task-for-systemctl-set-default-graphical-target-without
Asked 5 years, 1 month ago Modified 1 year, 8 months ago Viewed 9k times 7
I want to write a task in Ansible to perform the following command:
“systemctl set-default graphical.target” without using shell/command modules.
not sure that the “ansible.builtin.systemd” module has this option.
ansible Share Improve this question Follow edited Jan 29, 2021 at 21:36 β.εηοιτ.βε’s user avatar β.εηοιτ.βε 40.4k1414 gold badges8181 silver badges104104 bronze badges asked Nov 5, 2020 at 16:24 Ilana Polonsky’s user avatar Ilana Polonsky 7311 silver badge33 bronze badges There is an openned ticket for that: github.com/ansible/ansible/issues/65785 – Zeitounator CommentedNov 5, 2020 at 16:37 Add a comment 4 Answers Sorted by:
Highest score (default) 9
You can use the command module and use some when magic magic to have an idempotent solution for your problem. Actually there is no concrete module and the given change request for the systemd module was closed and is not merged.
-
name: “Get current systemd default” # noqa: command-instead-of-module ansible.builtin.command: “systemctl get-default” changed_when: false register: systemdefault
-
name: “Set default to graphical target” ansible.builtin.command: “systemctl set-default graphical.target” when: “‘graphical’ not in systemdefault.stdout” changed_when: true Of course this uses command. But there is no bad reason to not use command when you know, when you need to use it. It doesn’t look nice - ok. But at the end a custom module would also do the same under the hood and it could be a little bit to complex for such a “single” problem. Actually the “link” solution does the same, but its necessary to explicitly know the location of the link files (see Debian).
Share Improve this answer Follow edited Mar 19, 2024 at 12:25 NotTheDr01ds’s user avatar NotTheDr01ds 21.9k88 gold badges6666 silver badges9696 bronze badges answered May 11, 2022 at 5:53 TRW’s user avatar TRW 1,0341111 silver badges2626 bronze badges Sign up to request clarification or add additional context in comments.
Comments
6
When you execute systemctl set-default graphical.target you can see this log
Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target. Then you can use file module to create symlink as below
-
name: Change default target hosts: all become: yes gather_facts: no
tasks:
- name: Change default target to graphical.target file: src: /usr/lib/systemd/system/graphical.target dest: /etc/systemd/system/default.target state: link Share Improve this answer Follow answered Nov 5, 2020 at 16:35 gary lopez’s user avatar gary lopez 1,9841010 silver badges1717 bronze badges 2 Comments
njh Over a year ago On a Debian system the target files are stored in: /lib/systemd/system/ - is there a way of knowing where the directory for the targets are without hard-coding it?
TRW Over a year ago This is a bad solution. It uses the implementation details and is distro specific. I don’t understand why this is the excepted answer. It would be much better to use the “frontend” itself. Call systemctl get-default in Ansible and register the output. When the output contains not graphical, then call a second command with systemctl set-default. This works on all distributions 0
@njh I just spotted this when looking for something else. /lib/systemd contains the systemd files installed with APT, etc., local overrides are in /etc/systemd, which includes the default target, so if you want to change the default target, change the symlink as @gary-lopez suggests.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Sat Oct 23 20:00:36 2021 from 192.168.1.10 pi@penguin:~ $ ls -l /etc/systemd/system/default.target lrwxrwxrwx 1 root root 36 Feb 13 2020 /etc/systemd/system/default.target -> /lib/systemd/system/graphical.target pi@penguin:~ $ ls -l /lib/systemd/system/default.target lrwxrwxrwx 1 root root 16 Aug 6 17:38 /lib/systemd/system/default.target -> graphical.target pi@penguin:~ $ Share Improve this answer Follow answered Oct 24, 2021 at 9:04 tsgsh’s user avatar tsgsh 122 bronze badges Comments
0
I guess you want to avoid the command/shell modules, because - by default - they report changed, every time they are executed. But this can be avoided, if used like below.
-
name: Make sure, the graphical.target unit is the default. vars: cmdCheck: systemctl get-default | grep -q ^graphical.target$ cmdSet: systemctl set-default graphical.target ansible.builtin.shell: cmd: ‘{{ cmdCheck }} && exit 0; {{ cmdSet }}; {{ cmdCheck }} && exit 1; exit 2’ register: _ changed_when: _.rc == 1 failed_when: _.rc == 2 If you want to make sure, in addition, that the graphical.target is active:
-
name: Make sure, the graphical.target unit is active. vars: cmdCheck: systemctl status graphical.target cmdSet: systemctl isolate graphical.target ansible.builtin.shell: cmd: ‘{{ cmdCheck }} && exit 0; {{ cmdSet }}; {{ cmdCheck }} && exit 1; exit 2’ register: _ changed_when: _.rc == 1 failed_when: _.rc == 2 Share Improve this answer Follow edited Apr 26, 2024 at 9:49 answered Feb 13, 2024 at 11:22