I discovered this "feature" today and found it rather perplexing. I've used 
the workaround of 'gather_facts: yes' to fix it in this case, but it would 
be nice to be able to pre-populate the hostvars array with playbook and/or 
command-line variables instead of having to rely on the fact gathering 
"hack."

On Wednesday, October 23, 2013 11:51:25 AM UTC-7, Michael DeHaan wrote:
>
> I suspect you probably haven't talked to that host yet previously.
>
> The solution would be to make sure you talk to the host in a previous 
> play, even if that play contains no other facts than fact gathering.
>
> (FWIW, fact caching is something we're looking to add in a future release)
>
>
>
>
> On Wed, Oct 23, 2013 at 9:48 AM, Kesten Broughton <[email protected] 
> <javascript:>> wrote:
>
>> Can anybody explain why I can access ansible_default_ipv4 in a task, but 
>> not in a jinja2 file?
>>  
>>
>>  - debug: msg="{{ hostvars[inventory_hostname]['ansible_default_ipv4'] }}"
>> This gives an ipv4 address like 10.10.10.10
>>
>> In roles/common/tasks/main.yml
>>
>>  - name: Template /etc/hosts using inventory default ipv4
>>    template: src=hosts.j2 dest=/etc/hosts
>>
>> TASK: [common | Template /etc/hosts using inventory default ipv4] 
>> ************* 
>> fatal: [Ubuntu_Cluster_02] => {'msg': "One or more undefined variables: 
>> 'dict object' has no attribute 'ansible_default_ipv4'", 'failed': True}
>>
>> I checked with a dump.j2 template that all my hosts do have the 
>> ansible_default_ipv4 field.
>>
>> Here is the hosts.j2
>>
>> # {{ansible_managed}}
>> 127.0.0.1 localhost
>> {% for host in groups['all'] %}
>>
>> {{ hostvars[host]["ansible_default_ipv4"]["address"]   }}   {{ host }}
>>
>> {%  endfor %}
>>  
>>  -- 
>> 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] <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
>
> -- 
> Michael DeHaan <[email protected] <javascript:>>
> CTO, AnsibleWorks, Inc.
> http://www.ansibleworks.com/
>
>  

-- 
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/7249a38a-db9f-440c-9eca-d931236a7888%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to