Ping module status in CSV

26 views
Skip to first unread message

Vandana Thakur

unread,
Apr 18, 2019, 1:15:34 AM4/18/19
to : Ansible Project
Hello Team,

I have  created a csv for gathered facts of linux and  windows targets.

Now i am trying to add a column with Ping module status and result code if it fails.
I  wrote this play in the role- this is for linux targets :


---
    - ping:
      register: result


    - debug: var=hostvars[inventory_hostname].result

    - setup:
      delegate_facts: True

    - lineinfile:
        create: yes
        dest: /{{csv_log}}/setup.csv
        line: "Ping,Hostname,CPU_Architecture,OS,OS_VERSION,Customer_name,Location,Project"
        state: present
      delegate_to: localhost
      delegate_facts: True

    - lineinfile:
        create: yes
        regexp: "NONEXISTENTLINE"
        dest: /{{csv_log}}/setup.csv
        line: "{{ping_res.stdout_lines}},{{hostvars[inventory_hostname]['ansible_default_ipv4']['address']}},{{ inventory_hostname }},{{ ansible_architecture }},{{ ansible_distribution }},{{ ansible_distribution_version }}"
        state: present
      delegate_to: localhost
      delegate_facts: True


BUT the issue is , it gives all the output like :

[root@vuhplabspawx001 tasks]#   cat  /tmp/setup.csv
Ping,Hostname,CPU_Architecture,OS,OS_VERSION,Customer_name,Location,Project

[u'PING vuhplabspawx001 (172.20.24.188) 56(84) bytes of data.', u'64 bytes from vuhplabspawx001 (172.20.24.188): icmp_seq=1 ttl=64 time=0.039 ms', u'', u'--- vuhplabspawx001 ping statistics ---', u'1 packets transmitted, 1 received, 0% packet loss, time 0ms', u'rtt min/avg/max/mdev = 0.039/0.039/0.039/0.000 ms'],

172.20.24.188,vuhplabspawx001,x86_64,CentOS,7.2.1511
[root@vuhplabspawx001 tasks]#


How can i  sort this description to  only status and  result code  if  it  fails.

Thanks in advance.:)


S C Rigler

unread,
Apr 18, 2019, 9:14:27 AM4/18/19
to ansible...@googlegroups.com
Maybe use the value of "ping_res.rc".  It's the return code of the command; so it would be "0" for success.

Eg:

{{ (ping_res.rc|int == 0) | ternary('UP', 'DOWN') }}

--Steve

--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALJDb0mMwEGMXR3LP1vfOPjumBL9xkdYU9Hy-jRZNq_v%3DvhFMw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Vandana Thakur

unread,
Apr 18, 2019, 12:13:45 PM4/18/19
to ansible...@googlegroups.com
Thanks , but how can i register this status simultaneously, since i need to call that status in csv . 

Also  can i grep  a specifuc string from ping module output n then  define a customised result code and set that result code value in csv ? 


Thanks :)

Vandana Thakur

unread,
Apr 22, 2019, 6:07:58 AM4/22/19
to : Ansible Project
Further, to  customize this.
I  now have the ping module output for target hosts.
Now haw can i  decide result code in the ping output.

Like eg, if  A(some string) is  present in the ping output then Register RC = x
if  B(some string) is  present in the ping output then Register RC =y

these registered  values to  be called in the  above  csv .

I  tried ternary  expressions(as suggested)  but  i  don't know how to fetch the above condition .

Thanks  for the  help.



BR//
Vandana
Reply all
Reply to author
Forward
0 new messages