I have a similar issue with a playbook which runs once a day using a
cron job on the Ansible control host. Probably once a week, I get the
follwoing error for a random server:
(..)
The following task failed for host ejpdxannnn-mgt.example.com:
template:{"dest": "/tmp/fragments.d/SLES.frag", "src":
"std-templates/std-playbook-make-inventory-by-os.j2", "group": "admin2",
"mode": "0400", "owner": "admin2"}
with the following message:
AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_hostname'
A complete dump of the error:
{"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable:
'dict object' has no attribute 'ansible_hostname'"}
Remenber that all variables staring with 'ansible_*' are gathered at the beginning of a play section in a playbook.
Your playbook contains several plays, each play will initiate fact gathering by using the setup module, unless the play section specifies no fact gathering, as you do.
If memory serves me well, the default behaviour for fact gathering between plays in one playbook changed a while ago.
I use the 'smart' gathering, so new hosts get facts gathered, while old ones don't
[defaults]
gathering = smart
http://docs.ansible.com/ansible/intro_configuration.html#gathering
(..)
Ansible version is 2.0.1.0. The playbook runs on 600 hosts with
forks=20. The Ansible control host has 16 GB RAM and if I run the
playbook manually there is no memory shortage (no swapping on Ansible
control host) and I can not reproduce the error.
It seems that every now and then, Ansible does not gather the facts for
all 600 hosts or the facts get lost on the Ansible control host due to
some unknown resource constraints. As far as I understand the concept,
Ansible
keeps all the facts in the control host's memory while the playbook is
run. For 600 hosts that is a lot of data.
I am guessing and presuming you are doing this for speed.
It might be worth looking at setting up your ansible host with fact caching in redis
For a redis setup that would be as following
[defaults]
fact_caching = redis
fact_caching_timeout = 3600
fact_caching_connection = localhost:6379:0
I have used this in the case of a +/- 500 host setup with a mgmt host of 4GB works fine, and reduces ram, while speeding up most operations
If you want them on your disk you could setup file based caching, however for 600 hosts that might be slow.
For a file setup that would be as following
[defaults]
fact_caching = jsonfile
fact_caching_timeout = 3600
fact_caching_connection = /tmp/ansible/facts/cache
The playbook is:
admin2@xxxxx:/data/ansible/playbooks> cat
std-playbook-make-inventory-by-os.yml
---
- hosts: all
gather_facts: True
tasks:
- set_fact:
path_to_fragments: /tmp/fragments.d
file_inventory: /data/ansible/inventory/inventory.os
- hosts: all[0]
gather_facts: False
Here is you problem