Below playbook gets the disk usage percentage of '/tmp' from a list of 
remote_hosts [20 remote servers] and stores it locally (delegate_to) in a 
file {{ playbook_dir }}/tmpfiles/stats.yml

hosts: remote_hosts
    - name: Generate JSON data
        path: "{{ playbook_dir }}/tmpfiles/stats.yml"
        line: "{{ inventory_hostname }}_{{ item.mount }}: {{ (100 * 
((item.size_total - item.size_available) / item.size_total)) | round(1, 
'common') }}"
        insertafter: EOF
      delegate_to: localhost
      when: item.mount == '/tmp'
      with_items: '{{ ansible_mounts }}'

I wish to get the target hostname_mountname in the stats.yml

Thus if the remote_hosts are

My stats.yml should have the below entries (Expected Output): 54
> 42
> 65

However, after using lineinfile and delegate_to it always prints localhost

localhost:/tmp: 54
> localhost_/tmp: 42
> localhost_/tmp: 65

I tried using {{ ansible_host }} instead of {{ inventory_hostname }} but it 
always prints localhost instead of the target from where it is fetching the 
disk usage information.

Note: if I remove delegate_to then it prints the remote IP fine but then I 
wish the file to be created locally and not on the remote host.

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 view this discussion on the web visit

Reply via email to