Know Your Tech
Know Your Tech

Reputation: 33

Variable is undefined error when passing argument to Ansible roles

I am new to ansible roles here is what i am doing.

more site.yml

---

- hosts: user1_checkserverlist
  user: user1
  roles:
    - { role: speed_manager, DNAME: "/u" }

- hosts: oracle_checkserverlist
  user: oracle
  roles:
    - { role: speed_manager, DNAME: "/tmp" }

more speed_manager/defaults/main.yml

DNAME: ""

more speed_manager/tasks/main.yml

---

- include: check.yml diskname={{DNAME}}

more speed_manager/tasks/check.yml

---

   - name: ==== Reading Variable ====
     ping:
     vars:
         dn: "{{ diskname }}"

   - name: ====Sync  to remote hosts ====

     synchronize: src="/web/roles/speed_manager/files/" dest="/tmp/mohtt/"

   - name: ====Execute shell=============

     shell: /tmp/mohtt/fixwebserver.sh {{ dn }} chdir=/tmp/mohtt

This is how i execute the roles

ansible-playbook -vvv site.yml -i /web/hostfiles/myhost.txt

Error:

TASK [check_bk_speed_manager : ====Execute shell=============] *****************
task path: /web/roles/speed_manager/tasks/check.yml:25
fatal: [myserver.com]: FAILED! => {"failed": true, "msg": "'dn' is undefined"}
fatal: [myserver.com]: FAILED! => {"failed": true, "msg": "'dn' is undefined"}
        to retry, use: --limit @check_bk_speed.retry

I was expecting the value of dn to be "/u"

Can you point out where am i going wrong and if this is a bug ?

Upvotes: 0

Views: 1108

Answers (3)

techraf
techraf

Reputation: 68509

You don't actually need to use a different name for the variable that you already have defined.

You can change your tasks to use diskname, which you pass in the include task:

- name: ====Execute shell=============
  shell: /tmp/mohtt/fixwebserver.sh {{ diskname }} chdir=/tmp/mohtt

Actually, if this is the whole code, you can go with DNAME:

- name: ====Execute shell=============
  shell: /tmp/mohtt/fixwebserver.sh {{ DNAME }} chdir=/tmp/mohtt

And include with just:

- include: check.yml

Upvotes: 0

Rishabh
Rishabh

Reputation: 1195

In speed_manager/tasks/check.yml instead of dn use diskname as the variable or in the beginning of the file add

- set_fact:
  dn: "{{ diskname }}"

Upvotes: 0

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

In your example you define dn only for task "Reading Variable".
Other tasks know nothing about dn.

If you want dn to be available for subsequent tasks, you can use:

- set_fact:
    dn: "{{ diskname }}"

Upvotes: 2

Related Questions