I'm trying to restart nginx in my ansible playbook if I changed the configuration. The configuration copy is done via template.

- name: update nginx.conf
  template: src=../templates/nginx_global_conf.conf.j2 dest=/etc/nginx/nginx.conf
  sudo: yes
  register: nginx_conf
- name: restart nginx if needed
  shell: service nginx restart
  when: nginx_conf.changed

Even if I'm getting:

TASK: [webapp | update nginx.conf] ********************************************
changed: [IPADDRESS]


TASK: [webapp | restart nginx if needed] **************************************
skipping: [IPADDRESS]

Tried also instead of shell:

action: service name=nginx state=restarted enabled=yes

It is not the problem. The register that isn't signalling is...

  • 2
    Some recommendations. Always use nginx_conf | changed instead of nginx_conf.changed. And use an appropriate module whenever possible. Only use shell/command when there is no module for what you want to do. But that won't help here. Did you try to debug your registered value? - debug: var= nginx_conf. An alternative for the registered value would be to use a handler instead: docs.ansible.com/ansible/… – udondan Feb 16 '16 at 15:50
  • You should run Ansible with -v switch to debug the issue. You should see the actual value of the variable in the run. – Henrik Pingel Feb 16 '16 at 16:19
  • When running with -v I'm getting TASK: [webapp | update nginx.conf] ******************************************** changed: [] => {"changed": true, "checksum": "a657b88cbe76abce4a2a409b177631427347f267", "dest": "/etc/nginx/nginx.conf", "gid": 0, "group": "root", "md5sum": "94f073a0116c76455540b73384a5b668", "mode": "0644", "owner": "root", "size": 1055, "src": "/home/user/.ansible/tmp/ansible-tmp-1455648101.98-37044083565097/source", "state": "file", "uid": 0} Don't see nginx_conf in there – Boaz Feb 16 '16 at 18:42

OK. Apparently I had the same nginx_conf in a different yml I included. Keeping the question here because there are quite some good tips. Thanks to all the helpers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.