Splitting the Output data of a command and process the splitted and saved data further in Ansible

Viewed 30

I have a situation as below.
I have to run a command that fetches a data related to two nodes (let us say:NODE-1, NODE-2. Please note that there is no way to fetch the data seperately for NODE-1 and NODE-2). The sample data is as shown below.

#################### NODE-1 ####################
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                  12G  8.0K   12G   1% /dev
root                     4.0G  2.4G  1.7G  59% /
tmpfs                     17G  796K   17G   1% /dev/user1
tmpfs                     12G  1.2G   11G  10% /run
tmpfs                     12G     0   12G   0% /sys/user1
tmpfs                    2.4G     0  2.4G   0% /run/user1/0
/dev/sda1                3.9G  158M  3.5G   5% /boot
/dev/sda2                3.9G  158M  3.5G   5% /var/log
/dev/sda3                9.8G  1.1G  8.2G  12% /var/opt
none                      64M     0   64M   0% /var/opt/group1
none                      64M     0   64M   0% /var/opt/group2
#################### NODE-2 ####################
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                  12G  8.0K   12G   1% /dev
root                     4.0G  2.4G  1.7G  59% /
tmpfs                     17G  796K   17G   1% /dev/user1
tmpfs                     12G  1.2G   11G  10% /run
tmpfs                     12G     0   12G   0% /sys/user1
tmpfs                    2.4G     0  2.4G   0% /run/user1/0
/dev/sda1                3.9G  158M  3.5G   5% /boot
/dev/sda2                3.9G  158M  3.5G   5% /var/log
/dev/sda3                9.8G  1.1G  8.2G  12% /var/opt
none                      64M     0   64M   0% /var/opt/group1
none                      64M     0   64M   0% /var/opt/group2

It is required to process the data of these two nodes sepearely and draw & print the conclusions individually based on their repsctive values.

I am unable to find a way to seperate the data of these two nodes and save them as seperate variables/ in files to process them further.

PS: The number of fields (rows) in the data of the nodes is not constant.

Can I get a way (atleast a hint) to do it using ansible ?

1 Answers

i suggest you to use a custom filter (splitregex): you put this file

in folder filter_plugin (same level than your playbook)

#!/usr/bin/python
import re
class FilterModule(object):
    def filters(self):
        return {
            'splitregex': self.splitregex
        }

    def splitregex(self, l1, pattern):       
        return filter(None, re.split(pattern, l1, flags=re.MULTILINE))

you use it in this playbook:(the file datas.txt contains your initial result)

- hosts: localhost
  gather_facts: false

  tasks:
    - name: read file
      shell: cat datas.txt 
      register: cc

    - name: show item
      debug:
        msg: "{{ item }}"
      loop: "{{ parts }}"
      vars:
        parts: "{{ cc.stdout | splitregex('^#+ NODE-[0-9]+ #+$') }}"

parts is a list which contains your initial file cut following the pattern

you could use '^#+ NODE-[0-9]+ #+$\n' if you want to remove the \n in beginning of each part


if you are using roles

for role test (for example), you create a folder roles/test/filter_plugins and put it the python file in it

Related