0

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 ?

Anthon
  • 69,918
  • 32
  • 186
  • 246

3 Answers3

2

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 }}"
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
0

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 }}"
Rishabh
  • 1,185
  • 1
  • 12
  • 28
0

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
techraf
  • 64,883
  • 27
  • 193
  • 198