Search code examples
ansible

How do I write Ansible task for 'systemctl set-default graphical.target' without shell/command modules


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.


Solution

  • 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