Piosipov
Piosipov

Reputation: 83

Ansible. Looping over values extracted from output

im quite new to Ansible and eager to learn how much it can do. I have a task to do the following:

  1. Run command bpps -a to get a list of running NetBackup processes.
  2. Go through each string of output (we're interested in strings having 'bpbrm -backup' in them)
  3. Extract following data from those strings: Policy name (field after -cl key), Backup Schedule (field after -sched key), Hostname (field after -c key) ( I assume regex with capturing groups will do the trick? )
  4. Put this data into data structure we can iterate over
  5. Iterate over this data structure and run the bpbackup -i -p policy_name -s schedule_name -h hostname command to restart backup jobs for the client that were running backups at the time when we've collected data with bpps -a

I can do it with my eyes closed on Python, however i'd like to be able to do such tasks using Ansible alone to improve my skills if it takes sane amount of time and code :)

I would very appreciate if you will give some explanation on how it works, also could you please share some resources where i can find some Ansible tasks to practice on?

Its super easy for Python but I wasn't able to find some practice for Ansible yet. Thanks in advance and sorry for lame question :)

The output:

[[email protected] ~]# bpps -a
        NB Processes
        ------------
        root      8806     1  0 Sep30 ?        03:49:04 /usr/openv/pdde/pdag/bin/mtstrmd --config /usr/openv/lib/ost-plugins//mtstrm.conf
        root     11339     1  0 Sep30 ?        00:34:18 /usr/openv/netbackup/bin/vnetd -proxy inbound_proxy -number 0
        root     11340     1  1 Sep30 ?        05:30:58 /usr/openv/netbackup/bin/vnetd -proxy outbound_proxy -number 0
        root     11342     1  0 Sep30 ?        00:01:30 /usr/openv/netbackup/bin/vnetd -proxy http_pbx_tunnel -number 0
        root     11431     1  0 Sep30 ?        00:01:37 /usr/openv/netbackup/bin/vnetd -standalone
        root     11435     1  0 Sep30 ?        00:01:13 /usr/openv/netbackup/bin/bpcd -standalone
        root     11543     1  0 Sep30 ?        00:02:43 /usr/openv/netbackup/bin/nbdisco
        root     11674     1  0 01:18 ?        00:00:00 bpbrm -backup -S netbackup.master.server -c server1.domain.org -ct 13 -ru root -cl backup_policy-1 -sched Differential-File-6W -b server1.domain.org_1602890336 -bt 1602890336 -dt 109422 -st 1 -mediasvr media.server -jobid 11406453 -jobgrpid 11406420 -masterversion 812000 -maxfrag 51200 -bpstart_time 1602890636 -reqid -1601517467 -mt 0 -to 0 -stunit SS4-2_HEL_STU -rl 4 -rp 3628800 -eari 0 -p _PhysicalLSU -mst 6 -flags 0 -storagesvr storage_server-sts_type FTS -use_ofb -use_otm -WOFB_enabled 1 -nbu -jm -secure 1 -kl 28 -rg root -fso -tir -tir_plus -stream_count 5 -stream_number 5 -keyword NBUCS_3-4_6W_Hell-to-Ball -connect_options 16974338
        root     11678     1  0 01:19 ?        00:00:03 bpbrm -backup -S netbackup.master.server -c server2.domain.org -ct 13 -ru root -cl backup_policy-7 -sched Cumulative-File-6W -b server2.domain.org_1602890342 -bt 1602890342 -dt 1229040 -st 4 -mediasvr media.server -jobid 11406641 -jobgrpid 11406583 -masterversion 812000 -maxfrag 51200 -bpstart_time 1602890642 -reqid -1601517468 -mt 0 -to 0 -stunit SS4-2_HEL_STU -rl 4 -rp 3628800 -eari 0 -p _PhysicalLSU -mst 6 -flags 0 -storagesvr storage_server-sts_type FTS -use_ofb -use_otm -WOFB_enabled 1 -nbu -jm -secure 1 -kl 28 -rg root -fso -tir -tir_plus -stream_count 3 -stream_number 3 -keyword NBUCS_3-4_6W_Hell-to-Ball -connect_options 16974338
        root     11679 11674  2 01:19 ?        00:09:08 bptm -w -c server1.domain.org -dpath _PhysicalLSU -stunit SS4-2_HEL_STU -cl backup_policy-1 -bt 1602890336 -b server1.domain.org_1602890336 -st 1 -cj 1 -reqid -1601517467 -jm -brm -hostname server1.domain.org -ru root -rclnt server1.domain.org -rclnthostname server1.domain.org -rl 4 -rp 3628800 -sl Differential-File-6W -ct 13 -maxfrag 51200 -tir -eari 0 -mediasvr media.server -no_callback -connect_options 0x01010100 -auth_only -jobid 11406453 -jobgrpid 11406420 -masterversion 812000 -bpbrm_outsock_fd 4 -bpbrm_shm_id 667025419 -blks_per_buffer 512
        root     11696 11679  3 01:19 ?        00:15:00 bptm -w -c server1.domain.org -dpath _PhysicalLSU -stunit SS4-2_HEL_STU -cl backup_policy-1 -bt 1602890336 -b server1.domain.org_1602890336 -st 1 -cj 1 -reqid -1601517467 -jm -brm -hostname server1.domain.org -ru root -rclnt server1.domain.org -rclnthostname server1.domain.org -rl 4 -rp 3628800 -sl Differential-File-6W -ct 13 -maxfrag 51200 -tir -eari 0 -mediasvr media.server -no_callback -connect_options 0x01010100 -auth_only -jobid 11406453 -jobgrpid 11406420 -masterversion 812000 -bpbrm_outsock_fd 4 -bpbrm_shm_id 667025419 -blks_per_buffer 512
        root     11701 11678  1 01:19 ?        00:08:57 bptm -w -c server2.domain.org -dpath _PhysicalLSU -stunit SS4-2_HEL_STU -cl backup_policy-7 -bt 1602890342 -b server2.domain.org_1602890342 -st 4 -cj 1 -reqid -1601517468 -jm -brm -hostname server2.domain.org -ru root -rclnt server2.domain.org -rclnthostname server2.domain.org -rl 4 -rp 3628800 -sl Cumulative-File-6W -ct 13 -maxfrag 51200 -tir -eari 0 -mediasvr media.server -no_callback -connect_options 0x01010100 -auth_only -jobid 11406641 -jobgrpid 11406583 -masterversion 812000 -bpbrm_outsock_fd 4 -bpbrm_shm_id 667090957 -blks_per_buffer 512
        root     11718 11701  3 01:19 ?        00:15:36 bptm -w -c server2.domain.org -dpath _PhysicalLSU -stunit SS4-2_HEL_STU -cl backup_policy-7 -bt 1602890342 -b server2.domain.org_1602890342 -st 4 -cj 1 -reqid -1601517468 -jm -brm -hostname server2.domain.org -ru root -rclnt server2.domain.org -rclnthostname server2.domain.org -rl 4 -rp 3628800 -sl Cumulative-File-6W -ct 13 -maxfrag 51200 -tir -eari 0 -mediasvr media.server -no_callback -connect_options 0x01010100 -auth_only -jobid 11406641 -jobgrpid 11406583 -masterversion 812000 -bpbrm_outsock_fd 4 -bpbrm_shm_id 667090957 -blks_per_buffer 512
        root     11748     1  2 Sep30 ?        09:50:47 /usr/openv/pdde/pdcr/bin/spad
        root     11995     1 99 Sep30 ?        37-03:30:41 /usr/openv/pdde/pdcr/bin/spoold
        root     12036     1  0 Sep30 ?        00:05:50 /usr/openv/netbackup/bin/nbrmms
        root     12109     1  0 Sep30 ?        00:59:19 /usr/openv/netbackup/bin/nbsl
        root     12146     1  0 Sep30 ?        00:02:52 /usr/openv/netbackup/bin/nbsvcmon
        
        
        MM Processes
        ------------
        root     12015     1  0 Sep30 ?        00:01:50 /usr/openv/volmgr/bin/ltid
        root     12022     1  0 Sep30 ?        00:01:31 vmd
        root     12278 12015  0 Sep30 ?        00:00:00 tldd
        root     12356 12015  0 Sep30 ?        00:02:23 avrd
        root     12359     1  0 Sep30 ?        00:00:00 tldcd

Upvotes: 1

Views: 179

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Q: "1) It's super easy for Python but I wasn't able to find some practice for Ansible yet. 2) I'm not asking for a fish I need a net."

A: Let me provide you with the "net". In this case, the best option is custom filter plugin. For example, the playbook below

- hosts: localhost
  tasks:
    - shell: 'df|grep snap/core'
      register: result
    - debug:
        var: result.stdout_lines

gives

  result.stdout_lines:
  - /dev/loop3         99456    99456         0 100% /snap/core/9993
  - /dev/loop4         56704    56704         0 100% /snap/core18/1885
  - /dev/loop10        56320    56320         0 100% /snap/core18/1880
  - /dev/loop6        100096   100096         0 100% /snap/core/10126

Let's create a custom filter to parse the data. For example

shell> cat filter_plugins/parse_lines.py
def parse_lines(lines):
    array=[]
    for l in lines:
        array.append(l.split())
    return array

class FilterModule(object):

        def filters(self):
            return {
                'parse_lines': parse_lines
            }

Then the task below

    - debug:
        msg: "{{ result.stdout_lines|parse_lines|to_yaml }}"

gives the lists with the parsed parameters

  msg: |-
    - [/dev/loop3, '99456', '99456', '0', 100%, /snap/core/9993]
    - [/dev/loop4, '56704', '56704', '0', 100%, /snap/core18/1885]
    - [/dev/loop10, '56320', '56320', '0', 100%, /snap/core18/1880]
    - [/dev/loop6, '100096', '100096', '0', 100%, /snap/core/10126]

Upvotes: 1

Related Questions