Bootstrap FreeKB - Ansible - start stop restart enable disable systemd services
Ansible - start stop restart enable disable systemd services

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The systemd module can be used to start, stop, restart, reload, enable, and disable systemd services.

  • daemon_reload is used to ensure you are working with the current services
  • service_facts is used to determine if the service exists and if the service is currently running or stopped
  • become is used to run the command as root.

In this example, the nginx web server will be stopped and disabled.

---
- hosts: all
  tasks:
  - name: reload systemd daemons
    systemd:
      daemon_reload: yes

  - name: gather service facts
    ansible.builtin.service_facts:

  - name: stop nginx
    become: yes
    become_user: root
    systemd:
      name: nginx
      state: stopped
      enabled: no
    when: ansible_facts.services['nginx.service'] is defined and ansible_facts.services['nginx.service'].state != 'stopped'
...

 

In this example, the nginx web server will be started and enabled.

---
- hosts: all
  tasks:
  - name: reload systemd daemons
    systemd:
      daemon_reload: yes

  - name: gather service facts
    ansible.builtin.service_facts:

  - name: start nginx
    become: yes
    become_user: root
    systemd:
      name: nginx
      state: started
      enabled: yes
    when: ansible_facts.services['nginx.service'] is defined and ansible_facts.services['nginx.service'].state != 'running'
...

 

Or to restart.

---
- hosts: all
  tasks:
  - name: reload systemd daemons
    systemd:
      daemon_reload: yes

  - name: gather service facts
    ansible.builtin.service_facts:

  - name: restart nginx
    become: yes
    become_user: root
    systemd:
      name: nginx
      state: restarted
    when: ansible_facts.services['nginx.service'] is defined
...

 

If you make a change to a service, such as starting, stopping, enabling, disabling, installing, or removing, daemon_reload should be used so that systemd has the current state of services.

- name: reload systemd daemons
  systemd:
    daemon_reload: yes

 

If the service was successfully stopped, started, restated, or reloaded, the play should indicate changed.

TASK [stop nginx]
changed: [server1.example.com]

 


Masked

The masked option can be used to mask (or unmask) a service. When masked, it is impossible to start the service.

- name: stop, disable and mask nginx
  systemd:
    name: nginx
    state: stopped
    enabled: false
    masked: true

 


Arguments

Additional arguments can be included. For example, with the service command, here is how you would include the "foo" argument.

systemctl start nginx foo

 

And here is how to do the same in Ansible.

- name: stop nginx
  systemd:
    name: nginx
    state: started
    enabled: yes
    arguments: foo
  become: yes
  become_user: root

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 65c7c2 in the box below so that we can be sure you are a human.