Using playbook to start, stop or restart a service

2,493 views
Skip to first unread message

Rick O'Shea

unread,
May 1, 2016, 4:49:44 PM5/1/16
to Ansible Project
We have host groups whose service states we wish to automate.  

- name manage service
  service: name=foo state={{state}}

As near as we can tell this is the only way to automate a playbook to start, stop, restart, enable, disable or status a service is to pass a variable on the command line:

ansible-playbook foo-service.yml -e state=started

Is this best practice?



Uditha Desilva

unread,
May 3, 2016, 3:20:36 AM5/3/16
to Ansible Project
The variable can be defined however you wish -- I'm not quite sure why you think it can only be passed from the command line?

On the other hand, if all you want to do is to start a service from the command line, you can invoke the service module via the ansible command itself:

ansible -m service -e 'name=foo state=started'

without having to create a playbook to do it.

Johannes Kastl

unread,
May 3, 2016, 3:21:20 AM5/3/16
to ansible...@googlegroups.com
On 01.05.16 22:49 Rick O'Shea wrote:

> As near as we can tell this is the only way to automate a playbook to
> start, stop, restart, enable, disable or status a service is to pass a
> variable on the command line:
>
> ansible-playbook foo-service.yml -e state=started
>
> Is this best practice?

I would say putting these into the handler section of your
role/playbook would be preferable, so you only restart the service if
something has changed. Unless you want to force the restart.

Remember ansible is idempotent, so starting a service that is already
started does not harm, but does not restart it either.

Johannes

signature.asc

rup

unread,
May 3, 2016, 11:36:26 AM5/3/16
to Ansible Project
But using the service module isn't quite that simple, right?  You have to set up your init script to identify the services that the module would call.  That's where I'm at currently.
Reply all
Reply to author
Forward
0 new messages