I would like to convert my BASH scripts to Ansible User Modules

1,264 views
Skip to first unread message

JohnP

unread,
May 16, 2014, 12:09:27 PM5/16/14
to ansibl...@googlegroups.com
Hi All,

I have written some BASH ;( scripts that collects some information and shows them
"very" human friendly and colourful format. Via Aws Cli, json processing etc.
An obfuscated example as below for displaying the idea // result.

#========================================================
#=== VPCCOUNT                       X
#=== VPCID                          vpc-xxxxxxxx
#=== HOWMANYSECGRPs                 Y
#=== myVPCCIDR                      ZZ.X.X.X/XX
#=== myVPCTAG                       ZZ.X.X.X/XX
#========================================================

#--------------------------------------------------------
#-- GRP 1   vpc-xxxxxxxx  sg-aaaaaaaa  BBBBBBBBBBBBBB-sgp
#--------------------------------------------------------

RULES  1:  1:   12345   -tcp12345   111.111.111.111/32
RULES  1:  1:   12345   -tcp12345   222.222.222.222/32
RULES  1:  1:   12345   -tcp12345   333.333.333.333/32
RULES  1:  1:   12345   -tcp12345   444.444.444.444/32
RULES  1:  1:   12345   -tcp12345   555.555.555.555/32

#--------------------------------------------------------
#-- GRP 2   vpc-yyyyyyyy  sg-cccccccc  default
#--------------------------------------------------------
RULES  1:  1:           -nul-          sg-ssssssss


Now, I would like to convert those as Ansible User Modules.
The first hurdle I am facing that making Ansible print formatted & coloured
output (like mine above) on STDOUT (a.k.a. the terminal I an running it,
Linux, Centos, BASH). I love Python, btw.

Could you kindly give me some pointers and ideas for me to get started?

Best Regards: JohnP

James Cammarata

unread,
May 16, 2014, 3:08:27 PM5/16/14
to JohnP, ansibl...@googlegroups.com
All output is handled via callbacks - the modules themselves do not output anything other than JSON. Theoretically you could create your own callback plugin to create output similar to yours above, however you would have to use it via the Ansible API. See the code in bin/ansible or bin/ansible-playbook to see how the callbacks are used there.


--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
May 16, 2014, 7:32:19 PM5/16/14
to James Cammarata, JohnP, ansibl...@googlegroups.com
If you have bash scripts that return that structured output, and want to wrap them with a module, you could.

Really the question is how to write  a module in Bash -- that's easy.

The trick to writing a bash module in ansible is reading $1 (first argument) for the name of a file that returns key=value variables.

Then, either return JSON, or a series of results in key=value format, one per line (aka "baby JSON")

ex:


But I probably wouldn't try to parse your existing CLI, given the color codes and such.

JohnP

unread,
May 19, 2014, 10:45:11 AM5/19/14
to ansibl...@googlegroups.com, James Cammarata, JohnP
Hi, thanks to all whom kindly & promptly replied to my request.

> Brian Coca
> James Cammarata
> Michael DeHaan

After reading to posts, I am delighted to realise I can convert my scripts
to Ansible & Python via "calbacks".
I really like Python (although sadly not an expert at the moment).

> ...want to wrap them with a module, you could...

Since I have written them and know the algorithm, I would like to convert,
instead of making yet more calls to AWS API (written in Python), external
applications and process JSON in BASH inefficiently.
It seems like Boto and Ansible are marriage made in heaven. I'll definitely
have go as a start.

Best Regards: JohnP

Michael DeHaan

unread,
May 25, 2014, 5:20:12 PM5/25/14
to JohnP, ansibl...@googlegroups.com, James Cammarata
FYI - Ansible modules in bash and callbacks have nothing in common.   

Callbacks in Ansible are a way of hooking output and sending them to different places.



Reply all
Reply to author
Forward
0 new messages