FreeKB - Ansible service module (start stop restart services chkconfig)
Ansible - service module (start stop restart services chkconfig)

The service module is used to start, stop, restart, reload, enable and disable init services on the managed node (e.g. the target system). Or, the local_action module can be used to invoke this module on the control node (that's your Ansible server). This is similar to using the chkconfig command. The become module is used to run the command as root.

The ansible-doc service command can be used to show the Ansible documention on the service module.

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |