Bootstrap FreeKB - Ansible - start stop restart services
Ansible - start stop restart services

Updated:   |  Ansible articles

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

The service module is used to start, stop, restart, reload, enable and disable init services on the managed node (e.g. the target system).

This is similar to using the chkconfig command. The become module is used to run the command as root.

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

- name: stop nginx
  service:
    name: nginx
    state: stopped
    enabled: no
  become: yes
  become_user: root

 

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

- name: stop nginx
  service:
    name: nginx
    state: started
    enabled: yes
  become: yes
  become_user: root

 

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

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

 


Arguments

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

service nginx start foo

 

And here is how to do the same in Ansible.

- name: stop nginx
  service:
    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 31cc1c in the box below so that we can be sure you are a human.