alex sundukovskiy
alex sundukovskiy

Reputation: 493

Ansible error change handler is not defined

I'm trying to run my first playbook to install Java on four servers and subsequently define a JAVA_HOME environment variable.

ansible-playbook site.yml --check
PLAY [crave_servers] **********************************************************

GATHERING FACTS ***************************************************************
ok: [54.174.151.196]
ok: [54.174.197.35]
ok: [54.174.207.83]
ok: [54.174.208.240]

TASK: [java | install Java JDK] ***********************************************
changed: [54.174.197.35]
changed: [54.174.151.196]
changed: [54.174.208.240]
changed: [54.174.207.83]
ERROR: change handler (setvars) is not defined

I've placed my site.yml under /etc/ansible

---
- hosts: crave_servers
  remote_user: ubuntu
  sudo: yes
  roles:
  - java

I've placed main.yml under /etc/ansible/java/tasks

---
- name: install Java JDK
  apt: name=default-jdk state=present
  notify:
     - setvars

I've placed main.yml under /etc/ansible/handlers

---
- name: setvars
  shell:  echo "JAVA_HOME=\"/usr/lib/jvm/java-7-openjdk-amd64\"" >> /etc/environment

Now I'm not sure if the syntax is structure of my handlers is correct. But it's obvious from the output that Ansible is able to find the correct role and execute the correct task. But the task can't find the handler. Nobody else seems to have the same problem. And I don't really know how to debug it because my ansible version seems to be missing the config file.

Upvotes: 1

Views: 4140

Answers (1)

ProfHase85
ProfHase85

Reputation: 12183

You should put your handler to /etc/ansible/java/handlers/main.yml

As handlers are part of a role.

Remarks:

  • You should not use your handler as it would paste the line into /etc/environment each time you call this playbook. I would recommend the lineinefile module.

  • You should reconsider your decision to put ansible playbooks into /etc

Upvotes: 3

Related Questions