custom module, how to locate ansible magic vars

I am trying to write my first custom module and cannot find how to locate an ansible var that is not part of the task parameters sent. For example, let’s say I want to find the ansible_ssh_host or inventory_hostname and they are not in the environment vars. I searched for import ansible.constants but could not find any examples or if this is the right module to call for this.

Any help would be much appreciated.

You cannot, you only have access to the options passed into the module itself.

I am curious how for example the copy module works. There are no options to pass the user, password or server but somehow it must work. I suspect it uses ssh private keys that are set in ansible.cfg file. Is this how it works and if so how does the copy module access these values? I know I can pass whatever I want into the environment, but was hoping there was an ansible module which could access the values in ansible.cfg. I found the ansible.config.manager module and was wondering if it is used to parse the ansible.cfg file.

I was able to parse the ansible.cfg file using “ansible.module_utils.six.moves import configparser” and pull the values I am looking for. Now I just need a way to pull the remote_addr variable so I would not need to pass in the server: option from the task.

configparser examples = https://wiki.python.org/moin/ConfigParserExamples

There’s another class of plugins that you can think of as “sitting on top of modules”:

https://docs.ansible.com/ansible/devel/plugins/action.html

Action plugins can do a lot of complicated things such as accessing vars and internal play contexts.

@texas copy does not read the connection information, that is what the
connection plugin does, it is not module specific.