Re: [ansible-project] Ansible ping report

2023-11-08 Thread Vladimir Botka
On Wed, 8 Nov 2023 06:22:49 -0800 (PST)
Dimitri Yioulos  wrote:

> ... my goal is to see if hosts are Ansible-pingable (not
> ICMP-pingable; I believe there's a difference) or not, then take
> *all* of the output, and create a report from it. Thus, i end up
> with a report of hosts that are either Ansible-pingable or not.

Dimitri, the play does what you want. The code is available also here
https://gist.github.com/vbotka/10c57962976dd1e2dd3e9411d3745c75

Let me help you to understand it step by step

>>> - hosts: test_01,test_05,test_06,test_07 
>>>   gather_facts: false 

There are 4 hosts in the play. Setup is off, hence no connections to
the remote host up till now

>>> vars: 
>>>
>>>   h_unr: "{{ dict(ansible_play_hosts_all| 
>>>   zip(ansible_play_hosts_all| 
>>>   map('extract', hostvars, 'unr'))) }}" 

This dictionary will be evaluated when referenced in the debug task

>>> tasks: 
>>>
>>>   - block: 
>>>   - ping: 
>>> register: out 
>>>   - set_fact: 
>>>   unr: "{{ out.unreachable|d(false) }}" 
>>> ignore_unreachable: true 

Here come the first connections to the remote hosts. There are 2
tasks in the block. The first one is the Ansible module ping, not the
ICMP ping. (Yes, you're right. There is a difference.) The second one
is the module set_fact. Because of the ignore statement the tasks in
the block won't fail if a host is unreachable.

If a host can be reached by ping there is no attribute *unreachable*
in the registered dictionary *out*. Therefor, the value of the
variables *unr* will be the default (alias d) value *false*.

If a host can't be reached the value of the attribute *unreachable*
is *true*.

>>>   - debug: 
>>>   var: h_unr 
>>> run_once: true 
>>> delegate_to: localhost 

Here is the report. Let's analyse the dictionary *h_unr*. In the
above block, all hosts created the variable *unr*. When you take the
list of all hosts in the play *ansible_play_hosts_all* [1] and *map*
the function to *extract* the *hostvars* [2] variable *unr*
  
  ansible_play_hosts_all|map('extract', hostvars, 'unr')

you get a list, for example, where only the first host was reached

  [false, true, true, true]

Then, *zip* this list with *ansible_play_hosts_all*

  [[test_01,false], [test_02,true], [test_03,true], [test_04,true]]

and apply the function *dict* [3]. Below is the expected result.
Enjoy!

>>> gives (abridged) 
>>>
>>> ok: [test_01 -> localhost] => 
>>> h_unr: 
>>>   test_01: false 
>>>   test_05: true 
>>>   test_06: true 
>>>   test_07: true 

[1]
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html
[2]
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#selecting-values-from-arrays-or-hashtables
[3]
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#combining-items-from-multiple-lists-zip-and-zip-longest

-- 
Vladimir Botka

-- 
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/20231108220631.48c0146c%40gmail.com.


pgprBG_jdOr9m.pgp
Description: OpenPGP digital signature


Re: [ansible-project] Ansible ping report

2023-11-08 Thread Thanh Nguyen Duc
You may try the attached playbook i am using for linux, windows will be
similar as well. The report will be in html format

Vào Th 4, 8 thg 11, 2023 vào lúc 22:22 Dimitri Yioulos <
dimitri.g.yiou...@gmail.com> đã viết:

> Vladimir, this is very good, and very appreciated. Your playbook looks for
> hosts that are pingable, and marks them as not unreachable.  But, my goal
> is to see if hosts are Ansible-pingable (not ICMP-pingable; I believe
> there's a difference) or not, then take *all* of the output, and create a
> report from it. Thus, i end up with a report of hosts that are either
> Ansible-pingable or not.
>
> On Tuesday, November 7, 2023 at 2:15:30 PM UTC-5 Vladimir Botka wrote:
>
>> Sure, here is the gist
>> https://gist.github.com/vbotka/10c57962976dd1e2dd3e9411d3745c75
>>
>> (Is it possible to reasonably format a code in Google Groups?)
>> On Tuesday, November 7, 2023 at 6:55:50 PM UTC+1 dbs34 wrote:
>>
>>> would you mind sharing the whole, newly updated playbook?  thank you!
>>>
>>> On Monday, November 6, 2023 at 2:58:31 PM UTC-5 Vladimir Botka wrote:
>>>
 On Fri, 3 Nov 2023 08:40:19 -0700 (PDT)
 Dimitri Yioulos  wrote:

 > --- PING REPORT ---
 > {% for pr in ping_result.results %}
 > {{ pr.stdout_lines | first }}
 > {{ pr.stdout_lines | last }}
 >
 > {% endfor %}
 > run_once: true
 > delegate_to: localhost
 >
 > That works fine. However, I want to use ansible.builtin.ping ...


 Ignore unreachable hosts in a block

 - block:
 - ping:
 register: out
 - set_fact:
 unr: "{{ out.unreachable|d(false) }}"
 ignore_unreachable: true

 and declare the dictionary

 h_unr: "{{ dict(ansible_play_hosts_all|
 zip(ansible_play_hosts_all|
 map('extract', hostvars, 'unr'))) }}"

 For example, the below play

 - hosts: test_01,test_05,test_06,test_07
 gather_facts: false

 vars:

 h_unr: "{{ dict(ansible_play_hosts_all|
 zip(ansible_play_hosts_all|
 map('extract', hostvars, 'unr'))) }}"

 tasks:

 - block:
 - ping:
 register: out
 - set_fact:
 unr: "{{ out.unreachable|d(false) }}"
 ignore_unreachable: true

 - debug:
 var: h_unr
 run_once: true
 delegate_to: localhost

 gives (abridged)

 ok: [test_01 -> localhost] =>
 h_unr:
 test_01: false
 test_05: true
 test_06: true
 test_07: true

 --
 Vladimir Botka

>>> --
> 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/47bbdd8e-7b5f-4dfc-ac60-375a532a9adbn%40googlegroups.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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CA%2BnV637NJgVD2dmR15fdUHf6eGfdM%2B-AZxao_S6fPEeu-%3D0mxA%40mail.gmail.com.


exportreport.yml
Description: Binary data


linux_ping.j2
Description: Binary data


general.yml
Description: Binary data


createreport.yml
Description: Binary data


Daily_LinuxPing.yml
Description: Binary data


Re: [ansible-project] Ansible ping report

2023-11-08 Thread Dimitri Yioulos
Vladimir, this is very good, and very appreciated. Your playbook looks for 
hosts that are pingable, and marks them as not unreachable.  But, my goal 
is to see if hosts are Ansible-pingable (not ICMP-pingable; I believe 
there's a difference) or not, then take *all* of the output, and create a 
report from it. Thus, i end up with a report of hosts that are either 
Ansible-pingable or not.

On Tuesday, November 7, 2023 at 2:15:30 PM UTC-5 Vladimir Botka wrote:

> Sure, here is the gist
> https://gist.github.com/vbotka/10c57962976dd1e2dd3e9411d3745c75
>
> (Is it possible to reasonably format a code in Google Groups?)
> On Tuesday, November 7, 2023 at 6:55:50 PM UTC+1 dbs34 wrote:
>
>> would you mind sharing the whole, newly updated playbook?  thank you!
>>
>> On Monday, November 6, 2023 at 2:58:31 PM UTC-5 Vladimir Botka wrote:
>>
>>> On Fri, 3 Nov 2023 08:40:19 -0700 (PDT) 
>>> Dimitri Yioulos  wrote: 
>>>
>>> > --- PING REPORT --- 
>>> > {% for pr in ping_result.results %} 
>>> > {{ pr.stdout_lines | first }} 
>>> > {{ pr.stdout_lines | last }} 
>>> > 
>>> > {% endfor %} 
>>> > run_once: true 
>>> > delegate_to: localhost 
>>> > 
>>> > That works fine. However, I want to use ansible.builtin.ping ... 
>>>
>>>
>>> Ignore unreachable hosts in a block 
>>>
>>> - block: 
>>> - ping: 
>>> register: out 
>>> - set_fact: 
>>> unr: "{{ out.unreachable|d(false) }}" 
>>> ignore_unreachable: true 
>>>
>>> and declare the dictionary 
>>>
>>> h_unr: "{{ dict(ansible_play_hosts_all| 
>>> zip(ansible_play_hosts_all| 
>>> map('extract', hostvars, 'unr'))) }}" 
>>>
>>> For example, the below play 
>>>
>>> - hosts: test_01,test_05,test_06,test_07 
>>> gather_facts: false 
>>>
>>> vars: 
>>>
>>> h_unr: "{{ dict(ansible_play_hosts_all| 
>>> zip(ansible_play_hosts_all| 
>>> map('extract', hostvars, 'unr'))) }}" 
>>>
>>> tasks: 
>>>
>>> - block: 
>>> - ping: 
>>> register: out 
>>> - set_fact: 
>>> unr: "{{ out.unreachable|d(false) }}" 
>>> ignore_unreachable: true 
>>>
>>> - debug: 
>>> var: h_unr 
>>> run_once: true 
>>> delegate_to: localhost 
>>>
>>> gives (abridged) 
>>>
>>> ok: [test_01 -> localhost] => 
>>> h_unr: 
>>> test_01: false 
>>> test_05: true 
>>> test_06: true 
>>> test_07: true 
>>>
>>> -- 
>>> Vladimir Botka 
>>>
>>

-- 
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/47bbdd8e-7b5f-4dfc-ac60-375a532a9adbn%40googlegroups.com.


Re: [ansible-project] Ansible ping report

2023-11-07 Thread Vladimir Botka
Sure, here is the gist
https://gist.github.com/vbotka/10c57962976dd1e2dd3e9411d3745c75

(Is it possible to reasonably format a code in Google Groups?)
On Tuesday, November 7, 2023 at 6:55:50 PM UTC+1 dbs34 wrote:

> would you mind sharing the whole, newly updated playbook?  thank you!
>
> On Monday, November 6, 2023 at 2:58:31 PM UTC-5 Vladimir Botka wrote:
>
>> On Fri, 3 Nov 2023 08:40:19 -0700 (PDT) 
>> Dimitri Yioulos  wrote: 
>>
>> > --- PING REPORT --- 
>> > {% for pr in ping_result.results %} 
>> > {{ pr.stdout_lines | first }} 
>> > {{ pr.stdout_lines | last }} 
>> > 
>> > {% endfor %} 
>> > run_once: true 
>> > delegate_to: localhost 
>> > 
>> > That works fine. However, I want to use ansible.builtin.ping ... 
>>
>>
>> Ignore unreachable hosts in a block 
>>
>> - block: 
>> - ping: 
>> register: out 
>> - set_fact: 
>> unr: "{{ out.unreachable|d(false) }}" 
>> ignore_unreachable: true 
>>
>> and declare the dictionary 
>>
>> h_unr: "{{ dict(ansible_play_hosts_all| 
>> zip(ansible_play_hosts_all| 
>> map('extract', hostvars, 'unr'))) }}" 
>>
>> For example, the below play 
>>
>> - hosts: test_01,test_05,test_06,test_07 
>> gather_facts: false 
>>
>> vars: 
>>
>> h_unr: "{{ dict(ansible_play_hosts_all| 
>> zip(ansible_play_hosts_all| 
>> map('extract', hostvars, 'unr'))) }}" 
>>
>> tasks: 
>>
>> - block: 
>> - ping: 
>> register: out 
>> - set_fact: 
>> unr: "{{ out.unreachable|d(false) }}" 
>> ignore_unreachable: true 
>>
>> - debug: 
>> var: h_unr 
>> run_once: true 
>> delegate_to: localhost 
>>
>> gives (abridged) 
>>
>> ok: [test_01 -> localhost] => 
>> h_unr: 
>> test_01: false 
>> test_05: true 
>> test_06: true 
>> test_07: true 
>>
>> -- 
>> Vladimir Botka 
>>
>

-- 
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/af3ef710-2205-48b8-9004-84da0cf9f27an%40googlegroups.com.


Re: [ansible-project] Ansible ping report

2023-11-07 Thread dbs34
would you mind sharing the whole, newly updated playbook?  thank you!

On Monday, November 6, 2023 at 2:58:31 PM UTC-5 Vladimir Botka wrote:

> On Fri, 3 Nov 2023 08:40:19 -0700 (PDT)
> Dimitri Yioulos  wrote:
>
> > --- PING REPORT ---
> > {% for pr in ping_result.results %}
> > {{ pr.stdout_lines | first }}
> > {{ pr.stdout_lines | last }}
> > 
> > {% endfor %}
> > run_once: true
> > delegate_to: localhost
> > 
> > That works fine. However, I want to use ansible.builtin.ping ...
>
>
> Ignore unreachable hosts in a block
>
> - block:
> - ping:
> register: out
> - set_fact:
> unr: "{{ out.unreachable|d(false) }}"
> ignore_unreachable: true
>
> and declare the dictionary
>
> h_unr: "{{ dict(ansible_play_hosts_all|
> zip(ansible_play_hosts_all|
> map('extract', hostvars, 'unr'))) }}"
>
> For example, the below play
>
> - hosts: test_01,test_05,test_06,test_07
> gather_facts: false
>
> vars:
>
> h_unr: "{{ dict(ansible_play_hosts_all|
> zip(ansible_play_hosts_all|
> map('extract', hostvars, 'unr'))) }}"
>
> tasks:
>
> - block:
> - ping:
> register: out
> - set_fact:
> unr: "{{ out.unreachable|d(false) }}"
> ignore_unreachable: true
>
> - debug:
> var: h_unr
> run_once: true
> delegate_to: localhost
>
> gives (abridged)
>
> ok: [test_01 -> localhost] => 
> h_unr:
> test_01: false
> test_05: true
> test_06: true
> test_07: true
>
> -- 
> Vladimir Botka
>

-- 
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/b44a0d87-3bcb-4523-bca7-cd352fa9c6ean%40googlegroups.com.


Re: [ansible-project] Ansible ping report

2023-11-06 Thread Vladimir Botka
On Fri, 3 Nov 2023 08:40:19 -0700 (PDT)
Dimitri Yioulos  wrote:

> --- PING REPORT ---
> {% for pr in ping_result.results %}
> {{ pr.stdout_lines | first }}
> {{ pr.stdout_lines | last }}
> 
> {% endfor %}
> run_once: true
> delegate_to: localhost
> 
> That works fine. However, I want to use ansible.builtin.ping ...


Ignore unreachable hosts in a block

- block:
- ping:
  register: out
- set_fact:
unr: "{{ out.unreachable|d(false) }}"
  ignore_unreachable: true

and declare the dictionary

  h_unr: "{{ dict(ansible_play_hosts_all|
  zip(ansible_play_hosts_all|
  map('extract', hostvars, 'unr'))) }}"

For example, the below play

- hosts: test_01,test_05,test_06,test_07
  gather_facts: false

  vars:

h_unr: "{{ dict(ansible_play_hosts_all|
zip(ansible_play_hosts_all|
map('extract', hostvars, 'unr'))) }}"

  tasks:

- block:
- ping:
  register: out
- set_fact:
unr: "{{ out.unreachable|d(false) }}"
  ignore_unreachable: true

- debug:
var: h_unr
  run_once: true
  delegate_to: localhost

gives (abridged)

ok: [test_01 -> localhost] => 
  h_unr:
test_01: false
test_05: true
test_06: true
test_07: true

-- 
Vladimir Botka

-- 
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/20231106205809.164c42fb%40gmail.com.


pgpEoJmLL1d_d.pgp
Description: OpenPGP digital signature


Re: [ansible-project] Ansible ping report

2023-11-06 Thread Brian Coca
sorry, had only read the bottom, too used to people making that
mistake, but you did not.



-- 
--
Brian Coca

-- 
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/CACVha7drXWLCVJ5cL4qfPYy08FeMYk5RG_XhaeU_U690pF7P5w%40mail.gmail.com.


Re: [ansible-project] Ansible ping report

2023-11-06 Thread Dimitri Yioulos
Thanks, Brian. I suspected that was the case (I did read doc for the 
module), but was hoping there was some work-around. I'm still interested in 
trying to generate some type of report from Ansible ping. As you can see 
from the playbook I posted, that uses ICMP ping, which is not what I'm 
after.
On Monday, November 6, 2023 at 10:15:28 AM UTC-5 Brian Coca wrote:

> Not all modules return a stdout nor stdout_lines, ping is one of them,
> it returns 'data', check the RETURN DOCS for each module to know the
> structure of the registered var they return.
>
>
> -- 
> --
> Brian Coca
>
>

-- 
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/8a612b95-ce31-4d6d-b92e-5e4240fcbf3bn%40googlegroups.com.


Re: [ansible-project] Ansible ping report

2023-11-06 Thread Brian Coca
Not all modules return a stdout nor stdout_lines, ping is one of them,
it returns 'data', check the RETURN DOCS for each module to know the
structure of the registered var they return.


-- 
--
Brian Coca

-- 
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/CACVha7d60%3D7kBkG0LuA7oTB_whwQVT98PZ87M4bj0AW9OobOsQ%40mail.gmail.com.


[ansible-project] Ansible ping report

2023-11-06 Thread Dimitri Yioulos
Hi, All.

It's been some time since I used Ansible, and this list. I'm happy to be 
back using both.

I've created a [playbook to run pin, and create a report of output:

---
- name: check reachable hosts
hosts: dytest
gather_facts: no
tasks:
- name: Ping each host
ansible.builtin.command: ping -c1 {{ item }}
run_once: true
loop: "{{ ansible_play_hosts_all }}"
delegate_to: localhost
register: ping_result
failed_when: false

- name: How did we do?
ansible.builtin.debug:
msg: "{{ ping_result.results[0] }}"
run_once: true

- name: Format it a bit
# Note: the docs for 'copy' say don't do this, to use template instead,
# and there are reasons, but this suffices for posting purposes.
# The 'content:' below should be in your template.
ansible.builtin.copy:
dest: /home/deploy/ping.txt
content: |
--- PING REPORT ---
{% for pr in ping_result.results %}
{{ pr.stdout_lines | first }}
{{ pr.stdout_lines | last }}

{% endfor %}
run_once: true
delegate_to: localhost

That works fine. However, I want to use ansible.builtin.ping, not regular 
ping. When I substitute in ansible.builtin ping, I get the error:

fatal: [bed-test-9-dy1]: FAILED! => {"msg": "The task includes an option 
with an undefined variable. The error was: 'dict object' has no attribute 
'stdout'. 'dict object' has no attribute 'stdout'\n\nThe error appears to 
be in '/etc/ansible/playbooks/ping4.yml': line 15, column 7, but may\nbe 
elsewhere in the file depending on the exact syntax problem.\n\nThe 
offending line appears to be:\n\n\n- name: How did we do?\n 

What do I need to do to accomplish what I'm after?

Many thanks.

-- 
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/e7a88e24-658d-40f1-b6ab-d7a152c79109n%40googlegroups.com.