Re: [ansible-project] Unable to get remote target hostname when using delegate_to

2020-03-26 Thread Kai Stian Olstad
On Wed, Mar 25, 2020 at 10:41:35PM -0700, Shifa Shaikh wrote:
> ---
> hosts: remote_hosts
>   tasks:
> - name: Generate JSON data
>   lineinfile:
> 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
> 
> 10.0.0.2
> > 10.0.0.3
> > 10.0.0.5
> 
> 
> My stats.yml should have the below entries (Expected Output):
> 
> 10.0.0.2_/tmp: 54
> > 10.0.0.3_/tmp: 42
> > 10.0.0.5_/tmp: 65
> 
> 
> However, after using lineinfile and delegate_to it always prints localhost
> 
> localhost:/tmp: 54
> > localhost_/tmp: 42
> > localhost_/tmp: 65

Delegate to localhost does not change inventory_hostname, if it did it would 
break a lot of code out there.
And I can prove it with this code that mimic you codes behavior.

$ more test.yml
---
- hosts: a1,a2
  gather_facts: no
  become: no
  vars:
myvar:
  - value1
  - value2
  - value3
  tasks:
- lineinfile:
path: /tmp/test
line: "{{ inventory_hostname }} {{ item }}"
insertafter: EOF
create: true
  delegate_to: localhost
  when: item == "value2"
  with_items: "{{ myvar }}"

And by running it I get

$ ansible-playbook test.yml; echo "Content of /tmp/test:"; cat /tmp/test

PLAY [a1,a2] ***

TASK [lineinfile] **
skipping: [a1] => (item=value1) 
skipping: [a2] => (item=value1) 
changed: [a1 -> localhost] => (item=value2)
skipping: [a1] => (item=value3) 
changed: [a2 -> localhost] => (item=value2)
skipping: [a2] => (item=value3) 

PLAY RECAP *
a1 : ok=1changed=1unreachable=0failed=0
a2 : ok=1changed=1unreachable=0failed=0

Content of /tmp/test:
a1 value2
a2 value2


-- 
Kai Stian Olstad

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200326090545.qmkhcis2tyu7yv3p%40olstad.com.


[ansible-project] Unable to get remote target hostname when using delegate_to

2020-03-25 Thread Shifa Shaikh
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
  tasks:
- name: Generate JSON data
  lineinfile:
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

10.0.0.2
> 10.0.0.3
> 10.0.0.5


My stats.yml should have the below entries (Expected Output):

10.0.0.2_/tmp: 54
> 10.0.0.3_/tmp: 42
> 10.0.0.5_/tmp: 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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/ecf07c8c-83c4-4504-ba3f-a2f7975bf1a3%40googlegroups.com.