jean553
jean553

Reputation: 733

Ansible service restart does not work

I use Ansible in order to provision a Docker container with Vagrant.

My Ansible file contains the following section to start nginx:

- name: nginx started
  service:
    name: nginx
    state: restarted

From what I understand, this section should restart nginx in all cases, but when I connect via SSH to the container, nginx is not running (the whole Ansible provision process succeeds, there is no log for nginx in /var/log/nginx/error.log). It starts correctly when I manually type the following command: sudo service nginx start.

However, it works when I replace the section above by:

- name: nginx restarted
  command: service nginx restart

It seems the issue is not limited to nginx, and also happens with other services like syslog-ng.

Any idea why using Ansible service does not work ? (docker 17.10.0, Vagrant 2.0.1, Ansible 2.4.0)

Upvotes: 2

Views: 9158

Answers (2)

zigarn
zigarn

Reputation: 11605

Ansible service module try to guess the underlying init system.
In your case of the phusion/baseimage docker image, it finds /sbin/initctl, so the module simply launch /sbin/initctl stop nginx; /sbin/initctl start nginx inside the container which does nothing as the init system is changed in this image (my_init).

So the problem is the inconsistent init system state of the image that ansible doesn't detect correctly.

The solutions are:

  • write a my_init ansible module (service module try first to use the {{ ansible_service_mgr }} module [code])
  • remove initctl from the image, so ansible will not detect any init system and will use the service command (maybe raise an issue in phusion/baseimage-docker)
  • use the command module to explicitly use service command as you finally did

Upvotes: 2

juanlumn
juanlumn

Reputation: 7105

Please take a look into this ansible issue I think it may be related.

According to them:

...this is not service specific. The restarted/reloaded options are currently simple options that call the underlying init systems scripts, which (if successful) return success...

So, probably that's why even in Ansible documentation you can find an example of what you were trying to achieve:

# Example action to restart service httpd, in all cases
- service:
    name: httpd
    state: restarted

But for nginx doesn't work as it may not be supported.

Appart from your solution:

- name: nginx restarted
  command: service nginx restart

You can achieve that by using:

- name: restart nginx
  service: name=nginx state=restarted

Upvotes: 0

Related Questions