cancel
Showing results for 
Search instead for 
Did you mean: 
dennishart
Mission Specialist
Mission Specialist
  • 4,317 Views

Issues with an jinja and magic variables script please help

Jump to solution

So I'm trying to combine magic variables and jinja. Unfortnately I'm running into issues. So This is the jinja script

{% for host in groups['all'] %}

{{ hostvars[host]['ansible_facts']['devices']['sda']['partitions']['sda1']['size'] }}

{% endfor %}

I run into this error 

"AnsibleUndefinedVariable: 'dict object' has no attribute 'devices'" }

This works when I target a group with one managed host but not one with multiple managedhosts. I have tried with hostvars['host'] and no luck...

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
dennishart
Mission Specialist
Mission Specialist
  • 4,290 Views

Thank you all the issue came to be I needed to use ansible_devices instead of devices. I appreciate all the help!

View solution in original post

4 Replies
Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 4,306 Views

I could be wrong but I believe that values nested at the second level and more cannot be accessed directly.

That is, you can invoke the top-level values (such as the hostname, the inventory name, etc.) but only the setup module can access the values that it returns.

You can also double check to make sure that devices is in the ansible_facts hash

andkra
Flight Engineer Flight Engineer
Flight Engineer
  • 4,293 Views

Hello,

add a new first play to your playbook where you gather the facts of the hosts which you need or use a fact cache.

 

Greetings
Andreas

dennishart
Mission Specialist
Mission Specialist
  • 4,291 Views

Thank you all the issue came to be I needed to use ansible_devices instead of devices. I appreciate all the help!

Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 4,281 Views

Glad to hear that you solved the issue @dennishart 

I like to work with JSON clients - my favorite on Mac is "Jayson" - I then use the debug module and browse the output to make sure I can access a value in a hash.

0 Kudos
Join the discussion
You must log in to join this conversation.