1

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.

udondan
  • 57,263
  • 20
  • 190
  • 175
user3267989
  • 299
  • 3
  • 18

2 Answers2

0

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.

udondan
  • 57,263
  • 20
  • 190
  • 175
  • udondan@ : Thanks for your comment. I understand that the only arguments the custom modules get is what's explicitly passed to it. However, if I can somehow "extract" hostname (like we can do in the playbook using {{ inventory_hostname }} Jinja variable, then I can explicitly pass that to the custom module. – user3267989 Feb 26 '16 at 07:25
  • Ah, ok, so your question is actually how to get the hostname through the Ansible API and not how to get the hostname inside the module? I'm sure that's possible but I have no experience with the API. I also think there are not so many Ansible users here on SO with API experience. You might want to also ask the question then on the Ansible mailing list to get faster response. – udondan Feb 26 '16 at 07:31
0

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

user3267989
  • 299
  • 3
  • 18