Home > OS >  How to get args for an ansible module written in bash?
How to get args for an ansible module written in bash?

Time:04-13

I wrote an ansible module is bash. It is working fine, but if want to pass arguments to that module and read them in the bash module how can I do that .. please help

- name: get top processes consuming high cpu
  gettopprocesses:
    numberofprocesses: 5

In library I have bash script library/gettopprocesses.sh

#!/bin/bash
TPCPU=$(ps aux --sort -%cpu | head -${numberofprocesses}
echo "{\"changed\": false, "\msg\": {"$TPCPU"}}"
exit 0

CodePudding user response:

i write your bask like this: you have to add source $1 to specify you have args

#!/bin/bash
source $1
NUMBERPROC=$numberofprocesses
TPCPU=$(ps aux --sort -%cpu | head -${NUMBERPROC})

printf '{"changed": %s, "msg": "%s", "contents": %s}' "false" "$TPCPU" "contents"
exit 0

you could add a test to check if right arg is given:

#!/bin/bash
source $1

if [ -z "$numberofprocesses" ]; then
    printf '{"failed": true, "msg": "missing required arguments: numberofprocesses"}'
    exit 1
fi

NUMBERPROC=$numberofprocesses
TPCPU=$(ps aux --sort -%cpu | head -${NUMBERPROC})

printf '{"changed": %s, "msg": "%s", "contents": %s}' "false" "$TPCPU" "contents"
exit 0
  • Related