Hi Evrybody, 
I'm facing to a problem that I don't understand the origin:  Any help will 
be welcome. 

I want to use in a playbook a role where the name depends on a variable 
defined in the inventory. 
 
I have the following dynamic inventory for a host: 

the command ansible-inventory -i /etc/ansible/cobbler.py --host 
"rhel8-test-2"  returns: 

{
    "cobbler": {  
         ...
        "image": "",
        "ks_meta": {},
        "mgmt_classes": [
            "online",
        ],
        "name": "rhel8-test-2",
        "netboot_enabled": true,
        "power_user": "",
        "profile": "HWCL_402"
    }
}

In my playbook I want to acess to the profile variable   =>    
 "cobbler.profile": "HWCL_402"
The following playbook works fine: 

---
- name: " print profile"
  hosts: "{{ myhost }}"
  gather_facts: no

  vars:
    PROFILE: "{{ hostvars[inventory_hostname].cobbler.profile }}"

  tasks:

  - name: print profile value defined in inventory
    ansible.builtin.debug:
      msg: "{{  PROFILE }}"


This  playbook prints the correct value of my variable: 
ansible-playbook -i /etc/ansible/cobbler.py    playbook2.yml -e 
"myhost=rhel8-test-2"

PLAY [print profile] 
*******************************************************************************************
TASK [print profile value defined in inventory] 
*******************************************************************************************
ok: [rhel8-test-2] => {
    "msg": "HWCL_402"
}
...


Now, , I add the call to a role ( echo )  where the path depends  on 
profile value:  
-> the role is under HWCL_402/echo

---
- name: " use profile"
  hosts: "{{ myhost }}"
  gather_facts: no

  vars:

    PROFILE: "{{ hostvars[inventory_hostname].cobbler.profile }}"

  tasks:

  - name: print profile value defined in inventory
    ansible.builtin.debug:
      msg: "{{  PROFILE }}"

  
*roles:*
  *  - "{{ PROFILE }}/echo"*

the execution of this modifified playbook returns immediatly : 

*ERROR! {{ hostvars[inventory_hostname].cobbler.profile }}:** 'hostvars' is 
undefined*

what I did wrong ?  why hostvars is undefined ? it was correct in the 
previous example ? 
If I declare PROFILE  in a simple variable  ( not in the inventory) it 
'works also fine ! 

Best regards. 
Philippe. 

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/91d7f4bf-2656-4f8f-9c38-d9f594d9e8f4n%40googlegroups.com.

Reply via email to