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] Re: how to append to a line keeping

2023-11-08 Thread Sameer Modak
Thanks a lot Todd for making us learn new thing and developing more 
interest in ansible.

Absolute thanks Todd.

On Wednesday, November 1, 2023 at 7:12:16 PM UTC+5:30 Todd Lewis wrote:

> The short answer is no. If you want to modify the string with backrefs, 
> then lineinfile will not create the line if it doesn't exist.
>
> But step back and consider your options for managing your override.conf 
> file.
>
> A. The best scenario is to take total ownership of the entire file, 
> generating it from variables in your project. You have to take changes into 
> account as you upgrade kafka etc, but you should do that in any case.
>
> B. If for some reasons you aren't ready or able to take ownership of the 
> whole file, then the next best thing is to at least take complete ownership 
> of the 'Environment="KAFKA…' line, and build it from information in your 
> project. You can still have appropriate differences on various instances, 
> but that would all by controlled by data and logic in your Ansible project. 
> See the example below.
>
> C. The absolute worst scenario, the situation where you should be saying, 
> "How can we move to option 'B' or 'A' as soon as possible?", is when you 
> try to tweak a line that may or may not exist, in a file that may or may 
> not exist, and that may have unknown variances between hosts for reasons 
> that (apparently) aren't coming from your configuration management system. 
> As a stop-gap step in a house-on-fire situation, you've got to do what 
> you've got to do. But you should stop doing it as soon as possible.
>
> That's not to say that 'C' doesn't work; it does, and I've managed files 
> that way for years. But I got in that situation for two reasons: 1) Nobody 
> told me what I just told you, and 2) I figured my problem was to tweak a 
> line in a file, and if there was an Ansible module to do just that, then it 
> must be an okay way to do it.
>
> Here's a short playbook with variables to inform the line in question. 
> Clearly you could use group_vars, host_vars, and/or inventory variables to 
> tailor the result for individual hosts. But I strongly encourage you to 
> pull the responsibility for that entire line (option 'B') or better yet 
> that entire file (option 'A') into your Ansible project.
>
> ---
> # sameer-0.yml
> - name: KAFKA_JMX options management
>   hosts: localhost
>   gather_facts: false
>   vars:
> filename: sameer-0-override.conf
> kafka_jmx_opts:
>   - 
> '-javaagent:/usr/share/java/kafka/jolokia.jar=port=8778,host=pilot01.test'
>   - '-Dcom.sun.management.jmxremote'
>   - '-Dcom.sun.management.jmxremote.authenticate=false'
>   - '-Dcom.sun.management.jmxremote.ssl=false'
>   - '-Djava.rmi.server.hostname=pilot01.test'
>   - '-Dcom.sun.management.jmxremote.rmi.port=1099'
> kafka_jmx_opts_str: 'KAFKA_JMX_OPTS={{ kafka_jmx_opts | join(" ") }}'
>   tasks:
> - name: Manage kafka_jms_opts in override.conf
>   ansible.builtin.lineinfile:
> path: '{{ filename }}'
> regexp: 'Environment="KAFKA_JMX_OPTS=.*"'
> line: 'Environment="{{ kafka_jmx_opts_str }}"'
> state: present
> create: true # You should have "owner:", "group:", and "mode:" here 
> too.
>
>
>
> On 11/1/23 7:12 AM, Sameer Modak wrote:
>
> Hello Team, 
>
> I could figure out after 10 tries this is how i got it 
>
> - name: append the line
>   lineinfile:
>   path: /tmp/hello.txt
>   backrefs: yes
>   regexp: '^(.*KAFKA_JMX_OPTS.*)$"'.  <
>   line: '\1 
> -javaagent:{{jmxexporter_jar_path}}={{zookeeper_jmxexporter_port}}:{{zookeeper_jmxexporter_config_path}}"''
>
> but like can we handle the if else in one line in file 
>
> this will not work when file does not have "KAFKA_JMX_OPTS" in it. Can we 
> do it in same taks using item 2 regexp 
>
> if sting absent  add entire line else if string present append it.
>
>
>
>
>
>
> On Wednesday, November 1, 2023 at 4:11:40 PM UTC+5:30 Sameer Modak wrote:
>
>> We want to only append string  
>>  
>> -javaagent:{{jmxexporter_jar_path}}={{zookeeper_jmxexporter_port}}:{{zookeeper_jmxexporter_config_path}}"'
>>  
>>
>> to a file override.conf
>> if we have "Environment="KAFKA_JMX_OPTS" in it else 
>> add new line  "actual line"
>>
>>
>> Environment="KAFKA_OPTS=-javaagent:{{jmxexporter_jar_path}}={{zookeeper_jmxexporter_port}}:{{zookeeper_jmxexporter_config_path}}"'
>>
>> So basically append if something is there else add new line.
>>
>> I m struggling to get regex ryt.
>>
>> Currently the override.conf file has this below content with jmx 
>> parameters in it so  we want append  above line/string to  end of the 
>> existing line 
>>
>> Environment="KAFKA_JMX_OPTS=-javaagent:/usr/share/java/kafka/jolokia.jar=port=8778,host=pilot01.test
>>  
>> -Dcom.sun.management.jmxremote 
>> -Dcom.sun.management.jmxremote.authenticate=false 
>> -Dcom.sun.management.jmxremote.ssl=false 
>> -Djava.rmi.server.hostname=pilot01.test 
>>