cancel
Showing results for 
Search instead for 
Did you mean: 
Trevor
Commander Commander
Commander
  • 340 Views

Ansible Module Development

If I'm going to write an Ansible module, that targets Linux systems, 
what language, other than Python, would/could I use to write that
module?

Trevor "Red Hat Evangelist" Chandler
Labels (1)
4 Replies
Travis
Moderator
Moderator
  • 269 Views

@Trevor -

I would highly encourage Python, as that would be the "Native" and standard choice for Ansible since most Ansible modules are already built using Python and there is an AnsibleModule utility class. Additionally, you could use an existing Ansible module as a template for a module in your new collection.

It is possible to use other things like BASH, GO, Rust, C, or other languages, but each one would have some challenges such as possibly needing to compile and have binaries or tools to be available and installed. 

The important things or requirements for an Ansible Module is that it must have both Input and Output for the typical Ansible module protocol. This output is expected to be JSON - just like custom inventory plugins or inventory scripts, so again, some languages are better suited to make dealiing with JSON easier.

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training
Trevor
Commander Commander
Commander
  • 154 Views

And this response is the very reason I bring this type of query 
to the RHLC!

Many thanks Travis!!!

Trevor "Red Hat Evangelist" Chandler
0 Kudos
Chetan_Tiwary_
Community Manager
Community Manager
  • 175 Views

@Trevor Agree with Travis on this, python is the best bcz it is easiest and integrates well with Ansible libraries. 

Since the Ansible module contract relies on a straightforward JSONin/JSON out communication model, you have the flexibility to write modules in virtually any programming language. The only real requirements are that the language must be executable on the managed host and capable of reading input that is either via standard input or a temporary file and returning avalid JSON.

This approach has led to a wide variety of community driven examples. For instance, you will find everything from lightweight Bash scripts that handle simple tasks to compiled binaries written in Go or Rust for more performance-critical work. Even interpreted languages like Perl and Ruby work perfectly fine, provided they include the correct shebang. Ultimately, as long as the code can be executed on the target and emit the expected JSON structure, Ansible can manage it.

 

https://forum.ansible.com/t/modules-in-languages-other-than-python/30997 

https://docs.ansible.com/projects/ansible/latest/dev_guide/developing_modules_general.html 

Trevor
Commander Commander
Commander
  • 154 Views

Pure gold!

Many thanks Chetan!!!

Trevor "Red Hat Evangelist" Chandler
0 Kudos
Join the discussion
You must log in to join this conversation.