Ansible API : Custom Module

683 Views Asked by At

I would like to use a custom module for which I require "hostname" so that I can initiate SSH connection from the custom module and run commands. So I pass transport = "local" to the Runner object. However, I find no way to obtain "hostname" information in the custom module.

I am using Ansible 1.9.2 using Python API.

2

There are 2 best solutions below

2
udondan On

A module only has the information available that was explicitly passed to it. What you might be interested in instead is an action plugin, which by (non-exisiting) definition runs local on the control machine and has access to more (all?) data.

You can see some action plugin code here: https://github.com/ansible/ansible/tree/devel/lib/ansible/plugins/action

PS: Don't you want to upgrade to Ansible 2 before getting started writing custom modules/plugins? The API changed completely and once you upgrade you have to rewrite you module/plugin.

0
user3267989 On

Okay, silly me. It's exactly the same way in the API too. You can extract hostname using {{ inventory_hostname }}.